Example: Sparse Cholesky Factorization

The Cholesky Factorization of a sparse symmetric positive definite matrix is computed. Some additional information about the Cholesky factorization is also computed.
using System;
using Imsl.Math;

public class SparseCholeskyEx1
{
    public static void Main(String[] args)
    {
        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.NumericFactorizationMethod =
         SparseCholesky.NumericFactorization.MultiFrontalMethod;
        
        // Compute solution
        double[] solution = cholesky.Solve(b);
        new PrintMatrix("Computed Solution").Print(solution);
        
        // Print additional information about the factorization        
        Console.Out.Write("Smallest diagonal element of Cholesky factor: ");
        Console.Out.WriteLine(cholesky.SmallestDiagonalElement);
        Console.Out.Write("Largest diagonal element of Cholesky factor: ");
        Console.Out.WriteLine(cholesky.LargestDiagonalElement);
        Console.Out.Write("Number of nonzeros in Cholesky factor: ");
        Console.Out.WriteLine(cholesky.NumberOfNonzeros);
    }
}

Output

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

Smallest diagonal element of Cholesky factor: 3.16227766016838
Largest diagonal element of Cholesky factor: 7.01070609853244
Number of nonzeros in Cholesky factor: 11

Link to C# source.