% InfoRetrievalSVD.m
% Low-ranking approximation using SVD.
% Example from M. Berry, Z. Drmac and E. Jessup
% "Matrices, Vector Spaces, and Information Retrieval"
% SIAM Review, (41)#2, 335-362 (1999).
% K. Ming Leung, 10/06/03

clear all;
A0 = [1 0 0 1 0;
      1 0 1 1 1;
      1 0 0 1 0;
      0 0 0 1 0;
      0 1 0 1 1;
      0 0 0 1 0];
% 1st query vector and normalize it
Query1 = [1; 0; 1; 0; 0; 0];   
% 2nd query vector and normalize it
Query2 = [1; 0; 0; 0; 0; 0];  

% A0 = [ 0 1 0 1 1 0 1
% 0 1 1 0 0 0 0
% 0 0 0 0 0 1 1
% 0 0 0 1 0 0 0
% 0 1 1 0 0 0 0
% 1 0 0 1 0 0 0
% 0 0 0 0 1 1 0
% 0 0 1 1 0 0 0
% 1 0 0 1 0 0 0 
% 0 1 0 1 0 0 1];
% %1st query vector and normalize it
% Query1 = [1; 0; 1; 0; 0; 0; 0; 0; 0; 0 ];   
% %2nd query vector and normalize it
% Query2 = [1; 0; 0; 0; 0; 0; 0; 0; 0; 0 ];   

[t,d] = size(A0); A0norm = sqrt(sum(A0.*A0));
A = A0.*repmat(1./A0norm,t,1); % Column normalized A

Query1n = Query1/norm(Query1); % Normalized query vector 1
Query2n = Query2/norm(Query2); % Normalized query vector 2

% SVD factorization of A
[U,S,V] = svd(A), 
Sdiag = diag(S); % diagonal part of S
% Find the rank rk of matrix A
f=find(abs(Sdiag)>eps); 
rk = f(end), % rank of matrix A
% compute vectors s and normalized them
SJ = S*V.'; Snorm = sqrt(sum(SJ.*SJ)); % colummn norm
SJnormalized = SJ.*repmat(1./Snorm,t,1); % column normalized
% compute the correlation consines
CosineJ1 = Query1n.'*U*SJnormalized, % cosine for query 1
CosineJ2 = Query2n.'*U*SJnormalized, % cosine for query 2

% Successive lower rank approximation ntimes
ntimes = 3;
for n=1:ntimes
  k = rk-n; 
  disp(['====================================================']);
  disp(['Rank ' num2str(k) ' approximation - Correlation consines:']);
  UK = U(:,1:k); VK = V(:,1:k); SK = diag(Sdiag(1:k));
  % compute vectors s and normalized them
  SJK = SK*VK.'; 
  SJKnorm = sqrt(sum(SJK.*SJK));
  SJK_NORMALIZED = SJK.*repmat(1./SJKnorm,k,1);
  % compute the correlation consines for queries 1 and 2:
  CosineJk1 = Query1n.'*UK*SJK_NORMALIZED,
  CosineJk2 = Query2n.'*UK*SJK_NORMALIZED,
end

