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

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

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

int main(void) {
  /* Scalars */
  Complex alpha, beta;
  double norm;
  Integer i, j, n, pdc, pdd, pdh, pdz, w_len;
  Integer exit_status = 0;
  NagError fail;
  Nag_OrderType order;
  /* Arrays */
  Complex *c = 0, *d = 0, *h = 0, *w = 0, *z = 0;

#ifdef NAG_COLUMN_MAJOR
#define H(I, J) h[(J - 1) * pdh + I - 1]
#define D(I, J) d[(J - 1) * pdd + I - 1]
  order = Nag_ColMajor;
#else
#define H(I, J) h[(I - 1) * pdh + J - 1]
#define D(I, J) d[(I - 1) * pdd + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_zhseqr (f08psc) Example Program Results\n\n");

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

  /* Allocate memory */
  if (!(c = NAG_ALLOC(n * n, Complex)) || !(d = NAG_ALLOC(n * n, Complex)) ||
      !(h = NAG_ALLOC(n * n, Complex)) || !(w = NAG_ALLOC(w_len, Complex)) ||
      !(z = NAG_ALLOC(n * n, Complex))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read H from data file */
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j)
      scanf(" ( %lf , %lf )", &H(i, j).re, &H(i, j).im);
  }
  scanf("%*[^\n] ");

  /* Copy H into D */
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j) {
      D(i, j).re = H(i, j).re;
      D(i, j).im = H(i, j).im;
    }
  }

  /* nag_file_print_matrix_complex_gen_comp (x04dbc): Print matrix H */
  fflush(stdout);
  nag_file_print_matrix_complex_gen_comp(
      order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, h, pdh, Nag_BracketForm,
      "%7.4f", "Matrix H", 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;
  }

  /* Calculate the eigenvalues and Schur factorization of H */
  /* nag_lapackeig_zhseqr (f08psc).
   * Eigenvalues and Schur factorization of complex upper
   * Hessenberg matrix reduced from complex general matrix
   */
  nag_lapackeig_zhseqr(order, Nag_Schur, Nag_InitZ, n, 1, n, h, pdh, w, z, pdz,
                       &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_zhseqr (f08psc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* nag_blast_zgemm (f16zac): Compute A - Z*T*Z^H from the factorization of */
  /* A and store in matrix D */
  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, z, pdz, h,
                  pdh, 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, z,
                  pdz, beta, d, pdd, &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 D and print warning if */
  /* it is too large */
  nag_blast_zge_norm(order, Nag_OneNorm, n, n, d, pdd, &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 H-(Z*T*Z^H) is much greater than 0.",
           " Schur factorization has failed.");
  } else {
    printf(" Eigenvalues\n");
    for (i = 1; i <= n; ++i)
      printf("(%7.4f,%7.4f) ", w[i - 1].re, w[i - 1].im);
    printf("\n\n");
  }

END:
  NAG_FREE(c);
  NAG_FREE(d);
  NAG_FREE(h);
  NAG_FREE(w);
  NAG_FREE(z);

  return exit_status;
}