function main_matrix_norms

DRIVER_unit_balls
%DRIVER_rand

return

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function DRIVER_unit_balls

%A = [1,1;0,1];
A = randn(2);

%%% L2-norm
tt = linspace(0,2*pi,100);
xx = [cos(tt);sin(tt)];
yy = A*xx;
nn = sqrt(sum(yy.^2));
[normA,i] = max(nn);

figure(1)
subplot(1,2,1)
plot(xx(1,:),xx(2,:),'r',...
     [0,xx(1,i)],[0,xx(2,i)],'b',...
     'LineWidth',2)
axis equal
subplot(1,2,2)
plot(yy(1,:),yy(2,:),'r',...
     [0,yy(1,i)],[0,yy(2,i)],'b',...
     'LineWidth',2)
axis equal
fprintf(1,'Estimated L2 norm(A) = %0.13f\n',normA)
fprintf(1,'Exact     L2 norm(A) = %0.13f\n',norm(A,2))
keyboard

%%% L1-norm
xx = [linspace(1,0),linspace(0,-1),linspace(-1,0),linspace(0,1);
      linspace(0,1),linspace(1,0),linspace(0,-1),linspace(-1,0)];
yy = A*xx;
nn = sum(abs(yy));
[normA,i] = max(nn);

figure(1)
subplot(1,2,1)
plot(xx(1,:),xx(2,:),'r',...
     [0,xx(1,i)],[0,xx(2,i)],'b','LineWidth',2)
axis equal
subplot(1,2,2)
plot(yy(1,:),yy(2,:),'r',...
     [0,yy(1,i)],[0,yy(2,i)],'b','LineWidth',2)
axis equal
fprintf(1,'Estimated L1 norm(A) = %0.13f\n',normA)
fprintf(1,'Exact     L1 norm(A) = %0.13f\n',norm(A,1))
keyboard

%%% Linfty-norm
xx = [ones(1,100),linspace(1,-1),-ones(1,100),linspace(-1,1);...
      linspace(-1,1),ones(1,100),linspace(1,-1),-ones(1,100)];
yy = A*xx;
nn = max(abs(yy));
[normA,i] = max(nn);

figure(1)
subplot(1,2,1)
plot(xx(1,:),xx(2,:),'r',...
     [0,xx(1,i)],[0,xx(2,i)],'b','LineWidth',2)
axis equal
subplot(1,2,2)
plot(yy(1,:),yy(2,:),'r',...
     [0,yy(1,i)],[0,yy(2,i)],'b','LineWidth',2)
axis equal
fprintf(1,'Estimated L2 norm(A) = %0.13f\n',normA)
fprintf(1,'Exact     L2 norm(A) = %0.13f\n',norm(A,Inf))
keyboard

return

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function DRIVER_rand

rng(0)
nexp = 1000;
n    = 10;
A    = randn(n);
fprintf(1,'norm(A) = %0.5f   norm(A,1) = %0.5f   norm(A,Inf) = %0.5f\n',...
        norm(A),norm(A,1),norm(A,Inf))
    
nn1 = zeros(1,nexp);
nn2 = zeros(1,nexp);
nn3 = zeros(1,nexp);

for i = 1:nexp
  x      = randn(n,1);
  nn1(i) = norm(A*x,1)/norm(x,1);
  nn2(i) = norm(A*x,2)/norm(x,2);
  nn3(i) = norm(A*x,Inf)/norm(x,Inf);
  fprintf(1,'       %0.5f       %0.5f       %0.5f\n',...
          nn1(i),nn2(i),nn3(i))
end

figure(1)
subplot(1,1,1)
plot(1:nexp,sort(nn1),'b',...
     [1,nexp],norm(A,1)*[1,1],'b--',...
     1:nexp,sort(nn2),'r',...
     [1,nexp],norm(A,2)*[1,1],'r--',...
     1:nexp,sort(nn3),'g',...
     [1,nexp],norm(A,Inf)*[1,1],'g--',...
     'LineWidth',3)
keyboard

return