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

NAG AD Library Introduction
Example description
/* F07CD_P0W_F C++ Header Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 * Mark 30.0, 2024.
 */

#include <iostream>
#include <dco.hpp>
#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 << "F07CD_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];
  x    = new double[n * n];
  ipiv = new Integer[n];

  // Read the tridiagonal matrix A and right hand side B, register and copy
  double dd;
  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);

  // Print primal solution
  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[] x;
  delete[] ipiv;
  return exit_status;
}