Monday 16 January 2017

Chapter 19 Exercise 11, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

19.11 (ComplexMatrix)
Use the Complex class introduced in Programming Exercise 13.17
to develop the ComplexMatrix class for performing matrix operations
involving complex numbers. The ComplexMatrix class should extend the
GenericMatrix class and implement the add, multiple, and zero methods.
You need to modify GenericMatrix and replace every occurrence of Number by Object,
because Complex is not a subtype of Number. Write a test program that creates the
following two matrices and displays the result of addition and multiplication
of the matrices by invoking the printResult method.

import java.text.DecimalFormat;

public class Complex {

    // real numbers
    private double a;
    private double b;


    public Complex(double a, double b) {
        this.a = a;
        this.b = b;
    }

    public Complex(double a) {
        this(a, 0);
    }

    public Complex() {
        this(0);
    }

    public Complex add(Complex complex) {
        // a + bi + c + di = (a + c) + (b + d)i
        return new Complex((a + complex.a), (b + complex.b));
    }
    public Complex subtract(Complex complex) {
        // a + bi - (c + di) = (a - c) + (b - d)i

        return new Complex((a - complex.a), (b - complex.b));

    }
    public Complex multiply(Complex complex) {
        // (a + bi)*(c + di) = (ac - bd) + (bc + ad)i

        return new Complex((a * complex.a - b * complex.b), (b * complex.a + a * complex.b));

    }
    public Complex divide(Complex complex) {
        // (a+bi)/(c+di)=(ac+bd)/(c^2 +d^2)+(bc-ad)i/(c^2 +d^2)

        return new Complex( (a*complex.a+b*complex.b) / ((Math.pow(complex.a, 2) + Math.pow(complex.b, 2))),
                            (b * complex.a - a * complex.b) / ((Math.pow(complex.a, 2) + Math.pow(complex.b, 2))));
    }

    public double abs() {
        return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
    }

    public double getRealPart() {
        return a;
    }

    public double getImaginaryPart(){
        return b;
    }

    @Override
    public String toString() {

        if (b == 0) return a + "";

        DecimalFormat decimal = new DecimalFormat("#.###");
        return  "(" + decimal.format(a) + " + " + decimal.format(b) + "i)";

    }

}

public abstract class GenericMatrix<E extends Object> {
    /** Abstract method for adding two elements of the matrices */
    protected abstract E add(E o1, E o2);

    /** Abstract method for multiplying two elements of the matrices */
    protected abstract E multiply(E o1, E o2);

    /** Abstract method for defining zero for the matrix element */
    protected abstract E zero();

    /** Add two matrices */
    public E[][] addMatrix(E[][] matrix1, E[][] matrix2) {
        // Check bounds of the two matrices
        if ((matrix1.length != matrix2.length) ||
                (matrix1[0].length != matrix2[0].length)) {
            throw new RuntimeException(
                    "The matrices do not have the same size");
        }

        E[][] result =
                (E[][])new Object[matrix1.length][matrix1[0].length];

        // Perform addition
        for (int i = 0; i < result.length; i++)
            for (int j = 0; j < result[i].length; j++) {
                result[i][j] = add(matrix1[i][j], matrix2[i][j]);
            }

        return result;
    }

    /** Multiply two matrices */
    public E[][] multiplyMatrix(E[][] matrix1, E[][] matrix2) {
        // Check bounds
        if (matrix1[0].length != matrix2.length) {
            throw new RuntimeException(
                    "The matrices do not have compatible size");
        }

        // Create result matrix
        E[][] result =
                (E[][])new Object[matrix1.length][matrix2[0].length];

        // Perform multiplication of two matrices
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[0].length; j++) {
                result[i][j] = zero();

                for (int k = 0; k < matrix1[0].length; k++) {
                    result[i][j] = add(result[i][j],
                            multiply(matrix1[i][k], matrix2[k][j]));
                }
            }
        }

        return result;
    }

    /** Print matrices, the operator, and their operation result */
    public static void printResult(
            Object[][] m1, Object[][] m2, Object[][] m3, char op) {
        for (int i = 0; i < m1.length; i++) {
            for (int j = 0; j < m1[0].length; j++)
                System.out.print(" " + m1[i][j]);

            if (i == m1.length / 2)
                System.out.print("  " + op + "  ");
            else
                System.out.print("     ");

            for (int j = 0; j < m2.length; j++)
                System.out.print(" " + m2[i][j]);

            if (i == m1.length / 2)
                System.out.print("  =  ");
            else
                System.out.print("     ");

            for (int j = 0; j < m3.length; j++)
                System.out.print(m3[i][j] + " ");

            System.out.println();
        }
    }
}

public class Exercise_11 {


    public static void main(String[] args) {

        Complex[][] m1 = createMatrix(3);
        Complex[][] m2 = createMatrix(3);


        ComplexMatrix matrix = new ComplexMatrix();
        ComplexMatrix.printResult(m1, m2, matrix.addMatrix(m1, m2), '+');

    }

    public static Complex[][] createMatrix(int size) {
        Complex[][] m1 = new Complex[size][size];
        for (int i = 0; i < m1.length; i++) {
            for (int j = 0; j < m1[i].length; j++) {

                m1[i][j] = new Complex(Math.random() * 10, Math.random() * 10);
            }
        }

        return m1;
    }

}

class ComplexMatrix extends GenericMatrix<Complex> {

    @Override
    protected Complex add(Complex o1, Complex o2) {
        return o1.add(o2);
    }

    @Override
    protected Complex multiply(Complex o1, Complex o2) {
        return o1.multiply(o2);
    }

    @Override
    protected Complex zero() {
        return new Complex(0);
    }

}

No comments :

Post a Comment