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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_dtgsen (f08ygc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.1, 2024.
 */

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

int main(void) {

  /* Scalars */
  double pl, pr, small;
  Integer i, ijob, j, m, n, pdq, pds, pdt, pdz;
  Integer exit_status = 0;

  /* Arrays */
  double *alphai = 0, *alphar = 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 S(I, J) s[(J - 1) * pds + I - 1]
#define T(I, J) t[(J - 1) * pdt + I - 1]
#define Q(I, J) q[(J - 1) * pdq + I - 1]
#define Z(I, J) z[(J - 1) * pdz + I - 1]
  order = Nag_ColMajor;
#else
#define S(I, J) s[(I - 1) * pds + J - 1]
#define T(I, J) t[(I - 1) * pdt + J - 1]
#define Q(I, J) q[(I - 1) * pdq + J - 1]
#define Z(I, J) z[(I - 1) * pdz + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_dtgsen (f08ygc) 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, double)) || !(t = NAG_ALLOC(n * n, double)) ||
      !(alphai = NAG_ALLOC(n, double)) || !(alphar = NAG_ALLOC(n, double)) ||
      !(beta = NAG_ALLOC(n, double)) || !(select = NAG_ALLOC(n, Nag_Boolean)) ||
      !(q = NAG_ALLOC(pdq * pdq, double)) ||
      !(z = NAG_ALLOC(pdz * pdz, double))) {
    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", &S(i, j));
  scanf("%*[^\n]");
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
      scanf("%lf", &T(i, j));
  scanf("%*[^\n]");

  if (wantq) {
    for (i = 1; i <= n; ++i)
      for (j = 1; j <= n; ++j)
        scanf("%lf", &Q(i, j));
    scanf("%*[^\n]");
  }
  if (wantz) {
    for (i = 1; i <= n; ++i)
      for (j = 1; j <= n; ++j)
        scanf("%lf", &Z(i, j));
    scanf("%*[^\n]");
  }

  /* Reorder the Schur factors S and T and update the matrices Q and Z */
  nag_lapackeig_dtgsen(order, ijob, wantq, wantz, select, n, s, pds, t, pdt,
                       alphar, alphai, beta, q, pdq, z, pdz, &m, &pl, &pr, dif,
                       &fail);

  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_dtgsen (f08ygc).\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);
    if ((fabs(alphar[j]) + fabs(alphai[j])) * small >= fabs(beta[j]))
      printf("  infinite or undetermined, alpha = (%13.4e, %13.4e), "
             "beta = %13.4e\n",
             alphar[j], alphai[j], beta[j]);
    else if (alphai[j] == 0.0)
      printf("  %12.4e\n", alphar[j] / beta[j]);
    else
      printf("  (%13.4e, %13.4e)\n", alphar[j] / beta[j], alphai[j] / beta[j]);
  }

  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(alphai);
  NAG_FREE(alphar);
  NAG_FREE(beta);
  NAG_FREE(select);
  NAG_FREE(q);
  NAG_FREE(z);

  return exit_status;
}