使用 Jama 库进行矩阵乘法
我想使用 Jama 库乘以 2 个矩阵,但它返回:
A col: 4 row: 4
B col: 1 row: 4
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Matrix dimensions must agree.
我的代码:
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要执行
A. times(B)
用于矩阵乘法。arrayTimes
< /a> 是逐个元素相乘。You want to do
A.times(B)
for matrix multiplication.arrayTimes
is element by element multiplication.要更深入地了解 JAMA,我强烈建议:http://math.nist.gov/javanumerics /jama/doc/
For a deeper idea about JAMA, I really suggest: http://math.nist.gov/javanumerics/jama/doc/