Matrix multiplication using Jama library

6k Views Asked by At

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);
2

There are 2 best solutions below

0
On BEST ANSWER

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

arrayTimes is element by element multiplication.

0
On

For a deeper idea about JAMA, I really suggest: http://math.nist.gov/javanumerics/jama/doc/