%% Eigenvalues of Permuted Cholesky Factor
% Create sparse random matrices A and B that both have low densities of nonzero elements.

B = sprandn(1e3,1e3,0.001) + speye(1e3); 
B = B'*B; 
A = sprandn(1e3,1e3,0.005); 
A = A+A';

%
% Find the Cholesky decomposition of matrix B, using three outputs to return the permutation vector s and test value p.

[R,p,s] = chol(B,'vector');
p

%
% Since p is zero, B is a symmetric positive definite matrix that satisfies B(s,s) = R'*R.
% Calculate the six largest magnitude eigenvalues and eigenvectors of the generalized eigenvalue problem involving A and R. Since R is the Cholesky factor of B, specify 'IsCholesky' as true. Furthermore, since B(s,s) = R'*R and thus R = chol(B(s,s)), use the permutation vector s as the value of 'CholeskyPermutation'.

[V,D,flag] = eigs(A,R,6,'largestabs','IsCholesky',true,'CholeskyPermutation',s);
flag

% Since flag is zero, all of the eigenvalues converged.
% Copyright 2012 The MathWorks, Inc.