#include "libscl.h"

using namespace std;
using namespace scl;

LIB_ERROR_HANDLER_PTR previous;

void do_not_exit (string msg)
{
  throw lib_error(msg);
}

int main()
{
  REAL zero = 0;

  INTEGER r=5, c=4;
  realmat A(r,c);

  INTEGER i,j;

  for (j=1; j<=c; j++) {
    for (i=1; i<=r; i++) {
      A(i,j)=j+i;
    }
  }

  realmat U,S,V;

  cout << A << '\n';

  cout << "Return value " << svd(A,U,S,V) << '\n';

  cout << U << S << V << '\n';

  cout << U*diag(S)*T(V) << '\n';
  cout << T(U)*U << T(V)*V << '\n';

  A = T(A);

  cout << A << '\n';

  cout << "Return value " << svd(A,U,S,V) << '\n';

  cout << U << S << V << '\n';

  cout << U*diag(S)*T(V) << '\n';
  cout << T(U)*U << T(V)*V << '\n';

  A.resize(r,c,zero);

  cout << A << '\n';

  cout << "Return value " << svd(A,U,S,V) << '\n';

  cout << U << S << V << '\n';

  cout << U*diag(S)*T(V) << '\n';
  cout << T(U)*U << T(V)*V << '\n';

  previous = set_lib_error_handler(&do_not_exit);

  cout << starbox("/Try to invert a bad matrix//");
  try {
    realmat D = A;
    D[1] = exp(REAL_MAX);
    cout << svd(D,U,S,V) << '\n';
  }
  catch (lib_error e) {
    cout << e.what() << '\n';;
  }

  cout << starbox("/Try to invert a null matrix//");
  try {
    realmat D;
    cout << svd(D,U,S,V) << '\n';
  }
  catch (lib_error e) {
    cout << e.what() << '\n';;
  }

  previous = set_lib_error_handler(previous);


  return 0;

}


