/* nag_dggsvd (f08vac) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

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

int main(void)
{

  /* Scalars */
  double d, eps, rcond, serrbd;
  Integer exit_status = 0, i, irank, j, k, l, m, n, p,
         pda, pdb, pdq, pdu, pdv;
  NagError fail;
  Nag_OrderType order;

  /* Arrays */
  double *a = 0, *alpha = 0, *b = 0, *beta = 0, *q = 0, *u = 0, *v = 0;
  Integer *iwork = 0;

#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_dggsvd (f08vac) Example Program Results\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &m, &n, &p);
  if (m <= 10 && n <= 10 && p <= 10) {
    /* Allocate memory */
    if (!(a = NAG_ALLOC(m * n, double)) ||
        !(alpha = NAG_ALLOC(n, double)) ||
        !(b = NAG_ALLOC(p * n, double)) ||
        !(beta = NAG_ALLOC(n, double)) ||
        !(q = NAG_ALLOC(n * n, double)) ||
        !(u = NAG_ALLOC(m * m, double)) ||
        !(v = NAG_ALLOC(p * p, double)) || !(iwork = NAG_ALLOC(n, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
#ifdef NAG_COLUMN_MAJOR
    pda = m;
    pdb = p;
    pdq = n;
    pdu = m;
    pdv = p;
#else
    pda = n;
    pdb = n;
    pdq = n;
    pdu = m;
    pdv = p;
#endif
  }
  else {
    printf("m and/or n too small\n");
    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] ");

  /* nag_dggsvd (f08vac)
   * Compute the generalized singular value decomposition of (A, B)
   * (A = U*D1*(0 R)*(Q^T), B = V*D2*(0 R)*(Q^T), m.ge.n)
   */

  nag_dggsvd(order, Nag_AllU, Nag_ComputeV, Nag_ComputeQ, m, n, p, &k, &l, a,
             pda, b, pdb, alpha, beta, u, pdu, v, pdv, q, pdq, iwork, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dggsvd (f08vac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Print solution */
  irank = k + l;

  printf("Number of infinite generalized singular values (k)\n");
  printf("%5" NAG_IFMT "\n", k);
  printf("\nNumber of finite generalized singular values (l)\n");
  printf("%5" NAG_IFMT "\n", l);
  printf("Numerical rank of ( A^T B^T)^T   (k+l)\n");
  printf("%5" NAG_IFMT "\n", irank);

  printf("\nFinite generalized singular values\n");
  for (j = k; j < irank; ++j) {
    d = alpha[j] / beta[j];
    printf("%13.4e%s", d, (j + 1) % 8 == 0 || (j + 1) == irank ? "\n" : " ");
  }

  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) {
    printf("Error from nag_gen_real_mat_print_comp (x04cbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  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) {
    printf("Error from nag_gen_real_mat_print_comp (x04cbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  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);
  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;
  }

  printf("\n");
  fflush(stdout);
  nag_gen_real_mat_print_comp(order, Nag_UpperMatrix, Nag_NonUnitDiag, irank,
                              irank, &A(1, n - irank + 1), pda, "%13.4e",
                              "Nonsingular upper triangular matrix R",
                              Nag_IntegerLabels, 0, Nag_IntegerLabels, 0,
                              80, 0, 0, &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;
  }

  /* nag_dtrcon (f07tgc)
   * estimate the reciprocal condition number of R
   */

  nag_dtrcon(order, Nag_InfNorm, Nag_Upper, Nag_NonUnitDiag, irank,
             &A(1, n - irank + 1), pda, &rcond, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dtrcon (f07tgc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\nEstimate of reciprocal condition number for R\n");
  printf("%11.1e\n\n", rcond);

  /* So long as irank = n, get the machine precision, eps, and compute the
   * approximate error bound for the computed generalized singular values
   */
  if (irank == n) {
    eps = nag_machine_precision;
    serrbd = eps / rcond;
    printf("Error estimate for the generalized singular values");
    printf("\n%11.1e\n", serrbd);
  }
  else {
    printf("( A^T B^T)^T is not of full rank\n");
  }

END:
  NAG_FREE(a);
  NAG_FREE(alpha);
  NAG_FREE(b);
  NAG_FREE(beta);
  NAG_FREE(q);
  NAG_FREE(u);
  NAG_FREE(v);
  NAG_FREE(iwork);

  return exit_status;
}

#undef B
#undef A