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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_dsbgst (f08uec) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 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];
  double *ab = 0, *bb = 0, *d = 0, *e = 0, *x = 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_dsbgst (f08uec) 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, double)) ||
      !(bb = NAG_ALLOC(pdbb * n, double)) || !(d = NAG_ALLOC(d_len, double)) ||
      !(e = NAG_ALLOC(e_len, double)) || !(x = NAG_ALLOC(n * n, double))) {
    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", &AB_UPPER(i, j));
    }
    scanf("%*[^\n] ");
  } else {
    for (i = 1; i <= n; ++i) {
      for (j = MAX(1, i - ka); j <= i; ++j)
        scanf("%lf", &AB_LOWER(i, j));
    }
    scanf("%*[^\n] ");
  }
  if (uplo == Nag_Upper) {
    for (i = 1; i <= n; ++i) {
      for (j = i; j <= MIN(i + kb, n); ++j)
        scanf("%lf", &BB_UPPER(i, j));
    }
    scanf("%*[^\n] ");
  } else {
    for (i = 1; i <= n; ++i) {
      for (j = MAX(1, i - kb); j <= i; ++j)
        scanf("%lf", &BB_LOWER(i, j));
    }
    scanf("%*[^\n] ");
  }
  /* Compute the split Cholesky factorization of B */
  /* nag_lapackeig_dpbstf (f08ufc).
   * Computes a split Cholesky factorization of real symmetric
   * positive-definite band matrix A
   */
  nag_lapackeig_dpbstf(order, uplo, n, kb, bb, pdbb, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_dpbstf (f08ufc).\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_dsbgst (f08uec).
   * Reduction of real symmetric-definite banded generalized
   * eigenproblem Ax = lambda Bx to standard form
   * Cy = lambda y, such that C has the same bandwidth as A
   */
  nag_lapackeig_dsbgst(order, Nag_DoNotForm, uplo, n, ka, kb, ab, pdab, bb,
                       pdbb, x, pdx, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_dsbgst (f08uec).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Reduce C to tridiagonal form T = (Q^T)*C*Q */
  /* nag_lapackeig_dsbtrd (f08hec).
   * Orthogonal reduction of real symmetric band matrix to
   * symmetric tridiagonal form
   */
  nag_lapackeig_dsbtrd(order, Nag_DoNotForm, uplo, n, ka, ab, pdab, d, e, x,
                       pdx, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_dsbtrd (f08hec).\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;
}