/* nag_dormbr (f08kgc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

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

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

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

  INIT_FAIL(fail);

  printf("nag_dormbr (f08kgc) Example Program Results\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;
#else
    pda = n;
#endif
    pdu = m;
    pdpt = n;
    taup_len = n;
    tauq_len = n;
    tau_len = n;
    d_len = n;
    e_len = n - 1;

    /* Allocate memory */
    if (!(a = NAG_ALLOC(m * n, double)) ||
        !(d = NAG_ALLOC(d_len, double)) ||
        !(e = NAG_ALLOC(e_len, double)) ||
        !(pt = NAG_ALLOC(n * n, double)) ||
        !(tau = NAG_ALLOC(tau_len, double)) ||
        !(taup = NAG_ALLOC(taup_len, double)) ||
        !(tauq = NAG_ALLOC(tauq_len, double)) ||
        !(u = NAG_ALLOC(m * m, 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] ");
    if (m >= n) {
      /* Example 1. */

      /* nag_dgeqrf (f08aec): Compute the QR factorization of A */
      nag_dgeqrf(order, m, n, a, pda, tau, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_dgeqrf (f08aec).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }
      /* Copy A to U */
      for (i = 1; i <= m; ++i) {
        for (j = 1; j <= MIN(i, n); ++j)
          U(i, j) = A(i, j);
      }
      /* nag_dorgqr (f08afc):                              */
      /*        Form Q explicitly, storing the result in U */
      nag_dorgqr(order, m, m, n, u, pdu, tau, &fail);
      if (fail.code != NE_NOERROR) {
        printf("order=%d\n", order);
        printf("Error from nag_dorgqr (f08afc).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }
      /* Copy R to PT (used as workspace) */
      nag_dtr_copy(order, Nag_Upper, Nag_NoTrans, Nag_NonUnitDiag, n, a,
                   pda, pt, pdpt, &fail);
      /* Set the strictly lower triangular part of R to zero */
      for (i = 2; i <= n; ++i) {
        for (j = 1; j <= MIN(i - 1, n - 1); ++j)
          PT(i, j) = 0.0;
      }
      /* nag_dgebrd (f08kec): Bidiagonalize R. */
      nag_dgebrd(order, n, n, pt, pdpt, 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;
      }
      /* nag_dormbr (f08kgc): Update Q, storing the result in U. */
      nag_dormbr(order, Nag_ApplyQ, Nag_RightSide, Nag_NoTrans,
                 m, n, n, pt, pdpt, tauq, u, pdu, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_dormbr (f08kgc).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }
      /* Print bidiagonal form and matrix Q */
      printf("\nExample 1: bidiagonal matrix B\nDiagonal\n");
      for (i = 1; i <= n; ++i)
        printf("%8.4f%s", d[i - 1], i % 8 == 0 ? "\n" : " ");
      printf("\nSuperdiagonal\n");
      for (i = 1; i <= n - 1; ++i)
        printf("%8.4f%s", e[i - 1], i % 8 == 0 ? "\n" : " ");
      printf("\n\n");
      /* nag_gen_real_mat_print (x04cac): Print Q as stored in u. */
      fflush(stdout);
      nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                             m, n, u, pdu, "Example 1: matrix Q", 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. */

      /* nag_dgelqf (f08ahc): Compute the LQ factorization of A. */
      nag_dgelqf(order, m, n, a, pda, tau, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_dgelqf (f08ahc).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }
      /* Copy A to PT */
      for (i = 1; i <= m; ++i) {
        for (j = i; j <= n; ++j)
          PT(i, j) = A(i, j);
      }
      /* nag_dorglq (f08ajc):                               */
      /*       Form Q explicitly, storing the result in PT. */
      nag_dorglq(order, n, n, m, pt, pdpt, tau, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_dorglq (f08ajc).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }
      /* Copy L to U (used as workspace) */
      nag_dtr_copy(order, Nag_Lower, Nag_NoTrans, Nag_NonUnitDiag, m, a,
                   pda, u, pdu, &fail);
      /* Set the strictly upper triangular part of L to zero */
      for (i = 1; i <= m - 1; ++i) {
        for (j = i + 1; j <= m; ++j)
          U(i, j) = 0.0;
      }
      /* nag_dgebrd (f08kec): Bidiagonalize L. */
      nag_dgebrd(order, m, m, u, pdu, 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;
      }
      /* nag_dormbr (f08kgc):Update P^T, storing the result in PT. */
      nag_dormbr(order, Nag_ApplyP, Nag_LeftSide, Nag_Trans, m, n, m, u,
                 pdu, taup, pt, pdpt, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_dormbr (f08kgc).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }

      /* Print bidiagonal form and matrix P^T */
      printf("\nExample 2: bidiagonal matrix B\n%s\n", "Diagonal\n");
      for (i = 1; i <= m; ++i)
        printf("%8.4f%s", d[i - 1], i % 8 == 0 ? "\n" : " ");
      printf("\nSuperdiagonal\n");
      for (i = 1; i <= m - 1; ++i)
        printf("%8.4f%s", e[i - 1], i % 8 == 0 ? "\n" : " ");
      printf("\n\n");

      /* nag_gen_real_mat_print (x04cac), Print pt. */
      fflush(stdout);
      nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                             m, n, pt, pdpt, "Example 2: matrix P^T",
                             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(d);
    NAG_FREE(e);
    NAG_FREE(pt);
    NAG_FREE(tau);
    NAG_FREE(taup);
    NAG_FREE(tauq);
    NAG_FREE(u);
  }
  return exit_status;
}

#undef A
#undef U
#undef PT