我对 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 direction
sum(mtx(::, *))
// Transpose(DenseVector(9.0, 12.0))
// sum along the row direction
sum(mtx(*, ::))
// DenseVector(3.0, 7.0, 11.0)
翻翻过去那场雪
相关分类