/* nag_dggsvp (f08vec) 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 <nagx02.h>
#include <nagx04.h>

int main(void)
{
  /* Scalars */
  double eps, norm, tola, tolb;
  Integer i, irank, j, k, l, m, n, nrows, p, pda, pdb, pdq, pdu, pdv;
  Integer exit_status = 0;
  /* Arrays */
  double *a = 0, *b = 0, *q = 0, *u = 0, *v = 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_dggsvp (f08vec) Example Program Results\n\n");

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

#ifdef NAG_COLUMN_MAJOR
  pda = m;
  pdb = p;
  pdv = p;
#else
  pda = n;
  pdb = n;
  pdv = m;
#endif
  pdq = n;
  pdu = m;

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

  /* Read the m by n matrix A and p by n matrix 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 <= p; ++i)
    for (j = 1; j <= n; ++j)
      scanf("%lf", &B(i, j));
  scanf("%*[^\n]");

  /* get norms of A and B using nag_dge_norm (f16rac). */
  nag_dge_norm(order, Nag_OneNorm, m, n, a, pda, &norm, &fail);
  nag_dge_norm(order, Nag_OneNorm, p, n, b, pdb, &norm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dge_norm (f16rac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Get the machine precision, using nag_machine_precision (x02ajc) */
  eps = nag_machine_precision;

  tola = MAX(m, n) * norm * eps;
  tolb = MAX(p, n) * norm * eps;

  /* Compute the factorization of (A, B) (A = U*S*(Q^T), B = V*T*(Q^T))
   * using nag_dggsvp (f08vec).
   */
  nag_dggsvp(order, Nag_AllU, Nag_ComputeV, Nag_ComputeQ, m, p, n, a, pda, b,
             pdb, tola, tolb, &k, &l, u, pdu, v, pdv, q, pdq, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dggsvp (f08vec).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print details of the generalizeed SVD */
  irank = k + l;
  printf("Numerical rank of ( A^T B^T)^T   (k+l)\n%5" NAG_IFMT "\n\n", irank);
  nrows = MIN(m, irank);
  fflush(stdout);
  nag_gen_real_mat_print_comp(order, Nag_UpperMatrix, Nag_NonUnitDiag, nrows,
                              irank, &A(1, n - irank + 1), pda, "%13.4e",
                              "Matrix S", Nag_IntegerLabels, 0,
                              Nag_IntegerLabels, 0, 80, 0, 0, &fail);
  if (fail.code != NE_NOERROR)
    goto FAIL;
  printf("\n");
  fflush(stdout);
  nag_gen_real_mat_print_comp(order, Nag_UpperMatrix, Nag_NonUnitDiag, l, l,
                              &B(1, n - l + 1), pdb, "%13.4e",
                              "Upper triangular matrix T", Nag_IntegerLabels,
                              0, Nag_IntegerLabels, 0, 80, 0, 0, &fail);
  if (fail.code != NE_NOERROR)
    goto FAIL;
  printf("\n");
  fflush(stdout);
  nag_gen_real_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m,
                              m, u, pdu, "%13.4e", "Orthogonal matrix U",
                              Nag_IntegerLabels, 0, Nag_IntegerLabels, 0,
                              80, 0, 0, &fail);
  if (fail.code != NE_NOERROR)
    goto FAIL;
  printf("\n");
  fflush(stdout);
  nag_gen_real_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, p, p,
                              v, pdv, "%13.4e", "Orthogonal matrix V",
                              Nag_IntegerLabels, 0, Nag_IntegerLabels, 0, 80,
                              0, 0, &fail);
  if (fail.code != NE_NOERROR)
    goto FAIL;
  printf("\n");
  fflush(stdout);
  nag_gen_real_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n,
                              q, pdq, "%13.4e", "Orthogonal matrix Q",
                              Nag_IntegerLabels, 0, Nag_IntegerLabels, 0, 80,
                              0, 0, &fail);
FAIL:
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print_comp (x04cbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

END:
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(q);
  NAG_FREE(u);
  NAG_FREE(v);

  return exit_status;
}