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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_zhbgst (f08usc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.1, 2024.
 */

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

int main(void) {
  /* Scalars */
  Integer i, j, k1, k2, ka, kb, n, pdab, pdbb, pdx, d_len, e_len;
  Integer exit_status = 0;
  NagError fail;
  Nag_UploType uplo;
  Nag_OrderType order;
  /* Arrays */
  char nag_enum_arg[40];
  Complex *ab = 0, *bb = 0, *x = 0;
  double *d = 0, *e = 0;

#ifdef NAG_COLUMN_MAJOR
#define AB_UPPER(I, J) ab[(J - 1) * pdab + k1 + I - J - 1]
#define AB_LOWER(I, J) ab[(J - 1) * pdab + I - J]
#define BB_UPPER(I, J) bb[(J - 1) * pdbb + k2 + I - J - 1]
#define BB_LOWER(I, J) bb[(J - 1) * pdbb + I - J]
  order = Nag_ColMajor;
#else
#define AB_UPPER(I, J) ab[(I - 1) * pdab + J - I]
#define AB_LOWER(I, J) ab[(I - 1) * pdab + k1 + J - I - 1]
#define BB_UPPER(I, J) bb[(I - 1) * pdbb + J - I]
#define BB_LOWER(I, J) bb[(I - 1) * pdbb + k2 + J - I - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_zhbgst (f08usc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &n, &ka, &kb);
  pdab = ka + 1;
  pdbb = kb + 1;
  pdx = n;
  d_len = n;
  e_len = n - 1;

  /* Allocate memory */
  if (!(ab = NAG_ALLOC(pdab * n, Complex)) ||
      !(bb = NAG_ALLOC(pdbb * n, Complex)) || !(d = NAG_ALLOC(d_len, double)) ||
      !(e = NAG_ALLOC(e_len, double)) || !(x = NAG_ALLOC(n * n, Complex))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /* Read whether Upper or Lower part of A is stored */
  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);
  /* Read A and B from data file */
  k1 = ka + 1;
  k2 = kb + 1;
  if (uplo == Nag_Upper) {
    for (i = 1; i <= n; ++i) {
      for (j = i; j <= MIN(i + ka, n); ++j) {
        scanf(" ( %lf , %lf ) ", &AB_UPPER(i, j).re, &AB_UPPER(i, j).im);
      }
    }
    scanf("%*[^\n] ");
  } else {
    for (i = 1; i <= n; ++i) {
      for (j = MAX(1, i - ka); j <= i; ++j) {
        scanf(" ( %lf , %lf ) ", &AB_LOWER(i, j).re, &AB_LOWER(i, j).im);
      }
    }
    scanf("%*[^\n] ");
  }
  if (uplo == Nag_Upper) {
    for (i = 1; i <= n; ++i) {
      for (j = i; j <= MIN(i + kb, n); ++j) {
        scanf(" ( %lf, %lf ) ", &BB_UPPER(i, j).re, &BB_UPPER(i, j).im);
      }
    }
    scanf("%*[^\n] ");
  } else {
    for (i = 1; i <= n; ++i) {
      for (j = MAX(1, i - kb); j <= i; ++j) {
        scanf(" ( %lf, %lf ) ", &BB_LOWER(i, j).re, &BB_LOWER(i, j).im);
      }
    }
    scanf("%*[^\n] ");
  }
  /* Compute the split Cholesky factorization of B */
  /* nag_lapackeig_zpbstf (f08utc).
   * Computes a split Cholesky factorization of complex
   * Hermitian positive-definite band matrix A
   */
  nag_lapackeig_zpbstf(order, uplo, n, kb, bb, pdbb, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_zpbstf (f08utc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Reduce the problem to standard form C*y = lambda*y, */
  /* storing the result in A */
  /* nag_lapackeig_zhbgst (f08usc).
   * Reduction of complex Hermitian-definite banded
   * generalized eigenproblem Ax = lambda Bx to standard form
   * Cy = lambda y, such that C has the same bandwidth as A
   */
  nag_lapackeig_zhbgst(order, Nag_DoNotForm, uplo, n, ka, kb, ab, pdab, bb,
                       pdbb, x, pdx, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_zhbgst (f08usc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Reduce C to tridiagonal form T = (Q^T)*C*Q */
  /* nag_lapackeig_zhbtrd (f08hsc).
   * Unitary reduction of complex Hermitian band matrix to
   * real symmetric tridiagonal form
   */
  nag_lapackeig_zhbtrd(order, Nag_DoNotForm, uplo, n, ka, ab, pdab, d, e, x,
                       pdx, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_zhbtrd (f08hsc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Calculate the eigenvalues of T (same as C) */
  /* nag_lapackeig_dsterf (f08jfc).
   * All eigenvalues of real symmetric tridiagonal matrix,
   * root-free variant of QL or QR
   */
  nag_lapackeig_dsterf(n, d, e, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_dsterf (f08jfc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Print eigenvalues */
  printf(" Eigenvalues\n");
  for (i = 0; i < n; ++i)
    printf(" %8.4f", d[i]);
  printf("\n");
END:
  NAG_FREE(ab);
  NAG_FREE(bb);
  NAG_FREE(d);
  NAG_FREE(e);
  NAG_FREE(x);
  return exit_status;
}