我对 Apache Common Math 很陌生,所以请原谅这些琐碎的问题。根据API doc,我无法弄清楚如何执行按列或按行求和。import org.apache.commons.math3.linear.RealMatrix;import org.apache.commons.math3.linear.MatrixUtils;double[][] values = {{1.0, 2.0}, {3.0, 4.0}, {5.0, 6.0}};RealMatrix mtx = MatrixUtils.createRealMatrix(values);上面的代码生成如下矩阵:[[1, 2], [3, 4], [5, 6]]执行列式求和的正确语法是什么?这会给我:[9, 12]我如何执行按行求和?这会给我:[3, 7, 11]为了进行比较,以下是 Scala 的 Breeze 库中的语法:import breeze.linalg._val mtx = DenseMatrix((1.0, 2.0), (3.0, 4.0), (5.0, 6.0))// sum along the column directionsum(mtx(::, *))// Transpose(DenseVector(9.0, 12.0))// sum along the row directionsum(mtx(*, ::))// DenseVector(3.0, 7.0, 11.0)
1 回答
翻翻过去那场雪
TA贡献2065条经验 获得超14个赞
似乎没有简单的方法可以实现,如何生成一个矩阵来求和?,例如:
//get the row sums
mtx.multiply(MatrixUtils.createRealMatrix(new double[][]{{1}, {1}}))
> Array2DRowRealMatrix{{3.0},{7.0},{11.0}}
//get the column sums
MatrixUtils.createRealMatrix(new double[][]{{1, 1, 1}}).multiply(mtx)
> Array2DRowRealMatrix{{9.0,12.0}}
添加回答
举报
0/150
提交
取消