Example: Sparse Cholesky Factorization

The Cholesky Factorization of a sparse symmetric positive definite matrix is used to solve a linear system. Some additional information about the Cholesky factorization is also computed.
import com.imsl.math.*;
import com.imsl.IMSLException;

public class  SparseCholeskyEx1  {
    public static void main(String args[]) throws IMSLException {
        
        SparseMatrix A = new SparseMatrix(5, 5);
        A.set(0, 0, 10.0);
        A.set(1, 1, 20.0);
        A.set(2, 0,  1.0);
        A.set(2, 2, 30.0);
        A.set(3, 2,  4.0);
        A.set(3, 3, 40.0);
        A.set(4, 0,  2.0);
        A.set(4, 1,  3.0);
        A.set(4, 3,  5.0);
        A.set(4, 4, 50.0);

        double b[] = {55.0, 83.0, 103.0, 97.0, 82.0};

        SparseCholesky cholesky = new SparseCholesky(A);
        
        // Choose Multifrontal method as numeric factorization method
        cholesky.setNumericFactorizationMethod(SparseCholesky.MULTIFRONTAL_METHOD);
        
        // Compute solution
        double solution[] = cholesky.solve(b);
        new PrintMatrix("Computed Solution").print(solution);
        
        // Print additional information about the factorization        
        System.out.println("Smallest diagonal element of Cholesky factor: " +
                cholesky.getSmallestDiagonalElement());
        System.out.println("Largest diagonal element of Cholesky factor: " + 
                cholesky.getLargestDiagonalElement());
        System.out.println("Number of nonzeros in Cholesky factor: " + 
                cholesky.getNumberOfNonzeros());
    }
}

Output

Computed Solution
   0  
0  5  
1  4  
2  3  
3  2  
4  1  

Smallest diagonal element of Cholesky factor: 3.1622776601683795
Largest diagonal element of Cholesky factor: 7.010706098532443
Number of nonzeros in Cholesky factor: 11
Link to Java source.