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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_zunmhr (f08nuc) Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 *
 * Mark 28.3, 2022.
 */

#include <nag.h>
#include <stdio.h>
int main(void) {
  /* Scalars */
  Integer i, j, m, n, pda, pdh, pdvl, pdvr, pdz;
  Integer tau_len, ifaill_len, select_len, w_len;
  Integer exit_status = 0;
  double thresh;
  NagError fail;
  Nag_OrderType order;
  /* Arrays */
  Complex *a = 0, *h = 0, *vl = 0, *vr = 0, *z = 0, *w = 0, *tau = 0;
  Integer *ifaill = 0, *ifailr = 0;
  Nag_Boolean *select = 0;

#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J - 1) * pda + I - 1]
#define H(I, J) h[(J - 1) * pdh + I - 1]
#define VR(I, J) vr[(J - 1) * pdvr + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J) a[(I - 1) * pda + J - 1]
#define H(I, J) h[(I - 1) * pdh + J - 1]
#define VR(I, J) vr[(I - 1) * pdvr + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_zunmhr (f08nuc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &n);

  pda = n;
  pdh = n;
  pdvl = n;
  pdvr = n;
  pdz = 1;
  tau_len = n;
  w_len = n;
  ifaill_len = n;
  select_len = n;

  /* Allocate memory */
  if (!(a = NAG_ALLOC(n * n, Complex)) || !(h = NAG_ALLOC(n * n, Complex)) ||
      !(vl = NAG_ALLOC(n * n, Complex)) || !(vr = NAG_ALLOC(n * n, Complex)) ||
      !(z = NAG_ALLOC(1 * 1, Complex)) || !(w = NAG_ALLOC(w_len, Complex)) ||
      !(ifaill = NAG_ALLOC(ifaill_len, Integer)) ||
      !(ifailr = NAG_ALLOC(ifaill_len, Integer)) ||
      !(select = NAG_ALLOC(select_len, Nag_Boolean)) ||
      !(tau = NAG_ALLOC(tau_len, Complex))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /* Read A from data file */
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j)
      scanf(" ( %lf , %lf )", &A(i, j).re, &A(i, j).im);
  }
  scanf("%*[^\n] ");
  scanf("%lf%*[^\n] ", &thresh);

  /* Reduce A to upper Hessenberg form */
  /* nag_lapackeig_zgehrd (f08nsc).
   * Unitary reduction of complex general matrix to upper
   * Hessenberg form
   */
  nag_lapackeig_zgehrd(order, n, 1, n, a, pda, tau, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_zgehrd (f08nsc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

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

  /* Calculate the eigenvalues of H (same as A) */
  /* nag_lapackeig_zhseqr (f08psc).
   * Eigenvalues and Schur factorization of complex upper
   * Hessenberg matrix reduced from complex general matrix
   */
  nag_lapackeig_zhseqr(order, Nag_EigVals, Nag_NotZ, 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;
  }

  /* Print eigenvalues */
  printf(" Eigenvalues\n");
  for (i = 0; i < n; ++i)
    printf(" (%7.4f,%7.4f)", w[i].re, w[i].im);
  printf("\n");
  for (i = 0; i < n; ++i)
    select[i] = w[i].re < thresh ? Nag_TRUE : Nag_FALSE;
  /* Calculate the eigenvectors of H (as specified by SELECT), */
  /* storing the result in VR */
  /* nag_lapackeig_zhsein (f08pxc).
   * Selected right and/or left eigenvectors of complex upper
   * Hessenberg matrix by inverse iteration
   */
  nag_lapackeig_zhsein(order, Nag_RightSide, Nag_HSEQRSource, Nag_NoVec, select,
                       n, a, pda, w, vl, pdvl, vr, pdvr, n, &m, ifaill, ifailr,
                       &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_zhsein (f08pxc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Calculate the eigenvectors of A = Q * VR */
  /* nag_lapackeig_zunmhr (f08nuc).
   * Apply unitary transformation matrix from reduction to
   * Hessenberg form determined by nag_lapackeig_zgehrd (f08nsc)
   */
  nag_lapackeig_zunmhr(order, Nag_LeftSide, Nag_NoTrans, n, m, 1, n, a, pda,
                       tau, vr, pdvr, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_zunmhr (f08nuc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Normalize the eigenvectors */
  for (j = 1; j <= m; j++) {
    for (i = n; i >= 1; i--) {
      VR(i, j) = nag_complex_divide(VR(i, j), VR(1, j));
    }
  }

  /* Print Eigenvectors */
  printf("\n");
  /* nag_file_print_matrix_complex_gen_comp (x04dbc).
   * Print complex general matrix (comprehensive)
   */
  fflush(stdout);
  nag_file_print_matrix_complex_gen_comp(
      order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, m, vr, pdvr,
      Nag_BracketForm, "%7.4f", "Contents of array VR", Nag_IntegerLabels, 0,
      Nag_IntegerLabels, 0, 80, 0, 0, &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(h);
  NAG_FREE(vl);
  NAG_FREE(vr);
  NAG_FREE(z);
  NAG_FREE(w);
  NAG_FREE(ifaill);
  NAG_FREE(ifailr);
  NAG_FREE(select);
  NAG_FREE(tau);
  return exit_status;
}