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

NAG CL Interface Introduction
Example description
/* nag_lapacklin_zgtcon (f07cuc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.3, 2024.
 *
 */
#include <math.h>
#include <nag.h>
#include <stdio.h>

int main(void) {
  /* Scalars */
  double anorm, rcond;
  Integer exit_status = 0, i, n;

  /* Arrays */
  Complex *d = 0, *dl = 0, *du = 0, *du2 = 0;
  Integer *ipiv = 0;

  /* Nag Types */
  NagError fail;

#define CABS(dl) sqrt(dl.re *dl.re + dl.im * dl.im)

  INIT_FAIL(fail);

  printf("nag_lapacklin_zgtcon (f07cuc) 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 = -2;
    goto END;
  }
  /* Allocate memory */
  if (!(d = NAG_ALLOC(n, Complex)) || !(dl = NAG_ALLOC(n - 1, Complex)) ||
      !(du = NAG_ALLOC(n - 1, Complex)) || !(du2 = NAG_ALLOC(n - 2, Complex)) ||
      !(ipiv = NAG_ALLOC(n, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read the tridiagonal matrix A from data file */
  for (i = 0; i < n - 1; ++i)
    scanf(" ( %lf , %lf )", &du[i].re, &du[i].im);
  scanf("%*[^\n]");
  for (i = 0; i < n; ++i)
    scanf(" ( %lf , %lf )", &d[i].re, &d[i].im);
  scanf("%*[^\n]");
  for (i = 0; i < n - 1; ++i)
    scanf(" ( %lf , %lf )", &dl[i].re, &dl[i].im);
  scanf("%*[^\n]");

  /* Compute the 1-norm of A */

  if (n == 1) {
    anorm = CABS(d[0]);
  } else {
    anorm = MAX(CABS(d[0]) + CABS(dl[0]), CABS(d[n - 1]) + CABS(du[n - 2]));
    for (i = 1; i < n - 1; ++i)
      anorm = MAX(anorm, CABS(d[i]) + CABS(dl[i]) + CABS(du[i - 1]));
  }
  /* nag_lapacklin_zgttrf (f07crc)
   * LU factorization of complex tridiagonal matrix A
   */
  nag_lapacklin_zgttrf(n, dl, d, du, du2, ipiv, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapacklin_zgttrf (f07crc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Estimate the condition number of A using
   * nag_lapacklin_zgtcon (f07cuc).
   * Estimates the reciprocal of the condition number of a LU factorized
   * complex tridiagonal matrix.
   */
  nag_lapacklin_zgtcon(Nag_OneNorm, n, dl, d, du, du2, ipiv, anorm, &rcond,
                       &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapacklin_zgtcon (f07cuc).\n%s\n", fail.message);
    exit_status = 2;
    goto END;
  }

  /* Print the estimated condition number */
  if (rcond >= nag_machine_precision)
    printf("Estimate of condition number = %11.2e\n\n", 1.0 / rcond);
  else
    printf("A is singular to working precision. RCOND = %11.2e\n\n", rcond);
END:
  NAG_FREE(d);
  NAG_FREE(dl);
  NAG_FREE(du);
  NAG_FREE(du2);
  NAG_FREE(ipiv);

  return exit_status;
}