Example: LU Factorization of a Matrix

The LU Factorization of a Matrix is performed. A linear system is then solved using the factorization. The inverse, determinant, and condition number 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);
        
        // 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);
        
        // Find the condition number of A.
        double	condition = lu.condition(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.