% dense rectangular matrices with predefined eigen values

clc;
clear all;
close all;

% n fixed and m varied

% matrix dimensions
n=[100 200];
m=100:100:1500;

% intializing variables
t1=zeros(length(n),length(m));
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;
orth1_1=t1;
orth2_1=t1;
orth3_1=t1;
orth4_1=t1;
orth5_1=t1;
orth6_1=t1;
orth7_1=t1;
orth8_1=t1;
orth9_1=t1;
orth10_1=t1;
% orth11_1=t1;
% orth12_1=t1;
esval1=t1;
esval2=t1;
esval3=t1;
esval4=t1;
esval5=t1;
esval6=t1;

for i=1:length(n)
    
    for j=1:length(m)
        
        %         singular value generation
        a=randn(min(m(j),n(i)),1);
        a=abs(a);
        a=sort(a,'descend');
        
        % matrix generation
        [a,u,s,v]=matrixgen(n(i),m(j),a);
        
        % svd decompositions
        
        % built in
        tic
        [u1,d1,v1]=svd(a);
        t1(i,j) = toc;
        [x,y]=size(d1);
        
        % comb. built in
        tic
        [u2,d2,v2]=svdbibuilt(a);
        t2(i,j) = toc;
        
        % comb. square
        tic
        [u3,d3,v3]=svdcomb(a);
        t3(i,j) = toc;
        
        % indirect method
        tic
        [u4,d4,v4]=svdecom(a);
        t4(i,j) = toc;
        
        % direct method
        tic
        [u5,d5,v5]=svdindirect(a);
        t5(i,j) = toc;
        
        % francis
        tic
        [d6]=SVD(a);
        t6(i,j) = toc;
        
        
        % errors in decomposition
        e1(i,j)=norm(u1*d1*v1'-a,inf);
        e2(i,j)=norm(u2*d2*v2'-a,inf);
        e3(i,j)=norm(u3*d3*v3'-a,inf);
        e4(i,j)=norm(u4*d4*v4'-a,inf);
        e5(i,j)=norm(u5*d5*v5'-a,inf);
        %         e6(i,j)=norm(u6*d61*v6'-a,inf);
        
        % orthogonality checks
        orth1(i,j)=norm(u1*u1'-eye(n(i)),inf);
        orth2(i,j)=norm(v1*v1'-eye(m(j)),1);
        orth3(i,j)=norm(u2*u2'-eye(n(i)),inf);
        orth4(i,j)=norm(v2*v2'-eye(m(j)),1);
        orth5(i,j)=norm(u3*u3'-eye(n(i)),inf);
        orth6(i,j)=norm(v3*v3'-eye(m(j)),1);
        orth7(i,j)=norm(u4*u4'-eye(n(i)),inf);
        orth8(i,j)=norm(v4*v4'-eye(m(j)),1);
        orth9(i,j)=norm(u5*u5'-eye(n(i)),inf);
        orth10(i,j)=norm(v5*v5'-eye(m(j)),1);
        %         orth11(i,j)=norm(u6*u6'-eye(n(i)),inf);
        %         orth12(i,j)=norm(v6*v6'-eye(m(j)),1);
        orth1_1(i,j)=norm(u1'*u1-eye(n(i)),inf);
        orth2_1(i,j)=norm(v1'*v1-eye(m(j)),inf);
        orth3_1(i,j)=norm(u2'*u2-eye(n(i)),inf);
        orth4_1(i,j)=norm(v2'*v2-eye(m(j)),inf);
        orth5_1(i,j)=norm(u3'*u3-eye(n(i)),inf);
        orth6_1(i,j)=norm(v3'*v3-eye(m(j)),inf);
        orth7_1(i,j)=norm(u4'*u4-eye(n(i)),inf);
        orth8_1(i,j)=norm(v4'*v4-eye(m(j)),inf);
        orth9_1(i,j)=norm(u5'*u5-eye(n(i)),inf);
        orth10_1(i,j)=norm(v5'*v5-eye(m(j)),inf);
        %         orth11_1(i,j)=norm(u6'*u6-eye(n(i)),inf);
        %         orth12_1(i,j)=norm(v6'*v6-eye(m(j)),inf);
        
        % errors in singular values
        esval1(i,j)=norm(a-diag(d1), inf);
        esval2(i,j)=norm(a-diag(d2), inf);
        esval3(i,j)=norm(a-diag(d3), inf);
        esval4(i,j)=norm(a-diag(d4), inf);
        esval5(i,j)=norm(a-diag(d5), inf);
        esval6(i,j)=norm(a-(d6), inf);
        
    end
    
end

% m fixed and n varied

% matrix dimensions
m1=n;
n1=m;

% intializing variables
t7=zeros(length(m1),length(n1));
t8=t7;
t9=t7;
t10=t7;
t11=t7;
t12=t7;
e7=t7;
e8=t7;
e9=t7;
e10=t7;
e11=t7;
% e12=t7;
orth13=t7;
orth14=t7;
orth15=t7;
orth16=t7;
orth17=t7;
orth18=t7;
orth19=t7;
orth20=t7;
orth21=t7;
orth22=t7;
% orth23=t7;
% orth24=t7;
orth13_1=t7;
orth14_1=t7;
orth15_1=t7;
orth16_1=t7;
orth17_1=t7;
orth18_1=t7;
orth19_1=t7;
orth20_1=t7;
orth21_1=t7;
orth22_1=t7;
% orth23_1=t7;
% orth24_1=t7;
esval7=t7;
esval8=t7;
esval9=t7;
esval10=t7;
esval11=t7;
esval12=t7;

for i=1:length(m1)
    
    for j=1:length(n1)
        
        %         singular value generation
        a1=randn(min(m1(i),n1(j)),1);
        a1=abs(a1);
        a1=sort(a1,'descend');
        
        % matrix generation
        [a1,u1,s1,v1]=matrixgen(n1(j),m1(i),a1);
        % svd decompositions
        
        % built in
        tic
        [u7,d7,v7]=svd(a1);
        t7(i,j) = toc;
        [x1,y1]=size(d7);
        
        % svdbibuilt
        tic
        [u8,d8,v8]=svdbibuilt(a1);
        t8(i,j) = toc;
        
        % svdcomb
        tic
        [u9,d9,v9]=svdcomb(a1);
        t9(i,j) = toc;
        
        % svdecom
        tic
        [u10,d10,v10]=svdecom(a1);
        t10(i,j) = toc;
        
        % svdindirect
        tic
        [u11,d11,v11]=svdindirect(a1);
        t11(i,j) = toc;
        
        % svdfrancis
        tic
        [d12]=SVD(a1);
        t12(i,j) = toc;
        
        % errors in decompositions
        e7(i,j)=norm(u7*d7*v7'-a1,inf);
        e8(i,j)=norm(u8*d8*v8'-a1,inf);
        e9(i,j)=norm(u9*d9*v9'-a1,inf);
        e10(i,j)=norm(u10*d10*v10'-a1,inf);
        e11(i,j)=norm(u11*d11*v11'-a1,inf);
        %         e12(i,j)=norm(u12*d121*v12'-a1,inf);
        
        % orthogonality checks
        orth13(i,j)=norm(u7*u7'-eye(n1(j)),inf);
        orth14(i,j)=norm(v7*v7'-eye(m1(i)),1);
        orth15(i,j)=norm(u8*u8'-eye(n1(j)),inf);
        orth16(i,j)=norm(v8*v8'-eye(m1(i)),1);
        orth17(i,j)=norm(u9*u9'-eye(n1(j)),inf);
        orth18(i,j)=norm(v9*v9'-eye(m1(i)),1);
        orth19(i,j)=norm(u10*u10'-eye(n1(j)),inf);
        orth20(i,j)=norm(v10*v10'-eye(m1(i)),1);
        orth21(i,j)=norm(u11*u11'-eye(n1(j)),inf);
        orth22(i,j)=norm(v11*v11'-eye(m1(i)),1);
        %         orth23(i,j)=norm(u12*u12'-eye(n1(j)),inf);
        %         orth24(i,j)=norm(v12*v12'-eye(m1(i)),1);
        orth13_1(i,j)=norm(u7'*u7-eye(n1(j)),inf);
        orth14_1(i,j)=norm(v7'*v7-eye(m1(i)),inf);
        orth15_1(i,j)=norm(u8'*u8-eye(n1(j)),inf);
        orth16_1(i,j)=norm(v8'*v8-eye(m1(i)),inf);
        orth17_1(i,j)=norm(u9'*u9-eye(n1(j)),inf);
        orth18_1(i,j)=norm(v9'*v9-eye(m1(i)),inf);
        orth19_1(i,j)=norm(u10'*u10-eye(n1(j)),inf);
        orth20_1(i,j)=norm(v10'*v10-eye(m1(i)),inf);
        orth21_1(i,j)=norm(u11'*u11-eye(n1(j)),inf);
        orth22_1(i,j)=norm(v11'*v11-eye(m1(i)),inf);
        %         orth23_1(i,j)=norm(u12'*u12-eye(n1(j)),inf);
        %         orth24_1(i,j)=norm(v12'*v12-eye(m1(i)),inf);
        
        % errors is singular values
        esval7(i,j)=norm(a1-diag(d7), inf);
        esval8(i,j)=norm(a1-diag(d8), inf);
        esval9(i,j)=norm(a1-diag(d9), inf);
        esval10(i,j)=norm(a1-diag(d10), inf);
        esval11(i,j)=norm(a1-diag(d11), inf);
        esval12(i,j)=norm(a1-(d12), inf);
        
    end
    
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('orth1_1.mat','orth1_1');
save('orth2_1.mat','orth2_1');
save('orth3_1.mat','orth3_1');
save('orth4_1.mat','orth4_1');
save('orth5_1.mat','orth5_1');
save('orth6_1.mat','orth6_1');
save('orth7_1.mat','orth7_1');
save('orth8_1.mat','orth8_1');
save('orth9_1.mat','orth9_1');
save('orth10_1.mat','orth10_1');
% save('orth11_1.mat','orth11_1');
% save('orth12_1.mat','orth12_1');
save('esval1.mat','esval1');
save('esval2.mat','esval2');
save('esval3.mat','esval3');
save('esval4.mat','esval4');
save('esval5.mat','esval5');
save('esval6.mat','esval6');
save('t7.mat','t7');
save('t8.mat','t8');
save('t9.mat','t9');
save('t10.mat','t10');
save('t11.mat','t11');
save('t12.mat','t12');
save('e7.mat','e7');
save('e8.mat','e8');
save('e9.mat','e9');
save('e10.mat','e10');
save('e11.mat','e11');
% save('e12.mat','e12');
save('orth13.mat','orth13');
save('orth14.mat','orth14');
save('orth15.mat','orth15');
save('orth16.mat','orth16');
save('orth17.mat','orth17');
save('orth18.mat','orth18');
save('orth19.mat','orth19');
save('orth20.mat','orth20');
save('orth21.mat','orth21');
save('orth22.mat','orth22');
% save('orth23.mat','orth23');
% save('orth24.mat','orth24');
save('orth13_1.mat','orth13_1');
save('orth14_1.mat','orth14_1');
save('orth15_1.mat','orth15_1');
save('orth16_1.mat','orth16_1');
save('orth17_1.mat','orth17_1');
save('orth18_1.mat','orth18_1');
save('orth19_1.mat','orth19_1');
save('orth20_1.mat','orth20_1');
save('orth21_1.mat','orth21_1');
save('orth22_1.mat','orth22_1');
% save('orth23_1.mat','orth23_1');
% save('orth24_1.mat','orth24_1');
save('esval7.mat','esval7');
save('esval8.mat','esval8');
save('esval9.mat','esval9');
save('esval10.mat','esval10');
save('esval11.mat','esval11');
save('esval12.mat','esval12');