我想使用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);
但是我收到了错误 “方法解决(DoubleMatrix2D,DoubleMatrix2D)未定义为类型Test”, 其中Test是该类的名称。
我做错了什么? 任何想法?...
答案 0 :(得分:1)
您收到此错误的原因是因为方法solve()
是非静态的,无法从main()
访问。
这可以解决您的问题:
Algebra algebra = new Algebra();
DoubleMatrix2D X = algebra.solve(matrix, D);
答案 1 :(得分:1)
您也可以使用la4j(线性代数for 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 > n
,可以使用LinearAlgebra.LEAST_SQUARES
求解器。
所有示例均来自官方网站:http://la4j.org