簡體   English   中英

如何用java中的colt庫求解線性方程組

[英]How to solve linear system of equations using colt library in java

我想用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是類的名稱。

我做錯了什么? 有任何想法嗎?...

您收到此錯誤的原因是因為方法solve()是非靜態的,無法從main()訪問。

這應該可以解決您的問題:

Algebra algebra = new Algebra();
DoubleMatrix2D X = algebra.solve(matrix, D);

你也可以使用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 > n ,可以使用LinearAlgebra.LEAST_SQUARES求解器。

所有示例均來自官方網站: http//la4j.org

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM