2013-10-14 30 views
0

我想使用柯爾特庫求解線性方程矩陣* X = D。 我想:如何在java中使用柯爾特庫求解線性方程組

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)是未定義的類型測試」, 其中測試是類的名稱。

我做錯了什麼? 有沒有想法?...

+1

能否請你加你如何聲明你'solve'方法? – qiGuar

+0

_solve_方法是庫爾特中的一種方法,它不是我的。我只想了解如何在我的程序中使用它... –

+0

您是否在課堂中導入了Colt庫? – qiGuar

回答

1

你得到這個錯誤的原因是因爲方法solve()是非靜態的,不能從main()訪問。

這應該解決您的問題:

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

不錯,但是當我運行它,我得到的錯誤:在線程「主要」的java.lang '例外? .IllegalArgumentException:''矩陣是singular.''在cern.colt.matrix.linalg.LUDecompositionQuick.solve(未知來源)'' 在cern.colt.matrix.linalg.LUDecomposition.solve(未知來源)'' 在cern.colt.matrix.linalg.Algebra.solve(Unknown Source)' \t'at test.Test.main(Test.java:89)' –

+0

好的,我解決了它! 感謝您的回答! :) –

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 > n可以使用LinearAlgebra.LEAST_SQUARES解算器。

所有的例子都是從官方網站採取:http://la4j.org