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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_dhseqr (f08pec) Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 *
 * Mark 28.7, 2022.
 */

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

int main(void) {
  /* Scalars */
  double alpha, beta, norm;
  Integer i, j, n, pdc, pdd, pdh, pdz, wi_len, wr_len;
  Integer exit_status = 0;
  NagError fail;
  Nag_OrderType order;
  /* Arrays */
  double *c = 0, *d = 0, *h = 0, *wi = 0, *wr = 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_dhseqr (f08pec) 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
  wr_len = n;
  wi_len = n;

  /* Allocate memory */
  if (!(c = NAG_ALLOC(n * n, double)) || !(d = NAG_ALLOC(n * n, double)) ||
      !(h = NAG_ALLOC(n * n, double)) || !(wi = NAG_ALLOC(wi_len, double)) ||
      !(wr = NAG_ALLOC(wr_len, double)) || !(z = NAG_ALLOC(n * n, double))) {
    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", &H(i, j));
  }
  scanf("%*[^\n] ");

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

  /* nag_file_print_matrix_real_gen (x04cac): Print Matrix H. */
  fflush(stdout);
  nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
                                 n, h, pdh, "Matrix A", 0, &fail);
  printf("\n");
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (x04cac).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Calculate the eigenvalues and Schur factorization of H */
  /* nag_lapackeig_dhseqr (f08pec).
   * Eigenvalues and Schur factorization of real upper
   * Hessenberg matrix reduced from real general matrix
   */
  nag_lapackeig_dhseqr(order, Nag_Schur, Nag_InitZ, n, 1, n, h, pdh, wr, wi, z,
                       pdz, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_dhseqr (f08pec).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_blast_dgemm (f16yac): Compute H - Z*T*Z^T from the factorization of */
  /* H and store in matrix D */
  alpha = 1.0;
  beta = 0.0;
  nag_blast_dgemm(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_dgemm (f16yac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  alpha = -1.0;
  beta = 1.0;
  nag_blast_dgemm(order, Nag_NoTrans, Nag_Trans, n, n, n, alpha, c, pdc, z, pdz,
                  beta, d, pdd, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_blast_dgemm (f16yac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_blast_dge_norm (f16rac): Find norm of matrix D and print warning if */
  /* it is too large */
  nag_blast_dge_norm(order, Nag_OneNorm, n, n, d, pdd, &norm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_blast_dge_norm (f16rac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  if (norm > pow(x02ajc(), 0.8)) {
    printf("\n%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(" (%8.4f,%8.4f)", wr[i - 1], wi[i - 1]);
    printf("\n");
  }

END:
  NAG_FREE(c);
  NAG_FREE(d);
  NAG_FREE(h);
  NAG_FREE(wi);
  NAG_FREE(wr);
  NAG_FREE(z);

  return exit_status;
}