/* nag_dgesdd (f08kdc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */

#include <stdio.h>
#include <nag.h>
#include <nagx04.h>
#include <nag_stdlib.h>
#include <nagf08.h>
#include <nagx02.h>

int main(void)
{

  /* Scalars */
  double        eps, serrbd;
  Integer       exit_status = 0, i, j, m, n, pda, pdu;

  /* Arrays */
  double        *a = 0, *rcondu = 0, *rcondv = 0, *s = 0, *u = 0;
  double        *uerrbd = 0, *verrbd = 0;
  double        dummy[1];

  /* Nag Types */
  NagError      fail;
  Nag_OrderType order;

#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J - 1) * pda + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J) a[(I - 1) * pda + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_dgesdd (f08kdc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%ld%ld%*[^\n]", &m, &n);
  if (m < 0 && n < 0)
    {
      printf("Invalid m or n\n");
      exit_status = 1;
      goto END;
    }

  /* Allocate memory */
  if (!(a      = NAG_ALLOC(m * n, double)) ||
      !(rcondu = NAG_ALLOC(m, double)) ||
      !(rcondv = NAG_ALLOC(m, double)) ||
      !(s      = NAG_ALLOC(MIN(m, n), double)) ||
      !(u      = NAG_ALLOC(m * m, double)) ||
      !(uerrbd = NAG_ALLOC(m, double)) ||
      !(verrbd = NAG_ALLOC(m, double))
      )
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

#ifdef NAG_COLUMN_MAJOR
  pda = m;
  pdu = m;
#else
  pda = n;
  pdu = MIN(m, n);
#endif

  /* Read the m by n matrix A from data file */
  for (i = 1; i <= m; ++i)
    for (j = 1; j <= n; ++j) scanf("%lf", &A(i, j));
  scanf("%*[^\n]");

  /* nag_dgesdd (f08kdc).
   * Compute the singular values and left and right singular vectors
   * of A (A = U*S*(V**T), m.le.n)
  */
  nag_dgesdd(order, Nag_DoOverwrite, m, n, a, pda, s, u, pdu, dummy, 1, &fail);

  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dgesdd (f08kdc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* Print singular values */
  printf("Singular values\n");
  for (i = 0; i < m; ++i) printf(" %7.4f%s", s[i], i%8 == 7?"\n":"");
  printf("\n\n");

  /* Print left and right singular vectors using 
   * nag_gen_real_mat_print (x04cac).
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, m, u,
                         pdu, "Left singular vectors", 0, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
  printf("\n");

  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, n, a,
                         pda, "Right singular vectors by row",
                         0, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* Get the machine precision, eps using nag_machine_precision (x02ajc). */
  eps = nag_machine_precision;

  /* compute the approximate error bound for the computed singular values. 
   * Note that for the 2-norm, s[0] = ||A|| 
   */
  serrbd = eps * s[0];

  /* Call nag_ddisna (f08flc) to estimate reciprocal condition numbers for
   * the singular vectors.
   */
  nag_ddisna(Nag_LeftSingVecs, m, n, s, rcondu, &fail);
  nag_ddisna(Nag_RightSingVecs, m, n, s, rcondv, &fail);

  /* Compute the error estimates for the singular vectors. */
  for (i = 0; i < m; ++i)
    {
      uerrbd[i] = serrbd / rcondu[i];
      verrbd[i] = serrbd / rcondv[i];
    }

  /* Print the approximate error bounds for the singular values and vectors */
  printf("\nError estimate for the singular values\n%11.1e\n", serrbd);

  printf("\nError estimates for the left singular vectors\n");
  for (i = 0; i < m; ++i) printf(" %10.1e%s", uerrbd[i], i%6 == 5?"\n":"");

  printf("\n\nError estimates for the right singular vectors\n");
  for (i = 0; i < m; ++i) printf(" %10.1e%s", verrbd[i], i%6 == 5?"\n":"");
  printf("\n");

 END:
  NAG_FREE(a);
  NAG_FREE(rcondu);
  NAG_FREE(rcondv);
  NAG_FREE(s);
  NAG_FREE(u);
  NAG_FREE(uerrbd);
  NAG_FREE(verrbd);

  return exit_status;
}
#undef A