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

NAG CL Interface Introduction
Example description
/* nag_lapacklin_dgttrf (f07cdc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 */

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

int main(void) {
  /* Scalars */
  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_dgttrf (f07cdc) 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]");

  /* 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;
  }
  /* Print details of the factorization */
  printf("Details of factorization\n\n");
  printf(" Second superdiagonal of U\n");
  for (i = 0; i < n - 2; ++i)
    printf("%9.4f%s", du2[i], i % 8 == 7 ? "\n" : " ");
  printf("\n\n First superdiagonal of U\n");
  for (i = 0; i < n - 1; ++i)
    printf("%9.4f%s", du[i], i % 8 == 7 ? "\n" : " ");
  printf("\n\n Main diagonal of U\n");
  for (i = 0; i < n; ++i)
    printf("%9.4f%s", d[i], i % 8 == 7 ? "\n" : " ");

  printf("\n\n Multipliers\n");
  for (i = 0; i < n - 1; ++i)
    printf("%9.4f%s", dl[i], i % 8 == 7 ? "\n" : " ");
  printf("\n\n Vector of interchanges\n");
  for (i = 0; i < n; ++i)
    printf("%9" NAG_IFMT "%s", ipiv[i], i % 5 == 4 ? "\n" : " ");
  printf("\n");
END:
  NAG_FREE(d);
  NAG_FREE(dl);
  NAG_FREE(du);
  NAG_FREE(du2);
  NAG_FREE(ipiv);
  return exit_status;
}