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

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

int main(void)
{

  /* Scalars */
  double        rcond, rnorm;
  Integer       exit_status = 0, i, j, m, n, nrhs, rank, pda, pdb;

  /* Arrays */
  double        *a = 0, *b = 0, *s = 0;

  /* Nag Types */
  NagError      fail;
  Nag_OrderType order;

#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_dgelss (f08kac) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%ld%ld%ld%*[^\n]", &m, &n, &nrhs);
  if (m < 0 || n < 0 || nrhs < 0)
    {
      printf("Invalid m, n or nrhs\n");
      exit_status = 1;
      goto END;
    }

#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(MAX(m, n) * nrhs, double)) ||
      !(s = NAG_ALLOC(MIN(m, n), double)))
    {
      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]");

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

  /* Solve the least squares problem min( norm2(b - Ax) ) for the x
   * of minimum norm using nag_dgelss (f08kac). 
   */
  nag_dgelss(order, m, n, nrhs, a, pda, b, pdb, s, rcond, &rank, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dgelss (f08kac).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  printf("Least squares solution\n");
  for (i = 1; i <= n; ++i)
    {
      for (j = 1; j <= nrhs; ++j)
        printf(" %10.4f%s", B(i, j), j%7==0?"\n":"");
      printf("\n");
    }

  /* Print the effective rank of A */
  printf("\nTolerance used to estimate the rank of A\n%11.2e\n", rcond);
  printf("\nEstimated rank of A\n%6ld\n", rank);

  /* Print singular values of A */
  printf("\nSingular values of A\n");
  for (i = 0; i < n; ++i) printf(" %10.4f%s", s[i], i%7 == 6?"\n":"");
  printf("\n");

  /* Compute and print estimate of the square root of the
   * residual sum of squares using nag_dge_norm (f16rac).
   */
  if (rank == n)
    {
      nag_dge_norm(order, Nag_FrobeniusNorm, m - n, 1, &B(n+1,1), pdb, &rnorm,
                   &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 of the residual sum of squares\n%11.2e\n", rnorm);
    }

 END:
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(s);

  return exit_status;
}
#undef A
#undef B