NAG Library Manual, Mark 27.3
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_lapackeig_zggsvp (f08vsc) Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.3, 2021.
 */

#include <nag.h>
#include <stdio.h>

int main(void) {
  /* Scalars */
  double norm, eps, tola, tolb;
  Integer i, irank, j, k, l, m, n, nrows, p, pda, pdb, pdq, pdu, pdv;
  Integer exit_status = 0;

  /* Arrays */
  Complex *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_lapackeig_zggsvp (f08vsc) 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, Complex)) || !(b = NAG_ALLOC(p * n, Complex)) ||
      !(q = NAG_ALLOC(n * n, Complex)) || !(u = NAG_ALLOC(m * m, Complex)) ||
      !(v = NAG_ALLOC(p * m, Complex))) {
    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 , %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]");

  /* Get the machine precision, using nag_machine_precision (x02ajc) */
  eps = nag_machine_precision;
  /* Compute one-norm of A nad B using nag_blast_zge_norm (f16uac). */
  nag_blast_zge_norm(order, Nag_OneNorm, m, n, a, pda, &norm, &fail);
  nag_blast_zge_norm(order, Nag_OneNorm, p, n, b, pdb, &norm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_blast_zge_norm (f16uac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  tola = MAX(m, n) * norm * eps;
  tolb = MAX(p, n) * norm * eps;

  /* Compute the factorization of (A, B) A = U*S*(Q^H), B = V*T*(Q^H))
   * using using nag_lapackeig_zggsvp (f08vsc).
   */
  nag_lapackeig_zggsvp(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_lapackeig_zggsvp (f08vsc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print details of the generalizeed SVD */
  irank = k + l;
  printf("Numerical rank of ( A^H B^H)^H   (k+l)\n%5" NAG_IFMT "\n\n", irank);
  nrows = MIN(m, irank);
  fflush(stdout);
  nag_file_print_matrix_complex_gen_comp(
      order, Nag_UpperMatrix, Nag_NonUnitDiag, nrows, irank,
      &A(1, n - irank + 1), pda, Nag_BracketForm, "%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_file_print_matrix_complex_gen_comp(
      order, Nag_UpperMatrix, Nag_NonUnitDiag, l, l, &B(1, n - l + 1), pdb,
      Nag_BracketForm, "%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_file_print_matrix_complex_gen_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)
    goto FAIL;
  printf("\n");
  fflush(stdout);
  nag_file_print_matrix_complex_gen_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)
    goto FAIL;
  printf("\n");
  fflush(stdout);
  nag_file_print_matrix_complex_gen_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);
FAIL:
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_complex_gen_comp (x04dbc).\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;
}