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

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

int main(void)
{
  /* Scalars */
  double d, f, tol;
  Integer i, j, k, m, n, nrhs, pda, pdb;
  Integer exit_status = 0;
  /* Arrays */
  double *a = 0, *b = 0, *rnorm = 0, *tau = 0, *work = 0;
  Integer *jpvt = 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_dtzrzf (f08bhc) Example Program Results\n\n");

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

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

  /* 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)) ||
      !(work = NAG_ALLOC(n, double)) || !(jpvt = NAG_ALLOC(n, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* 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_iload (f16dbc).
   * Initialize jpvt to be zero so that all columns are free.
   */
  nag_iload(n, 0, jpvt, 1, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_iload (f16dbc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_dgeqp3 (f08bfc).
   * Compute the QR factorization of A with column pivoting as
   * A = Q*(R11 R12)*(P^T)                                   
   *       ( 0  R22)                                          
   */
  nag_dgeqp3(order, m, n, a, pda, jpvt, tau, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dgeqp3 (f08bfc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

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

  /* Choose tol to reflect the relative accuracy of the input data */
  tol = 0.01;

  /* Determine and print the rank, k, of R relative to tol */
  for (k = 1; k <= n; ++k)
    if ((f = A(k, k), fabs(f)) <= tol * (d = A(1, 1), fabs(d)))
      break;
  --k;

  printf("Tolerance used to estimate the rank of A\n");
  printf("%11.2e\n", tol);
  printf("Estimated rank of A\n");
  printf("%8" NAG_IFMT "\n\n", k);

  /* nag_dtzrzf (f08bhc).
   * Compute the RZ factorization of the k by k part of R as
   * (R11 R12) = (T 0)*Z                                     
   */
  nag_dtzrzf(order, k, n, a, pda, tau, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dtzrzf (f08bhc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_dtrsm (f16yjc).
   * Compute least squares solutions of triangular problems by
   * back substitution in T*Y1 = C1, storing the result in b.
   */
  nag_dtrsm(order, Nag_LeftSide, Nag_Upper, Nag_NoTrans, Nag_NonUnitDiag,
            k, nrhs, 1.0, a, pda, b, pdb, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dtrsm (f16yjc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_dge_norm (f16rac).
   * Compute estimates of the square roots of the residual sums of
   * squares (2-norm of each of the columns of C2).
   */
  for (j = 1; j <= nrhs; ++j) {
    nag_dge_norm(order, Nag_FrobeniusNorm, m - k, 1, &B(k + 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;
    }
  }

  /* nag_dge_load (f16qhc).
   * Set the remaining elements of the solutions to zero (to give 
   * the minimum-norm solutions), Y2 = 0.
   */
  nag_dge_load(order, n - k, nrhs, 0.0, 0.0, &B(k + 1, 1), pdb, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dge_load (f16qhc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_dormrz (f08bkc).
   * Form W = (Z^T)*Y. 
   */
  nag_dormrz(order, Nag_LeftSide, Nag_Trans, n, nrhs, k, n - k, a, pda, tau,
             b, pdb, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dormrz (f08bkc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Permute the least squares solutions stored in B to give X = P*W */
  for (j = 1; j <= nrhs; ++j) {
    for (i = 1; i <= n; ++i)
      work[jpvt[i - 1] - 1] = B(i, j);
    for (i = 1; i <= n; ++i)
      B(i, j) = work[i - 1];
  }

  /* nag_gen_real_mat_print (x04cac).
   * Print least squares solutions.
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, nrhs,
                         b, 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;
  }

  /* Print the square roots of the residual sums of squares */
  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 % 6 == 5 ? "\n" : " ");

END:
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(rnorm);
  NAG_FREE(tau);
  NAG_FREE(work);
  NAG_FREE(jpvt);
  return exit_status;
}

#undef A
#undef B