%% Effect of Preordering on Sparsity of Cholesky Factors
% Compute the Cholesky factor of a matrix before and after it is ordered using amd to examine the effect on sparsity.
% Load the barbell graph sparse matrix and add a sparse identity matrix to it to ensure it is positive definite. Compute two Cholesky factors: one of the original matrix and one of the original matrix preordered with amd.

load barbellgraph.mat
A = A+speye(size(A)); 
p = amd(A);
L = chol(A,'lower');
Lp = chol(A(p,p),'lower');

%
% Plot the sparsity patterns of all four matrices. The Cholesky factor obtained from the preordered matrix is much sparser compared to the factor of the matrix in its natural ordering.

figure
subplot(2,2,1)
spy(A)
title('Original Matrix A')
subplot(2,2,2) 
spy(A(p,p))
title('AMD ordered A')
subplot(2,2,3) 
spy(L)
title('Cholesky factor of A')
subplot(2,2,4) 
spy(Lp)
title('Cholesky factor of AMD ordered A')

% Copyright 2012 The MathWorks, Inc.