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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_ztrsen (f08quc) Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.2, 2021.
 */

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

int main(void) {
  /* Scalars */
  Integer i, j, m, n, pda, pdc, pdq, pdt, select_len, w_len;
  Integer exit_status = 0;
  double norm, s, sep;
  Complex alpha, beta;
  NagError fail;
  Nag_OrderType order;
  /* Arrays */
  Complex *a = 0, *c = 0, *q = 0, *t = 0, *w = 0;
  char nag_enum_arg[40];
  Nag_Boolean *select = 0;

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

  INIT_FAIL(fail);

  printf("nag_lapackeig_ztrsen (f08quc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &n);
#ifdef NAG_COLUMN_MAJOR
  pda = n;
  pdc = n;
  pdq = n;
  pdt = n;
#else
  pda = n;
  pdc = n;
  pdq = n;
  pdt = n;
#endif
  w_len = n;
  select_len = n;

  /* Allocate memory */
  if (!(a = NAG_ALLOC(n * n, Complex)) || !(c = NAG_ALLOC(n * n, Complex)) ||
      !(q = NAG_ALLOC(n * n, Complex)) || !(w = NAG_ALLOC(w_len, Complex)) ||
      !(select = NAG_ALLOC(select_len, Nag_Boolean)) ||
      !(t = NAG_ALLOC(n * n, Complex))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read T and Q from data file */
  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] ");
  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] ");
  for (i = 0; i < n; ++i) {
    scanf("%39s ", nag_enum_arg);
    /* nag_enum_name_to_value (x04nac).
     * Converts NAG enum member name to value
     */
    select[i] = (Nag_Boolean)nag_enum_name_to_value(nag_enum_arg);
  }
  scanf("%*[^\n] ");

  /* nag_blast_zgemm (f16zac): Compute A = Q*T*Q^H */
  alpha.re = 1.0;
  alpha.im = 0.0;
  beta.re = 0.0;
  beta.im = 0.0;
  nag_blast_zgemm(order, Nag_NoTrans, Nag_NoTrans, n, n, n, alpha, q, pdq, t,
                  pdt, beta, c, pdc, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_blast_zgemm (f16zac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  nag_blast_zgemm(order, Nag_NoTrans, Nag_ConjTrans, n, n, n, alpha, c, pdc, q,
                  pdq, beta, a, pda, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_blast_zgemm (f16zac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_file_print_matrix_complex_gen_comp (x04dbc): Print matrix A */
  fflush(stdout);
  nag_file_print_matrix_complex_gen_comp(
      order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, a, pda, Nag_BracketForm,
      "%7.4f", "Matrix A", Nag_IntegerLabels, 0, Nag_IntegerLabels, 0, 80, 0, 0,
      &fail);
  printf("\n");
  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;
  }

  /* Reorder the Schur factorization T */
  /* nag_lapackeig_ztrsen (f08quc).
   * Reorder Schur factorization of complex matrix, form
   * orthonormal basis of right invariant subspace for
   * selected eigenvalues, with estimates of sensitivities
   */
  nag_lapackeig_ztrsen(order, Nag_DoBoth, Nag_UpdateSchur, select, n, t, pdt, q,
                       pdq, w, &m, &s, &sep, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_ztrsen (f08quc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_blast_zgemm (f16zac): Compute A - Qt*Tt*Qt^H from the reordered */
  /* Q and T */
  alpha.re = 1.0;
  alpha.im = 0.0;
  beta.re = 0.0;
  beta.im = 0.0;
  nag_blast_zgemm(order, Nag_NoTrans, Nag_NoTrans, n, n, n, alpha, q, pdq, t,
                  pdt, beta, c, pdc, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_blast_zgemm (f16zac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  alpha.re = -1.0;
  beta.re = 1.0;
  nag_blast_zgemm(order, Nag_NoTrans, Nag_ConjTrans, n, n, n, alpha, c, pdc, q,
                  pdq, beta, a, pda, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_blast_zgemm (f16zac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_blast_zge_norm (f16uac): Find norm of matrix A and print warning if */
  /* it is too large */
  nag_blast_zge_norm(order, Nag_OneNorm, n, n, a, pda, &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;
  }
  if (norm > pow(x02ajc(), 0.8)) {
    printf("%s\n%s\n", "Norm of A-(Qt*Tt*Qt^H) is much greater than 0.",
           "Schur factorization has failed.");
  } else {
    /* Print condition number estimates */
    printf(" Condition number estimate of the selected cluster of"
           " eigenvalues = %11.2e\n",
           1.0 / s);
    printf("\n Condition number estimate of the specified invariant"
           " subspace = %11.2e\n",
           1.0 / sep);
  }

END:
  NAG_FREE(a);
  NAG_FREE(c);
  NAG_FREE(q);
  NAG_FREE(t);
  NAG_FREE(w);
  NAG_FREE(select);

  return exit_status;
}