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

#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 */
  char *clabs = 0, *rlabs = 0;
  Complex *a = 0, *b = 0, *q = 0, *u = 0, *v = 0;
  double *alpha = 0, *beta = 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_zggsvd (f08vnc) 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 (!(clabs = NAG_ALLOC(2, char)) ||
        !(rlabs = NAG_ALLOC(2, char)) ||
        !(a = NAG_ALLOC(m * n, Complex)) ||
        !(b = NAG_ALLOC(p * n, Complex)) ||
        !(q = NAG_ALLOC(n * n, Complex)) ||
        !(u = NAG_ALLOC(m * m, Complex)) ||
        !(v = NAG_ALLOC(p * p, Complex)) ||
        !(alpha = NAG_ALLOC(n, double)) ||
        !(beta = NAG_ALLOC(n, 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 , %lf )", &A(i, j).re, &A(i, j).im);
  scanf("%*[^\n] ");

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

  /* nag_zggsvd (f08vnc)
   * Compute the generalized singular value decomposition of (A, B)
   * (A = U*D1*(0 R)*(Q^H), B = V*D2*(0 R)*(Q^H), m.ge.n)
   */
  nag_zggsvd(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_zggsvd (f08vnc).\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("Number of finite generalized singular values (l)\n");
  printf("%5" NAG_IFMT "\n", l);
  printf("Numerical rank of ( A^H B^H)^H   (k+l)\n");
  printf("%5" NAG_IFMT "\n\n", irank);
  printf("Finite 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_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                                m, m, u, pdu, Nag_BracketForm, "%13.4e",
                                "Unitary matrix U", Nag_IntegerLabels,
                                0, Nag_IntegerLabels, 0, 80, 0, 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\n");
  fflush(stdout);
  nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                                p, p, v, pdv, Nag_BracketForm, "%13.4e",
                                "Unitary matrix V", Nag_IntegerLabels,
                                0, Nag_IntegerLabels, 0, 80, 0, 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\n");
  fflush(stdout);
  nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                                n, n, q, pdq, Nag_BracketForm, "%13.4e",
                                "Unitary matrix Q", Nag_IntegerLabels,
                                0, Nag_IntegerLabels, 0, 80, 0, 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\n");
  fflush(stdout);
  nag_gen_complx_mat_print_comp(order, Nag_UpperMatrix, Nag_NonUnitDiag,
                                irank, irank, &A(1, n - irank + 1), pda,
                                Nag_BracketForm, "%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_complx_mat_print_comp (x04dbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_ztrcon (f07tuc)
   * estimate the reciprocal condition number of R
   */
  nag_ztrcon(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_ztrcon (f07tuc).\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\n");
    printf("%11.1e\n", serrbd);
  }
  else {
    printf("(A^H B^H)^H is not of full rank\n");
  }

END:

  NAG_FREE(clabs);
  NAG_FREE(rlabs);
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(q);
  NAG_FREE(u);
  NAG_FREE(v);
  NAG_FREE(alpha);
  NAG_FREE(beta);
  NAG_FREE(iwork);

  return exit_status;
}

#undef B
#undef A