Skip to content

Commit

Permalink
First_Version
Browse files Browse the repository at this point in the history
Lanczos Algorithm
  • Loading branch information
jishnurajendran committed Feb 18, 2018
1 parent 3afa7d4 commit ae83d0c
Show file tree
Hide file tree
Showing 8 changed files with 132,362 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Lanczos.asv
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function [T] = Lanczos(A,n);
%Lancozs Algorithm to find Eigenvalue spectra
%Inputs :A hermitian matrix of the order n
% :n order of the matrix

T = zeros(n);

v = rand(n,1); %Random vector
v1 = v/norm(v); %

t = A*v1;
alpha = v1'*t;
t = t - v1*alpha;

T(1,1) = alpha;

for j = 2:n,

beta = norm(t);
v0 = v1; v1 = t/beta;

t = A*v1 - v0*beta;
alpha = v1'*t;
t = t - v1*alpha;

T(j,j-1) = beta; T(j-1,j) = beta; T(j,j) = alpha;


end
T
eig(T)
end
49 changes: 49 additions & 0 deletions Lanczos.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
%Lancozs Algorithm to find Eigenvalue spectra
%Inputs :A matrix of the order n
% :n order of the matrix
%Author:Jishnu Rajendran
%As a part of semester project
%


function [T,V,u] = Lanczos(A,n)

T = zeros(n); %Initailising the T matrix with all
%All entries 0
V = zeros(n,n);
% if abs(norm(u)-1)<0.001
% w1=u;
%else
w1 = rand(n,1); %Generating Random vector of norm 1
w1 = w1/norm(w1); %
u=w1;


t = A*w1;
alpha = w1'*t;
t = t - w1*alpha;

V(:,1) = w1;
T(1,1) = alpha;

for j = 2:n,

beta = norm(t);
w0 = w1; w1 = t/beta;

t = A*w1 - w0*beta;
alpha = w1'*t;
t = t - w1*alpha;

T(j,j-1) = beta;
T(j-1,j) = beta;
T(j,j) = alpha;
V(:,j) = w1;
[X,~]=eig(T);
if(abs(X(j,n))<0.000001)
break
end

end

end
15 changes: 15 additions & 0 deletions inp.asv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
%A=load('input.txt');
%A=[2 0 1;
% 0 3 0;
% 1 0 2;];
%A = rand(5,5);
%A=rherm(5)
A
eig(A)
[n,n]=size(A);

Lanczos(A,n);




26 changes: 26 additions & 0 deletions inp.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
sHH=load('sparse_ham_sz_2.txt');
%H = diag(ones(20, 1),0) + 0.5*diag(ones(19,1), 1) + 0.5*diag(ones(19,1), -1);
%H(20,1)=1;H(1,20)=1;
%D=[2 0 1; 0 3 0; 1 0 2;];
%u =[0.171799151874028; 0.751483050929023; 0.636991582033681;];

A=spconvert(H);
%save('A_mat.txt','A');
issparse(A)
g=eig(A);
disp(g);
[~,n]=size(A);
[S,D]=eigs(A); %S is the matrix with the eigenvectors as column
S;
[T,V,u]=Lanczos(A,n);
T;
V;
k=eig(T);
disp(k);
[X,Z]=eig(T); % X is the matrix with the eigenvectors as column
X;
B=eig(T)-eig(A);
disp(B);
disp([g,k]);
disp(u);

Empty file added input.txt
Empty file.
8 changes: 8 additions & 0 deletions rherm.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function [ A ] = rherm(n)
% Generate a hermitian matrix with random values.
% rherm(n) returns a random hermitian matrix of size n*n
if exist('n')~=1, n=2; end
A = [zeros(1,0),(2*rand(1,1)-1),(2*rand(1,n-1)-1)+(2*rand(1,n-1)-1)*1i;zeros(n-1,n)];
for i=1:n-1, A = A + [zeros(n-i,n);zeros(1,n-i),(2*rand(1,1)-1),(2*rand(1,i-1)-1)+(2*rand(1,i-1)-1)*1i;zeros(i-1,n)];end
for i=1:n-1, for j=1:n-i, A = A + [zeros(n-i,n);zeros(1,j-1),conj(A(j,n-i+1)),zeros(1,n-j);zeros(i-1,n)];end;end
end
132,132 changes: 132,132 additions & 0 deletions sparse_ham_sz_2.txt

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions test_mat.txt

Large diffs are not rendered by default.

0 comments on commit ae83d0c

Please sign in to comment.