function [V,T] = lansym(A,v1,k) %LANSYM Symmetric Lanczos algorithm % [V,T] = lansym(A,v1,k) computes a k x k symmteric tridiagonal matrix %T and an orthonormal matrix V using the Lanczos %algorithm. Matrix A is symmetric and v1 is a unit vector. %Integer k is the number of basis vectors v1,v2, ... %to be computed. This program implements Algorithm 12.4 %of the book. %Input : Matrix A, vector v1 and integer k %Output : matrices V and T % Vadim Sokolov 09/04/2009 [m,n] = size(A); v_prev = zeros(m,1); beta(1) = 0; v=v1/norm(v1); for j = 1:k V(:,j) = v; v_hat = A*v-beta(j)*v_prev; alpha(j) = v.'*v_hat; v_hat = v_hat-alpha(j)*v; beta(j+1) = norm(v_hat); if beta(j+1) < eps return end v_prev = v; v = v_hat/beta(j+1); end; beta = beta(2:k); T = diag(alpha(1:k))+ diag(beta(1:k-1),1)+diag(beta(1:k-1),-1);