3

I am using ojalgo to solve a (NxN) Ax = b System in java. Since there are chances that A will be a singular matrix, I would like my code to know this somehow. Is there a way? (documentation states that the solve() method returns one possible solution if the problem is under-qualified and the invert() method does not throw an exception).

Any help would be greatly appreciated. Thanks in advance.

apete
  • 1,250
  • 1
  • 10
  • 16
Nick Simos
  • 67
  • 4

2 Answers2

2

Sounds like you're using the solve- and invert-methods on a BasicMatrix directly.

Switch to using an LU-decompostion instead. After you have decomposed the matrix, but before attempting the solve, you can call:

lu.isSquareAndNotSingular();

apete
  • 1,250
  • 1
  • 10
  • 16
  • 2
    Hi Apete, many thanks for your swift response. Could you please provide a simple code where you solve the Ax = b problem with LU-Decomposition? Thank you very much! – Nick Simos May 22 '16 at 10:18
0

To solve using LU-Decomposition try something like this.

        MatrixStore M = ...equations
        MatrixStore v = ...rhs
        LU<Double> lu = LU.PRIMITIVE.make(M);
        boolean decompose = lu.decompose(M);
        MatrixStore solution;
        if(lu.isSolvable()){
            solution = lu.getSolution(v);
        // and/or check that solution is good enough
            double norm = M.multiply(solution).subtract(v).norm();
        }
Magnus
  • 35
  • 5