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

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

int main(void)
{
  /* Scalars */
  Integer       i, j, m, n, nrhs, pda, pdb;
  Integer       exit_status = 0;
  /* Arrays */
  double        *a = 0, *b = 0, *rnorm = 0, *tau = 0;
  /* Nag Types */
  Nag_OrderType order;
  NagError      fail;

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

  INIT_FAIL(fail);

  printf("nag_dgeqlf (f08cec) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%ld%ld%ld%*[^\n]", &m, &n, &nrhs);

  /* Allocate memory */
  if (!(a = NAG_ALLOC(m*n, double)) ||
      !(b = NAG_ALLOC(m*nrhs, double)) ||
      !(rnorm = NAG_ALLOC(nrhs, double)) ||
      !(tau = NAG_ALLOC(n, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

#ifdef NAG_COLUMN_MAJOR
  pda = m;
  pdb = m;
#else
  pda = n;
  pdb = nrhs;
#endif

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

  for (i = 1; i <= m; ++i)
    for (j = 1; j <= nrhs; ++j)
      scanf("%lf", &B(i, j));
  scanf("%*[^\n]");

  /* nag_dgeqlf (f08cec).
   * Compute the QL factorization of A.
   */
  nag_dgeqlf(order, m, n, a, pda, tau, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dgeqlf (f08cec).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* nag_dormql (f08cgc).
   * Compute C = (C1) = (Q**T)*B, storing the result in B.
   *             (C2)
   */
  nag_dormql(order, Nag_LeftSide, Nag_Trans, m, nrhs, n, a, pda, tau, b, pdb,
             &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dormql (f08cgc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* nag_dtrtrs (f07tec).
   * Compute least-squares solutions by backsubstitution in
   * L*X = C2.
   */
  nag_dtrtrs(order, Nag_Lower, Nag_NoTrans, Nag_NonUnitDiag, n, nrhs,
             &A(m - n + 1, 1), pda, &B(m - n + 1, 1), pdb, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dtrtrs (f07tec).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* nag_gen_real_mat_print (x04cac).
   * Print least-squares solution(s).
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, nrhs,
                         &B(m - n + 1, 1), pdb, "Least-squares solution(s)",
                         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;
    }

  /* nag_dge_norm (f16rac).
   * Compute and print estimates of the square roots of the residual
   * sums of squares.
   */
  for (j = 1; j <= nrhs; ++j) {
    nag_dge_norm(order, Nag_FrobeniusNorm, m-n, 1, &B(1, j), pdb,
                 &rnorm[j-1], &fail);
    if (fail.code != NE_NOERROR)
      {
        printf("Error from nag_dge_norm (f16rac).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }
  }

  printf("\nSquare root(s) of the residual sum(s) of squares\n");
  for (j = 0; j < nrhs; ++j)
    printf("%11.2e%s", rnorm[j], (j+1)%7 == 0?"\n":" ");

 END:
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(rnorm);
  NAG_FREE(tau);

  return exit_status;
}

#undef A
#undef B