javamatrixmultiplicationjama

Matrix multiplication using Jama library


I want to multiply 2 matrix using Jama library but it returns:

A col: 4 row: 4
B col: 1 row: 4
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Matrix dimensions must agree.

My code:

double[][] arrA = { {1,0,0,0}, {0, Math.cos(0.34), -Math.sin(0.34), 0}, {0, Math.sin(0.34), Math.cos(0.34), 0}, {0,0,0,1} };
        double[][] arrB = { {x}, {y}, {z}, {1} };
        Matrix A = new Matrix(arrA, 4, 4);
        Matrix B = new Matrix(arrB, 4, 1);
        A.print(1, 1);
        B.print(1, 1);
        System.out.println("A col: " + A.getColumnDimension() + " row: " + A.getRowDimension());
        System.out.println("B col: " + B.getColumnDimension() + " row: " + B.getRowDimension());
        Matrix C = A.arrayTimes(B);

Solution

  • You want to do A.times(B) for matrix multiplication.

    arrayTimes is element by element multiplication.