/* nag_dorgbr (f08kfc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

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

int main(void)
{
  /* Scalars */
  Integer i, ic, j, m, n, pda, pdc, pdu, pdvt, d_len;
  Integer e_len, tauq_len, taup_len;
  Integer exit_status = 0;
  NagError fail;
  Nag_OrderType order;
  /* Arrays */
  double *a = 0, *c = 0, *d = 0, *e = 0, *taup = 0, *tauq = 0, *u = 0;
  double *vt = 0;

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

  INIT_FAIL(fail);

  printf("nag_dorgbr (f08kfc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  for (ic = 1; ic <= 2; ++ic) {
    scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &m, &n);

#ifdef NAG_COLUMN_MAJOR
    pda = m;
    pdu = m;
    pdvt = m;
#else
    pda = n;
    pdu = n;
    pdvt = n;
#endif
    pdc = n;
    d_len = n;
    e_len = n - 1;
    tauq_len = n;
    taup_len = n;

    /* Allocate memory */
    if (!(a = NAG_ALLOC(m * n, double)) ||
        !(c = NAG_ALLOC(n * n, double)) ||
        !(d = NAG_ALLOC(d_len, double)) ||
        !(e = NAG_ALLOC(e_len, double)) ||
        !(taup = NAG_ALLOC(taup_len, double)) ||
        !(tauq = NAG_ALLOC(tauq_len, double)) ||
        !(u = NAG_ALLOC(m * n, double)) || !(vt = NAG_ALLOC(m * n, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    /* Read A from data file */
    for (i = 1; i <= m; ++i) {
      for (j = 1; j <= n; ++j)
        scanf("%lf", &A(i, j));
    }
    scanf("%*[^\n] ");

    /* Reduce A to bidiagonal form using  nag_dgebrd (f08kec). */
    nag_dgebrd(order, m, n, a, pda, d, e, tauq, taup, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_dgebrd (f08kec).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
    if (m >= n) {
      /* Example 1 */
      /* Copy A to VT and U */
      for (i = 1; i <= n; ++i) {
        for (j = i; j <= n; ++j)
          VT(i, j) = A(i, j);
      }
      for (i = 1; i <= m; ++i) {
        for (j = 1; j <= MIN(i, n); ++j)
          U(i, j) = A(i, j);
      }
      /* nag_dorgbr (f08kfc):                                 */
      /*       Form P^T explicitly, storing the result in VT */
      nag_dorgbr(order, Nag_FormP, n, n, m, vt, pdvt, taup, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_dorgbr (f08kfc).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }

      /* nag_dorgbr (f08kfc):                             */
      /*       Form Q explicitly, storing the result in U */
      nag_dorgbr(order, Nag_FormQ, m, n, n, u, pdu, tauq, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_dorgbr (f08kfc).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }

      /* nag_dbdsqr (f08mec): Compute the SVD of A.        */
      nag_dbdsqr(order, Nag_Upper, n, n, m, 0, d, e, vt, pdvt, u,
                 pdu, c, pdc, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_dbdsqr (f08mec).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }

      /* Print singular values, left & right singular vectors */
      printf("\n Example 1: singular values\n");
      for (i = 1; i <= n; ++i)
        printf("%8.4f%s", d[i - 1], i % 8 == 0 ? "\n" : " ");
      printf("\n\n");

      /* nag_gen_real_mat_print (x04cac): Print VT. */
      fflush(stdout);
      nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                             n, n, vt, pdvt,
                             "Example 1: 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;
      }
      printf("\n");

      /* nag_gen_real_mat_print (x04cac): Print U. */
      fflush(stdout);
      nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                             m, n, u, pdu,
                             "Example 1: left singular vectors, by column",
                             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;
      }
    }
    else {
      /* Example 2 */
      /* Copy A to VT and U */
      for (i = 1; i <= m; ++i) {
        for (j = i; j <= n; ++j)
          VT(i, j) = A(i, j);
      }
      for (i = 1; i <= m; ++i) {
        for (j = 1; j <= i; ++j)
          U(i, j) = A(i, j);
      }
      /* nag_dorgbr (f08kfc):                                  */
      /*        Form P^T explicitly, storing the result in VT */
      nag_dorgbr(order, Nag_FormP, m, n, m, vt, pdvt, taup, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_dorgbr (f08kfc).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }
      /* nag_dorgbr (f08kfc):                              */
      /*        Form Q explicitly, storing the result in U */
      nag_dorgbr(order, Nag_FormQ, m, m, n, u, pdu, tauq, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_dorgbr (f08kfc).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }
      /* nag_dbdsqr (f08mec): Compute the SVD of A */
      nag_dbdsqr(order, Nag_Lower, m, n, m, 0, d, e, vt, pdvt, u,
                 pdu, c, pdc, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_dbdsqr (f08mec).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }

      /* Print singular values, left & right singular vectors */
      printf("\n Example 2: singular values\n");
      for (i = 1; i <= m; ++i)
        printf("%8.4f%s", d[i - 1], i % 8 == 0 ? "\n" : " ");
      printf("\n\n");
      /* nag_gen_real_mat_print (x04cac): Print VT */
      fflush(stdout);
      nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                             m, n, vt, pdvt,
                             "Example 2: 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;
      }
      printf("\n");
      /* nag_gen_real_mat_print (x04cac): print U */
      fflush(stdout);
      nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                             m, m, u, pdu,
                             "Example 2: left singular vectors, by column",
                             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;
      }
    }
  END:
    NAG_FREE(a);
    NAG_FREE(c);
    NAG_FREE(d);
    NAG_FREE(e);
    NAG_FREE(taup);
    NAG_FREE(tauq);
    NAG_FREE(u);
    NAG_FREE(vt);
  }
  return exit_status;
}

#undef A
#undef U
#undef VT