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

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

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

int main(void) {

  /* Scalars */
  double abnrm, eps, rcnd, tol;
  Integer exit_status = 0, i, ihi, ilo, j, n, pda, pdvr, pdvl;
  Complex eig;

  /* Arrays */
  double *a = 0, *rconde = 0, *rcondv = 0, *scale = 0, *vl = 0, *vr = 0;
  double *wi = 0, *wr = 0;

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

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

  INIT_FAIL(fail);

  printf("nag_lapackeig_dgeevx (f08nbc) Example Program Results\n");

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

  pda = n;
  pdvr = n;
  pdvl = n;
  /* Allocate memory */
  if (!(a = NAG_ALLOC(n * n, double)) || !(rconde = NAG_ALLOC(n, double)) ||
      !(rcondv = NAG_ALLOC(n, double)) || !(scale = NAG_ALLOC(n, double)) ||
      !(vl = NAG_ALLOC(n * n, double)) || !(vr = NAG_ALLOC(n * n, double)) ||
      !(wi = NAG_ALLOC(n, double)) || !(wr = NAG_ALLOC(n, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read the matrix A from data file */
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
      scanf("%lf", &A(i, j));
  scanf("%*[^\n]");

  /* Solve the eigenvalue problem using nag_lapackeig_dgeevx (f08nbc). */
  nag_lapackeig_dgeevx(order, Nag_BalanceBoth, Nag_LeftVecs, Nag_RightVecs,
                       Nag_RCondBoth, n, a, pda, wr, wi, vl, pdvl, vr, pdvr,
                       &ilo, &ihi, scale, &abnrm, rconde, rcondv, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_dgeevx (f08nbc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Compute the machine precision */
  eps = nag_machine_precision;
  tol = eps * abnrm;

  /* Print the eigenvalues/vectors, associated condition number and bounds. */
  for (j = 0; j < n; ++j) {
    /* Print information on j-th eigenvalue */
    printf("\n\nEigenvalue %3" NAG_IFMT "%14s= ", j + 1, "");
    if (wi[j] == 0.)
      printf("%12.4e\n", wr[j]);
    else
      printf("(%13.4e, %13.4e)\n", wr[j], wi[j]);

    rcnd = rconde[j];
    printf("\nReciprocal condition number = %9.1e\n", rcnd);
    if (rcnd > 0.0)
      printf("Error bound                 = %9.1e\n", tol / rcnd);
    else
      printf("Error bound is infinite\n");

    /* Normalize and print information on j-th eigenvector */
    printf("\nEigenvector %2" NAG_IFMT "\n", j + 1);
    if (wi[j] == 0.0)
      for (i = 0; i < n; ++i)
        printf("%29s%13.4e\n", "", VR(i, j) / VR(n - 1, j));
    else if (wi[j] > 0.)
      for (i = 0; i < n; ++i) {
        eig = nag_complex_divide(
            nag_complex_create(VR(i, j), VR(i, j + 1)),
            nag_complex_create(VR(n - 1, j), VR(n - 1, j + 1)));
        printf("%30s(%13.4e, %13.4e)\n", "", eig.re, eig.im);
      }
    else
      for (i = 0; i < n; ++i) {
        eig = nag_complex_divide(
            nag_complex_create(VR(i, j - 1), VR(i, j)),
            nag_complex_create(VR(n - 1, j - 1), VR(n - 1, j)));
        printf("%30s(%13.4e, %13.4e)\n", "", eig.re, -eig.im);
      }

    rcnd = rcondv[j];
    printf("\nReciprocal condition number = %9.1e\n", rcnd);
    if (rcnd > 0.0)
      printf("Error bound                 = %9.1e\n", tol / rcnd);
    else
      printf("Error bound is infinite\n");
  }

END:
  NAG_FREE(a);
  NAG_FREE(rconde);
  NAG_FREE(rcondv);
  NAG_FREE(scale);
  NAG_FREE(vl);
  NAG_FREE(vr);
  NAG_FREE(wi);
  NAG_FREE(wr);

  return exit_status;
}

#undef A
#undef VR