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

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

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

int main(void) {

  /* Scalars */
  double anorm, rcond;
  Integer exit_status = 0, i, n;

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

  /* Nag Types */
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_lapacklin_dgtcon (f07cgc) 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;
  }
  /* Allocate memory */
  if (!(d = NAG_ALLOC(n, double)) || !(dl = NAG_ALLOC(n - 1, double)) ||
      !(du = NAG_ALLOC(n - 1, double)) || !(du2 = NAG_ALLOC(n - 2, double)) ||
      !(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", &du[i]);
  scanf("%*[^\n]");
  for (i = 0; i < n; ++i)
    scanf("%lf", &d[i]);
  scanf("%*[^\n]");
  for (i = 0; i < n - 1; ++i)
    scanf("%lf", &dl[i]);
  scanf("%*[^\n]");

  /* Compute the 1-norm of A */
  if (n == 1) {
    anorm = ABS(d[0]);
  } else {
    anorm = MAX(ABS(d[0]) + ABS(dl[0]), ABS(d[n - 1]) + ABS(du[n - 2]));
    for (i = 1; i < n - 1; ++i)
      anorm = MAX(anorm, ABS(d[i]) + ABS(dl[i]) + ABS(du[i - 1]));
  }

  /* Factorize the tridiagonal matrix A using nag_lapacklin_dgttrf (f07cdc). */
  nag_lapacklin_dgttrf(n, dl, d, du, du2, ipiv, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapacklin_dgttrf (f07cdc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Estimate the condition number of A using nag_lapacklin_dgtcon (f07cgc). */
  nag_lapacklin_dgtcon(Nag_OneNorm, n, dl, d, du, du2, ipiv, anorm, &rcond,
                       &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapacklin_dgtcon (f07cgc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print the estimated condition number if A non-singular */
  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;
}