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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_ztgsen (f08yuc) Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 *
 * Mark 28.5, 2022.
 */

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

int main(void) {
  /* Scalars */
  double abs_a, abs_b, pl, pr, small;
  Complex eig;
  Integer i, ijob, j, m, n, pds, pdt, pdq, pdz;
  Integer exit_status = 0;

  /* Arrays */
  Complex *alpha = 0, *beta = 0, *q = 0, *s = 0, *t = 0, *z = 0;
  double dif[2];
  char nag_enum_arg[40];

  /* Nag Types */
  NagError fail;
  Nag_OrderType order;
  Nag_Boolean wantq, wantz;
  Nag_Boolean *select = 0;

#ifdef NAG_COLUMN_MAJOR
#define Q(I, J) q[(J - 1) * pdq + I - 1]
#define Z(I, J) z[(J - 1) * pdz + I - 1]
#define S(I, J) s[(J - 1) * pds + I - 1]
#define T(I, J) t[(J - 1) * pdt + I - 1]
  order = Nag_ColMajor;
#else
#define Q(I, J) q[(I - 1) * pdq + J - 1]
#define Z(I, J) z[(I - 1) * pdz + J - 1]
#define S(I, J) s[(I - 1) * pds + J - 1]
#define T(I, J) t[(I - 1) * pdt + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_ztgsen (f08yuc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &n, &ijob);
  if (n < 0 || ijob < 0 || ijob > 5) {
    printf("Invalid n or ijob\n");
    exit_status = 1;
    goto END;
  }
  scanf(" %39s%*[^\n]", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  wantq = (Nag_Boolean)nag_enum_name_to_value(nag_enum_arg);
  scanf(" %39s%*[^\n]", nag_enum_arg);
  wantz = (Nag_Boolean)nag_enum_name_to_value(nag_enum_arg);

  pds = n;
  pdt = n;
  pdq = (wantq ? n : 1);
  pdz = (wantz ? n : 1);

  /* Allocate memory */
  if (!(s = NAG_ALLOC(n * n, Complex)) || !(t = NAG_ALLOC(n * n, Complex)) ||
      !(alpha = NAG_ALLOC(n, Complex)) || !(beta = NAG_ALLOC(n, Complex)) ||
      !(select = NAG_ALLOC(n, Nag_Boolean)) ||
      !(q = NAG_ALLOC(pdq * pdq, Complex)) ||
      !(z = NAG_ALLOC(pdz * pdz, Complex))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  for (i = 0; i < n; ++i) {
    scanf("%39s", nag_enum_arg);
    select[i] = (Nag_Boolean)nag_enum_name_to_value(nag_enum_arg);
  }
  scanf("%*[^\n]");

  /* Read S, T, Q, Z and the logical array select from data file */
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
      scanf(" ( %lf , %lf )", &S(i, j).re, &S(i, j).im);
  scanf("%*[^\n]");
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
      scanf(" ( %lf , %lf )", &T(i, j).re, &T(i, j).im);
  scanf("%*[^\n]");
  if (wantq) {
    for (i = 1; i <= n; ++i)
      for (j = 1; j <= n; ++j)
        scanf(" ( %lf , %lf )", &Q(i, j).re, &Q(i, j).im);
    scanf("%*[^\n]");
  }
  if (wantz) {
    for (i = 1; i <= n; ++i)
      for (j = 1; j <= n; ++j)
        scanf(" ( %lf , %lf )", &Z(i, j).re, &Z(i, j).im);
    scanf("%*[^\n]");
  }

  /* Reorder the Schur factors S and T and update the matrices  Q and Z. */
  nag_lapackeig_ztgsen(order, ijob, wantq, wantz, select, n, s, pds, t, pdt,
                       alpha, beta, q, pdq, z, pdz, &m, &pl, &pr, dif, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_ztgsen (f08yuc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_machine_real_safe (x02amc). */
  small = nag_machine_real_safe;

  /* Print the eigenvalues */
  printf("Selected Eigenvalues\n");
  for (j = 0; j < m; ++j) {
    printf("%2" NAG_IFMT " ", j + 1);
    abs_a = nag_complex_abs(alpha[j]);
    abs_b = nag_complex_abs(beta[j]);
    if (abs_a * small >= abs_b)
      printf("  infinite or undetermined, alpha = (%13.4e, %13.4e), "
             "|beta| = %13.4e\n",
             alpha[j].re, alpha[j].im, abs_b);
    else {
      eig = nag_complex_divide(alpha[j], beta[j]);
      printf("  (%13.4e, %13.4e)\n", eig.re, eig.im);
    }
  }

  /* Print deflating subspaces */
  if (ijob == 1 || ijob == 4 || ijob == 5) {
    printf("\n");
    printf("For the selected eigenvalues,\nthe reciprocals of projection "
           "norms onto the deflating subspaces are\n");
    printf(" for left  subspace, pl = %11.2e\n for right subspace, pr = "
           "%11.2e\n\n",
           pl, pr);
  }
  if (ijob > 1) {
    printf(" upper bound on Difu    = %11.2e\n", dif[0]);
    printf(" upper bound on Difl    = %11.2e\n", dif[1]);
    if (ijob == 2 || ijob == 4) {
      printf("\nUpper bounds on Difl, Difu are based on the Frobenius norm\n");
    }
    if (ijob == 3 || ijob == 5) {
      printf("\nUpper bounds on Difl, Difu are based on the one norm.\n");
    }
  }

END:
  NAG_FREE(s);
  NAG_FREE(t);
  NAG_FREE(alpha);
  NAG_FREE(beta);
  NAG_FREE(select);
  NAG_FREE(q);
  NAG_FREE(z);

  return exit_status;
}