% CS 231A
% Problem Set 1
% Problem 1

% load data
readYaleFaces;

% part d -- find the mean
mean_image = mean(A,2);

% subtract the mean image from all the images
B = zeros(size(A,1),size(A,2));
for i = 1:size(A,2)
    B(:,i) = A(:,i) - mean_image;
end

% % form a smaller matrix to take the eigenvalues of
% B_sm = B'*B;
% [V_sm,D_sm] = eig(B_sm);
% % arrange the singular values in decreasing order
% D = zeros(size(D_sm,1),size(D_sm,2));
% V = zeros(size(V_sm,1),size(V_sm,2)); 
% for i = 1:size(D_sm,1)
%     V(:,size(V_sm,1)+1-i) = V_sm(:,i);
%     D(size(D_sm,1)+1-i,size(D_sm,1)+1-i) = sqrt(D_sm(i,i));
% end
% % the matrix B is not full rank, thus we need to make 
% % the matrices for the
% % svd thin
% D_hat = D;
% V_hat = V;
% find the matrix U of the left singular vectors 
%to be used for eigenfaces
%U_hat = B*pinv(D_hat*V_hat');

[U,S,V] = svd(B,0);

% report first 5 singular values and the coressponding 5
fprintf('First 5 Singular values are along the diagonal')
S(1:5,1:5)


% part e -- determine accuracy and plot projections
clear e
e = zeros(size(image1,1)*size(image1,2),1);
x_proj = zeros(size(image1,1)*size(image1,2),1);

for i = 1:20
    clear x
    testFileName = ['image' num2str(i)];
    x = eval(testFileName);
    x = double(reshape(x,size(x,1)*size(x,2),1));
    
    r = 25;
    V_r = U(:,1:r);
    alpha_opt = pinv(V_r)*(x-mean_image);
    x_proj(:,i) = mean_image + V_r*alpha_opt;
    e(:,i) = x - x_proj(:,i);
    norm_e(i) = e(:,i)'*e(:,i);
end

numGndTrth = 20;
gnd_truth = ones(numGndTrth,1);
gnd_truth(1) = 0;
gnd_truth(3) = 0;
gnd_truth(6) = 0;
gnd_truth(12) = 0;
gnd_truth(14) = 0;

thres = 10^8*[.009 .09 .9 9];
num_correct = 0;
for i = 1:length(thres)
    for j = 1:length(gnd_truth)
        if ( norm_e(j) < thres(i) && gnd_truth(j) == 1)
            num_correct = num_correct + 1;
        elseif (norm_e(j) > thres(i) && gnd_truth(j) == 0)
            num_correct = num_correct + 1;
        end
    end
    num_correct
    num_correct = 0;
end

