function ShowCholInc()
% Illustrates cholinc with various drop tolerances
close all

% Generate and display a sparse symmetric positive definite matrix...
A = delsq(numgrid('C',35));
[n,n] = size(A);
% and its Cholesky factor...
G = chol(A)';
% Apply incomplete Cholesky with zero fill-in...
G0 = cholinc(A,'0')';
% Apply incomplete Cholesky with 3 different positive drop tolerances.
% Roughly speaking, for i>j the algorithm uses the usual recipe for G(i,j).
% If this is less than dropTol*norm(A(:,j)), then G(i,j)
% is set to zero instead.
% Smaller drop tolerances lead to more nonzeros.
dropTol1 = .01;
G1 = cholinc(A,dropTol1)';
dropTol2 = .005;
G2 = cholinc(A,dropTol2)';
dropTol3 = .001;
G3 = cholinc(A,dropTol3)';

% Let's look at the results...
subplot(2,3,1)
spy(A)
title(sprintf('A n = %1d',n),'fontsize',14)
set(get(gca,'xlabel'),'fontsize',12)
subplot(2,3,4)
spy(G)
title('G = chol(A)','fontsize',14)
set(get(gca,'xlabel'),'fontsize',12)

subplot(2,3,2)
spy(G0)
title(sprintf('G = cholinc(A,''0'')'),'fontsize',14)
set(get(gca,'xlabel'),'fontsize',12)
subplot(2,3,3)
spy(G1)
title(sprintf('G = cholinc(A,%6.3f)',dropTol1),'fontsize',14)
set(get(gca,'xlabel'),'fontsize',12)
subplot(2,3,5)
spy(G2)
title(sprintf('G = cholinc(A,%6.3f)',dropTol2),'fontsize',14)
set(get(gca,'xlabel'),'fontsize',12)
subplot(2,3,6)
spy(G3)
title(sprintf('G = cholinc(A,%6.3f)',dropTol3),'fontsize',14)
set(get(gca,'xlabel'),'fontsize',12)

% Check out the G factor as a preconditioner..
clc
fprintf(' cond(A) = %6.3f\n\n',cond(full(A)))
fprintf('cond(inv(G0)Ainv(G0)'' = %6.3f dropTol = ''0''\n\n',cond(full(G0\(G0\A)')))
fprintf('cond(inv(G1)Ainv(G1)'' = %6.3f dropTol = %6.3f\n\n',cond(full(G1\(G1\A)')),dropTol1)
fprintf('cond(inv(G2)Ainv(G2)'' = %6.3f dropTol = %6.3f\n\n',cond(full(G2\(G2\A)')),dropTol2)
fprintf('cond(inv(G3)Ainv(G3)'' = %6.3f dropTol = %6.3f\n\n',cond(full(G3\(G3\A)')),dropTol3)