Java 使用Colt库的简单矩阵运算(2D和3D)

Java 使用Colt库的简单矩阵运算(2D和3D),java,matrix,add,scalar,colt,Java,Matrix,Add,Scalar,Colt,我想在我的代码中执行简单的矩阵运算,并使用Colt库 (见此处:) 例如,我想给矩阵的每个单元格加/减/乘,加/减/乘/除一个标量等等……但是这个库中似乎没有这样的函数 然而,我发现了这样的评论: 如何使用assign()命令在代码中执行这些简单操作 为什么不试试(Java的线性代数)?它易于使用: Matrix a = new Basci2DMatrix(new double[][]{ { 1.0, 2.0 }, { 3.0, 4.0 } }); Matrix b = new

我想在我的代码中执行简单的矩阵运算,并使用Colt库

(见此处:)

例如,我想给矩阵的每个单元格加/减/乘,加/减/乘/除一个标量等等……但是这个库中似乎没有这样的函数

然而,我发现了这样的评论:

如何使用assign()命令在代码中执行这些简单操作

为什么不试试(Java的线性代数)?它易于使用:

Matrix a = new Basci2DMatrix(new double[][]{
    { 1.0, 2.0 },
    { 3.0, 4.0 }
});

Matrix b = new Basci2DMatrix(new double[][]{
    { 5.0, 6.0 },
    { 7.0, 8.0 }
});

Matrix c = a.multiply(b); // a * b
Matrix d = a.add(b); // a + b
Matrix e = a.subtract(b); // a - b
还有一种
transform()
方法类似于Colt的
assign()
。它可以按如下方式使用:

Matrix f = a.transform(Matrices.INC_MATRIX); // inreases each cell by 1
Matrix g = a.transform(Matrices.asDivFunction(2)); // divides each cell by 2
// you can define your own function
Matrix h = a.transform(new MatrixFunction {
  public double evaluate(int i, int j, int value) {
    return value * Math.sqrt(i + j);
  }
});
double scalar_to_add  = 0.5;
DoubleMatrix2D matrix = new DenseDoubleMatrix2D(10, 10); // creates an empty 10x10 matrix
matrix.assign(DoubleFunctions.plus(scalar_to_add)); // adds the scalar to each cell

但是它只适用于2D矩阵。

Colt在
assign()
方法下提供了一个更通用的框架。例如,如果要向矩阵的每个单元格添加标量,可以执行以下操作:

Matrix f = a.transform(Matrices.INC_MATRIX); // inreases each cell by 1
Matrix g = a.transform(Matrices.asDivFunction(2)); // divides each cell by 2
// you can define your own function
Matrix h = a.transform(new MatrixFunction {
  public double evaluate(int i, int j, int value) {
    return value * Math.sqrt(i + j);
  }
});
double scalar_to_add  = 0.5;
DoubleMatrix2D matrix = new DenseDoubleMatrix2D(10, 10); // creates an empty 10x10 matrix
matrix.assign(DoubleFunctions.plus(scalar_to_add)); // adds the scalar to each cell
标准函数在
DoubleFunctions
类中作为静态方法提供。其他的需要你来写

如果希望矢量相加而不是仅仅添加标量值,
assign()
的第二个参数需要是一个
DoubleDoubleFunction
。比如说,

DoubleDoubleFunction plus = new DoubleDoubleFunction() {
    public double apply(double a, double b) { return a+b; }
};    
DoubleMatrix1D aVector = ... // some vector
DoubleMatrix1D anotherVector = ... // another vector of same size
aVector.assign(anotherVector, plus); // now you have the vector sum

我想要一个适用于1D-2D-3D矩阵的库。。。我还想要数字分析,不仅仅是线性代数。。。