Example: LU Factorization of a Matrix

The LU Factorization of a Matrix is performed. The reciprocal of the condition number of the Matrix is then computed and checked against machine precision to determine whether or not to issue a Warning about the results. A linear system is then solved using the factorization. The inverse and determinant of the input matrix are also computed.

import com.imsl.math.*;

public class LUEx1 {

    public static void main(String args[]) throws SingularMatrixException {
        double a[][] = {
            {1, 3, 3},
            {1, 3, 4},
            {1, 4, 3}
        };
        double b[] = {12, 13, 14};

        // Compute the LU factorization of A
        LU lu = new LU(a);

        // Check the reciprocal of the condition number of
        // A against machine precision
        double condition = lu.condition(a);
        if (condition <= 2.220446049250313e-16) {
            System.out.println("WARNING. The matrix is too ill-conditioned.");
            System.out.println("An estimate of the reciprocal of its L1 "
                    + "condition number is " + condition + ".");
            System.out.println("Results based on this factorization "
                    + "may not be accurate.");
        }

        // Solve Ax = b
        double x[] = lu.solve(b);
        new PrintMatrix("x").print(x);

        // Find the inverse of A.
        double ainv[][] = lu.inverse();
        new PrintMatrix("ainv").print(ainv);

        // Print the condition number of A.
        System.out.println("condition number = " + condition);
        System.out.println();

        // Find the determinant of A.
        double determinant = lu.determinant();
        System.out.println("determinant = " + determinant);
    }
}

Output

  x
   0  
0  3  
1  2  
2  1  

     ainv
   0   1   2   
0   7  -3  -3  
1  -1   0   1  
2  -1   1   0  

condition number = 0.015120274914089344

determinant = -0.9999999999999998
Link to Java source.