0

我想使用 Colt 库求解线性方程矩阵*X=D 。我试过了 :

DoubleMatrix2D matrix;
matrix = new DenseDoubleMatrix2D(4,4);
for (int row = 0; row < 4; row++) {
    for (int column = 0; column < 4; column++) {
        // We set and get a cell value:             
        matrix.set(row,column,row+column);          
    }
}
DoubleMatrix2D D;
D = new DenseDoubleMatrix2D(4,1);
D.set(0,0, 1.0);
D.set(1,0, -1.0);
D.set(2,0, 91.0);
D.set(3,0, -5.0);
DoubleMatrix2D X;
X = solve(matrix,D);

但我收到一个错误
"The method solve(DoubleMatrix2D, DoubleMatrix2D) is undefined for the type Test" ,其中 Test 是类的名称。

我做错了什么?有任何想法吗?...

4

2 回答 2

1

您还可以为此使用la4j(Java 的线性代数):

  • 对于实际确定的系统m == n,您的情况:

    // The coefficient matrix 'a'
    Matrix a = new Basic2DMatrix(new double[][] {
      { 1.0, 2.0, 3.0 },
      { 4.0, 5.0, 6.0 },
      { 7.0, 8.0. 9.0 }
    });
    
    // A right hand side vector, which is simple dense vector
    Vector b = new BasicVector(new double[] { 1.0, 2.0, 3.0 });
    
    // We will use standard Forward-Back Substitution method,
    // which is based on LU decomposition and can be used with square systems
    LinearSystemSolver solver = 
       a.withSolver(LinearAlgebra.FORWARD_BACK_SUBSTITUTION);
    
    Vector x = solver.solve(b, LinearAlgebra.DENSE_FACTORY);
    
  • 对于超定系统m > nLinearAlgebra.LEAST_SQUARES可以使用求解器。

所有示例均取自官方网站: http: //la4j.org

于 2013-10-15T11:10:43.263 回答
1

您收到此错误的原因是因为方法solve()是非静态的,无法从main().

这应该可以解决您的问题:

Algebra algebra = new Algebra();
DoubleMatrix2D X = algebra.solve(matrix, D);
于 2013-10-14T10:51:13.820 回答