function svd_truncated_test ( )

%*****************************************************************************80
%
%% svd_truncated_test() tests the truncated SVD factorization.
%
%  Discussion:
%
%    The standard SVD factorization of an MxN matrix A is:
%      A = U*S*V'
%    where U is MxM, S (diagonal) is MxN, and V' is NxN.
%
%    In fact, if M is much greater than N, we can write
%      A = U$ * S * V'
%    where U$ is MxN, S is NxN, and V' is NxN
%
%    and if M is much smaller than N, we can write
%      A = U * S * V$'
%    where U is MxM, S is MxM, and V$' is MxN.
%
%    When there is a great discrepancy between M and N, these
%    alternatives can save time and space.
%
%    MATLAB calls this the 'economy' SVD.
%    Python calls this the 'full_matrices=False' option.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    02 April 2022
%
%  Author:
%
%    John Burkardt
%
  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'svd_truncated_test():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  Demonstrate the use of the truncated\n' );
  fprintf ( 1, '  Singular Value Decomposition (SVD) for cases where\n' );
  fprintf ( 1, '  the sizes of M and N are very different.\n' );

  m = 4;
  n = 3;
  svd_truncated_u_test ( m, n );

  m = 3;
  n = 4;
  svd_truncated_v_test ( m, n );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'svd_truncated_test():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  return
end
function svd_truncated_u_test ( m, n )

%*****************************************************************************80
%
%% svd_truncated_u_test() tests svd_truncated_u().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    19 March 2012
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'svd_truncated_u_test():\n' );
  fprintf ( 1, '  M = %d\n', m );
  fprintf ( 1, '  N = %d\n', n );

  a_save = rand ( m, n );
 
  a(1:m,1:n) = a_save(1:m,1:n);

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Original matrix A:\n' );
  disp ( a );

  [ un, sn, v ] = svd ( a, 'econ' );
%
%  Check the factorization by computing A = U * S * V'
%
  a = un * sn * v';

  err = max ( max ( abs ( a(1:m,1:n) - a_save(1:m,1:n) ) ) );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Maximum error |A - U*S*V''| = %g\n', err );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Recomputed A = U * S * V'':\n' );
  disp ( a );

  return
end
function svd_truncated_v_test ( m, n )

%*****************************************************************************80
%
%% svd_truncated_v_test() tests svd_truncated_v().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    19 March 2012
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'svd_truncated_v_test():\n' );
  fprintf ( 1, '  M = %d\n', m );
  fprintf ( 1, '  N = %d\n', n );

  a_save = rand ( m, n );

  a(1:m,1:n) = a_save(1:m,1:n);

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Original matrix A:\n' );
  disp ( a );

  [ u, sm, vm ] = svd ( a, 'econ' );
%
%  Check the factorization by computing A = U * S * V'
%
  a = u * sm * vm';

  err = max ( max ( abs ( a(1:m,1:n) - a_save(1:m,1:n) ) ) );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Maximum error |A - U*S*V''| = %g\n', err );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Recomputed A = U * S * V'':\n' );
  disp ( a );

  return
end
function timestamp ( )

%*****************************************************************************80
%
%% timestamp() prints the current YMDHMS date as a timestamp.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  t = now;
  c = datevec ( t );
  s = datestr ( c, 0 );
  fprintf ( 1, '%s\n', s );

  return
end
 
