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

NAG AD Library Introduction
Example description
/* F07CE_P0W_F C++ Header Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 * Mark 29.0, 2023.
 */

#include <iostream>
#include <nag.h>
#include <nagad.h>
#include <nagx04.h>
#include <stdio.h>
#include <string>
using namespace std;

int main()
{
  int               exit_status = 0;
  nag::ad::handle_t ad_handle;
  Integer           nrhs = 1, ifail = 0;

  cout << "F07CE_P0W_F C++ Header Example Program Results\n\n";
  // Skip heading in data file
  string mystr;
  getline(cin, mystr);

  // Read problem size and number of right-hand-sides
  Integer n;
  cin >> n;
  cin >> nrhs;

  // Allocate arrays containing A and its factorized form, B
  // and the solution X.
  double * dl = 0, *d = 0, *du = 0, *du2 = 0, *x = 0;
  Integer *ipiv = 0;
  Integer  n1 = n - 1, n2 = n - 2;
  dl   = new double[n1];
  d    = new double[n];
  du   = new double[n1];
  du2  = new double[n2];
  ipiv = new Integer[n];
  x    = new double[n * nrhs];

  // Read the tridiagonal matrix A and right hand side B, register and copy
  for (int i = 0; i < n1; i++)
  {
    cin >> du[i];
  }
  for (int i = 0; i < n; i++)
  {
    cin >> d[i];
  }
  for (int i = 0; i < n1; i++)
  {
    cin >> dl[i];
  }
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < nrhs; j++)
    {
      int k = i + j * n;
      cin >> x[k];
    }
  }

  // Factorize the tridiagonal matrix A
  ifail = 0;
  nag::ad::f07cd(ad_handle, n, dl, d, du, du2, ipiv, ifail);

  // Solve the equations Ax = b for x
  ifail = 0;
  nag::ad::f07ce(ad_handle, "N", n, nrhs, dl, d, du, du2, ipiv, x, n, ifail);

  cout << "\n\n";
  NagError fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, nrhs, x, n,
         "  Solution", 0, &fail);

  delete[] dl;
  delete[] d;
  delete[] du;
  delete[] du2;
  delete[] ipiv;
  delete[] x;
  return exit_status;
}