% here the singular values used for comparison are the ones found from the
% built-in function
clc;
clear all;
close all;

% n fixed and m varied

% matrix dimensions
n=100:100:1500;

% intializing variables
t1=zeros(1,length(n));
t2=t1;
t3=t1;
t4=t1;
t5=t1;
t6=t1;
e1=t1;
e2=t1;
e3=t1;
e4=t1;
e5=t1;
e6=t1;
orth1=t1;
orth2=t1;
orth3=t1;
orth4=t1;
orth5=t1;
orth6=t1;
orth7=t1;
orth8=t1;
orth9=t1;
orth10=t1;
orth11=t1;
orth12=t1;
esval1=t1;
esval2=t1;
esval3=t1;
esval4=t1;
esval5=t1;

for i=1:length(n)
    
    % Matrix generation
    A=randn(n(i));
    A=A+A';
    
    % SVD decompositions
    
    % built in
    tic
    [u1,d1,v1]=svd(A);
    t1(i) = toc;
    [x,y]=size(d1);
    
    % SVDBiBuilt
    tic
    [u2,d2,v2]=SVDBiBuilt(A);
    t2(i) = toc;
    
    % SVDComb
    tic
    [u3,d3,v3]=SVDComb(A);
    t3(i) = toc;
    
    % SVDecom
    tic
    [u4,d4,v4]=SVDecom(A);
    t4(i) = toc;
    
    % SVDIndirect
    tic
    [u5,d5,v5]=SVDIndirect(A);
    t5(i) = toc;
    
    % SVDFrancis
    tic
    [d6]=SVD(A);
    t6(i) = toc;
    
    % errors in decomposition
    e1(i)=norm(u1*d1*v1'-A,inf);
    e2(i)=norm(u2*d2*v2'-A,inf);
    e3(i)=norm(u3*d3*v3'-A,inf);
    e4(i)=norm(u4*d4*v4'-A,inf);
    e5(i)=norm(u5*d5*v5'-A,inf);
%     e6(i)=norm(u6*d61*v6'-A,inf);
    
    % orthogonality checks
    orth1(i)=norm(u1*u1'-eye(n(i)),inf);
    orth2(i)=norm(v1*v1'-eye(n(i)),1);
    orth3(i)=norm(u2*u2'-eye(n(i)),inf);
    orth4(i)=norm(v2*v2'-eye(n(i)),1);
    orth5(i)=norm(u3*u3'-eye(n(i)),inf);
    orth6(i)=norm(v3*v3'-eye(n(i)),1);
    orth7(i)=norm(u4*u4'-eye(n(i)),inf);
    orth8(i)=norm(v4*v4'-eye(n(i)),1);
    orth9(i)=norm(u5*u5'-eye(n(i)),inf);
    orth10(i)=norm(v5*v5'-eye(n(i)),1);
    orth11(i)=norm(u6*u6'-eye(n(i)),inf);
    orth12(i)=norm(v6*v6'-eye(n(i)),1);
    
    % errors is singular values
    esval1(i)=norm(d2-d1, inf);
    esval2(i)=norm(d3-d1, inf);
    esval3(i)=norm(d4-d1, inf);
    esval4(i)=norm(d5-d1, inf);
    esval5(i)=norm(d6-d1, inf);
    
end

save('t1.mat','t1');
save('t2.mat','t2');
save('t3.mat','t3');
save('t4.mat','t4');
save('t5.mat','t5');
save('t6.mat','t6');
save('e1.mat','e1');
save('e2.mat','e2');
save('e3.mat','e3');
save('e4.mat','e4');
save('e5.mat','e5');
save('e6.mat','e6');
save('orth1.mat','orth1');
save('orth2.mat','orth2');
save('orth3.mat','orth3');
save('orth4.mat','orth4');
save('orth5.mat','orth5');
save('orth6.mat','orth6');
save('orth7.mat','orth7');
save('orth8.mat','orth8');
save('orth9.mat','orth9');
save('orth10.mat','orth10');
save('orth11.mat','orth11');
save('orth12.mat','orth12');
save('esval1.mat','esval1');
save('esval2.mat','esval2');
save('esval3.mat','esval3');
save('esval4.mat','esval4');
save('esval5.mat','esval5');