Simple Lanczos algorithm code to obtain eigenvalues and eigenvectors of a symmetric matrix

12.4k views Asked by At

I would like to write a simple program (in C) using Lanczos algorithm. I came across a Matlab example which helped me to understand a bit further the algorithm, however from this piece of code I can't find the way of getting the eigenvalues and eigenvectors. I can follow the algorithm but I think I must be missing something. Can someone guided to get the eigenvalues from this example so I can understand the method and then code it in C?

% Create a random symmetric matrix 
D=6
for i=1:D,
    for j=1:i,
        A(i,j)=rand; 
        A(j,i)=A(i,j);
    end 
end

% Iteration with j=0 
r0 = rand(D,1); 
b0 = sqrt(r0'*r0); 
q1 = r0/b0; 
a1 = q1'*A*q1

%Iteration with j=1
r1 = A*q1 - a1*q1
b1 = sqrt(r1'*r1)
q2 = r1/b1;
a2 = q2'*A*q2

%Iteration with j=2
r2 = A*q2 - a2*q2 - b1*q1;
b2 = sqrt(r2'*r2)
q3 = r2/b2
a3 = q3'*A*q3

% Create Matrix Q
Q = [q1 q2 q3];

%Check orthogonality
EYE = Q'*Q
T = Q'*A*Q
1

There are 1 answers

0
Meriadoc Brandybuck On

In initial Lanczos method firstly you are to count the biggest eigenvalue of matrix A. After that you count the eigenvector which corresponds to this eigenvalue. Having counted this two objects you can decrease dimension of matrix which you are using on one and then find the maximum eigenvalue of new matrix. And you are to iterate through this m times where m is the dimension of initial matrix A.

But if you want to count all the eigenvalues simultaneously, you are to use Paige iterative procedure (see in middle) in which firstly you build tridiogonal matrix. Then you count it's eigenvalues using one of the well-known and fast algorithms as such matrix is very sparse and by formulas specified in article above you can easily count eigenvalues of initial matrix and their corresponding eigenvectors.