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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_zhpgvd (f08tqc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 */

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

int main(void) {
  /* Scalars */
  double anorm, bnorm, eps, rcond, rcondb, t1;
  Integer i, j, n;
  Integer exit_status = 0;
  /* Arrays */
  Complex *ap = 0, *bp = 0;
  Complex dummy[1];
  double *eerbnd = 0, *w = 0;
  char nag_enum_arg[40];

  /* Nag Types */
  NagError fail;
  Nag_OrderType order;
  Nag_UploType uplo;

#ifdef NAG_COLUMN_MAJOR
#define A_UPPER(I, J) ap[J * (J - 1) / 2 + I - 1]
#define A_LOWER(I, J) ap[(2 * n - J) * (J - 1) / 2 + I - 1]
#define B_UPPER(I, J) bp[J * (J - 1) / 2 + I - 1]
#define B_LOWER(I, J) bp[(2 * n - J) * (J - 1) / 2 + I - 1]
  order = Nag_ColMajor;
#else
#define A_UPPER(I, J) ap[(2 * n - I) * (I - 1) / 2 + J - 1]
#define A_LOWER(I, J) ap[I * (I - 1) / 2 + J - 1]
#define B_UPPER(I, J) bp[(2 * n - I) * (I - 1) / 2 + J - 1]
#define B_LOWER(I, J) bp[I * (I - 1) / 2 + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_zhpgvd (f08tqc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%*[^\n]", &n);
  if (n < 0) {
    printf("Invalid n\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
   */
  uplo = (Nag_UploType)nag_enum_name_to_value(nag_enum_arg);

  /* Allocate memory */
  if (!(ap = NAG_ALLOC(n * (n + 1) / 2, Complex)) ||
      !(bp = NAG_ALLOC(n * (n + 1) / 2, Complex)) ||
      !(eerbnd = NAG_ALLOC(n, double)) || !(w = NAG_ALLOC(n, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read the triangular parts of the matrices A and B from data file. */
  if (uplo == Nag_Upper) {
    for (i = 1; i <= n; ++i)
      for (j = i; j <= n; ++j)
        scanf(" ( %lf , %lf )", &A_UPPER(i, j).re, &A_UPPER(i, j).im);
    scanf("%*[^\n]");
    for (i = 1; i <= n; ++i)
      for (j = i; j <= n; ++j)
        scanf(" ( %lf , %lf )", &B_UPPER(i, j).re, &B_UPPER(i, j).im);
  } else if (uplo == Nag_Lower) {
    for (i = 1; i <= n; ++i)
      for (j = 1; j <= i; ++j)
        scanf(" ( %lf , %lf )", &A_LOWER(i, j).re, &A_LOWER(i, j).im);
    scanf("%*[^\n]");
    for (i = 1; i <= n; ++i)
      for (j = 1; j <= i; ++j)
        scanf(" ( %lf , %lf )", &B_LOWER(i, j).re, &B_LOWER(i, j).im);
  }
  scanf("%*[^\n]");

  /* Compute the one-norms of the symmetric matrices A and B
   * using nag_blast_zhp_norm (f16udc).
   */
  nag_blast_zhp_norm(order, Nag_OneNorm, uplo, n, ap, &anorm, &fail);
  nag_blast_zhp_norm(order, Nag_OneNorm, uplo, n, bp, &bnorm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_blast_zhp_norm (f16udc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Solve the generalized symmetric eigenvalue problem
   * A*B*x = lambda*x (itype = 2) using nag_lapackeig_zhpgvd (f08tqc).
   */
  nag_lapackeig_zhpgvd(order, 2, Nag_EigVals, uplo, n, ap, bp, w, dummy, 1,
                       &fail);

  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_zhpgvd (f08tqc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print eigensolution */
  printf("Eigenvalues\n   ");
  for (j = 0; j < n; ++j)
    printf(" %11.4f%s", w[j], j % 6 == 5 ? "\n" : "");
  printf("\n\n");

  /* Estimate the reciprocal condition number of the Cholesky factor of B.
   * nag_lapacklin_ztpcon (f07uuc).
   * Note that: cond(B) = 1/(rcond*rcond).
   */
  nag_lapacklin_ztpcon(order, Nag_OneNorm, uplo, Nag_NonUnitDiag, n, bp, &rcond,
                       &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapacklin_ztpcon (f07uuc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print the reciprocal condition number of B */
  rcondb = rcond * rcond;
  printf("Estimate of reciprocal condition number for B\n    %12.1e\n", rcondb);

  /* Get the machine precision, using nag_machine_precision (x02ajc) */
  eps = nag_machine_precision;
  if (rcond < eps) {
    printf("\nB is very ill-conditioned, error estimates have not been"
           " computed.\n");
    goto END;
  }

  t1 = anorm * bnorm;
  for (i = 0; i < n; ++i)
    eerbnd[i] = eps * (t1 + fabs(w[i]) / rcondb);

  /* Print the approximate error bounds for the eigenvalues */
  printf("\nError estimates for the eigenvalues\n    ");
  for (i = 0; i < n; ++i)
    printf(" %11.1e%s", eerbnd[i], i % 6 == 5 ? "\n" : "");
  printf("\n");

END:
  NAG_FREE(ap);
  NAG_FREE(bp);
  NAG_FREE(eerbnd);
  NAG_FREE(w);

  return exit_status;
}