/* nag_zgerqf (f08cvc) 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>

int main(void)
{
  /* Scalars */
  Complex       zero = { 0.0, 0.0 };
  Integer       i, j, m, n, nrhs, pda, pdb, pdx;
  Integer       exit_status = 0;
  /* Arrays */
  Complex       *a = 0, *b = 0, *tau = 0, *x = 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_zgerqf (f08cvc) Example Program Results\n\n");

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

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

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

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

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

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

  /* nag_zge_copy (f16tfc).
   * Copy the m*nrhs element vector b into x2, where x2 is
   * the vector containing the elements x(n-m+1), ..., x(n) of x.
   */
  nag_zge_copy(order, Nag_NoTrans, m, 1, &B(1, 1), pdb, &x[n - m], pdx, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_zge_copy (f16tfc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* nag_ztrtrs (f07tsc).
   * Solve R*y2 = b, storing the result in x2.
   */
  nag_ztrtrs(order, Nag_Upper, Nag_NoTrans, Nag_NonUnitDiag, m, 1,
             &A(1, n - m + 1), pda, &x[n - m], pdx, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_ztrtrs (f07tsc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* nag_zload (f16hbc).
   * Set y1 to zero (stored in rows 1 to (n-m) of x).
   */
  nag_zload(n - m, zero, x, 1, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_zload (f16hbc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* nag_zunmrq (f08cxc).
   * Compute minimum-norm solution x = (Q**H)*y.
   */
  nag_zunmrq(order, Nag_LeftSide, Nag_ConjTrans, n, 1, m, a, pda, tau, x, pdx,
             &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_zunmrq (f08cxc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* Print minimum-norm solution */
  printf("Minimum-norm solution\n");
  for (i = 0; i < n; ++i)
    printf("(%8.4f, %8.4f)\n", x[i].re, x[i].im);

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

  return exit_status;
}

#undef A
#undef B