function [Y,Theta,Q,T]=Lanczos(A,nn); 
% Version fo clusstering
% Simple Lanczos algorithm
% Usage: [Y,Theta]=Lanczos(A);
[n,m]=size(A);
r=ones(n,1);
beta=norm(r);
% for j=1:round(4*sqrt(n));
for j=1:nn;
   q=r/beta;
   Q(:,j)=q;
   u=A*q;
   if j==1, 
      r=u;
   else
      r=u-Q(:,j-1)*T(j-1,j);
   end;
   T(j,j)=Q(:,j)'*r;
   r=r-Q(:,j)*T(j,j);
   beta=norm(r);
   T(j,j+1)=beta;
   T(j+1,j)=beta;
   [S,Theta]=eig(T(1:j,1:j));
   Y=Q*S;
   nres=norm(A*Y-Y*Theta);
   for i=1:j;
      nresi(i)=norm(A*Y(:,i)-Y(:,i)*Theta(i,i));
   end;
   nresi;
end;
