Pregunta

I want to solve the linear equation matrix*X=D using Colt library. I tried :

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);

but I get an error
"The method solve(DoubleMatrix2D, DoubleMatrix2D) is undefined for the type Test" , where Test is the name of the class.

What have I done wrong? Any ideas?...

¿Fue útil?

Solución

The reason why you are getting this error is because method solve() is non-static and can't be accessed from main().

This should solve your problem:

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

Otros consejos

You also can use la4j (Linear Algebra for Java) for this:

  • For determined systems m == n which is actually, your case:

    // 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);
    
  • For overdetermined systems m > n the LinearAlgebra.LEAST_SQUARES solver can be used.

All the examples are taken from official web-site: http://la4j.org

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top