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

NAG AD Library Introduction
Example description
#include "dco.hpp"
/* F07CD_A1W_F C++ Header Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 * Mark 30.0, 2024.
 */

#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 << "F07CD_A1W_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.
  nagad_a1w_w_rtype *dl = 0, *d = 0, *du = 0, *du2 = 0, *b = 0;
  nagad_a1w_w_rtype *dlf = 0, *df = 0, *duf = 0, *x = 0;
  double *           sol  = 0;
  Integer *          ipiv = 0;
  Integer            n1 = n - 1, n2 = n - 2;
  if (!(dl = NAG_ALLOC(n1, nagad_a1w_w_rtype)) ||
      !(d = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
      !(du = NAG_ALLOC(n1, nagad_a1w_w_rtype)) ||
      !(du2 = NAG_ALLOC(n2, nagad_a1w_w_rtype)) ||
      !(dlf = NAG_ALLOC(n1, nagad_a1w_w_rtype)) ||
      !(df = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
      !(duf = NAG_ALLOC(n1, nagad_a1w_w_rtype)) ||
      !(b = NAG_ALLOC(n * nrhs, nagad_a1w_w_rtype)) ||
      !(ipiv = NAG_ALLOC(n, Integer)) ||
      !(x = NAG_ALLOC(n * n, nagad_a1w_w_rtype)) ||
      !(sol = NAG_ALLOC(n * n, double)))
  {
    cout << "Allocation failure\n";
    exit_status = -1;
    goto END;
  }

  // Create AD tape
  dco::ga1s<double>::global_tape = dco::ga1s<double>::tape_t::create();

  // Read the tridiagonal matrix A and right hand side B, register and copy
  double dd;
  for (int i = 0; i < n1; i++)
  {
    cin >> dd;
    du[i] = dd;
    dco::ga1s<double>::global_tape->register_variable(du[i]);
    duf[i] = du[i];
  }
  for (int i = 0; i < n; i++)
  {
    cin >> dd;
    d[i] = dd;
    dco::ga1s<double>::global_tape->register_variable(d[i]);
    df[i] = d[i];
  }
  for (int i = 0; i < n1; i++)
  {
    cin >> dd;
    dl[i] = dd;
    dco::ga1s<double>::global_tape->register_variable(dl[i]);
    dlf[i] = dl[i];
  }
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < nrhs; j++)
    {
      cin >> dd;
      int k = i + j * n;
      b[k]  = dd;
      dco::ga1s<double>::global_tape->register_variable(b[k]);
      x[k] = b[k];
    }
  }

  // Create AD configuration data object
  ifail = 0;

  // Factorize the tridiagonal matrix A
  ifail = 0;
  nag::ad::f07cd(ad_handle, n, dlf, df, duf, du2, ipiv, ifail);

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

  // Print primal solution
  for (int i = 0; i < n * nrhs; i++)
  {
    sol[i] = dco::value(x[i]);
  }
  cout << "\n\n";
  NagError fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, nrhs, sol, n,
         "  Solution", 0, &fail);

  cout << "\n\n Derivatives calculated: First order adjoints\n";
  cout << " Computational mode    : algorithmic\n";
  cout << "\n Derivatives of first solution column w.r.t. inputs:\n";

  // Obtain derivatives for each output solution point

  cout.setf(ios::scientific, ios::floatfield);
  cout.setf(ios::right);
  cout.precision(2);
  for (int i = 0; i < n; i++)
  {
    cout << "\n  Solution point " << i + 1 << endl;

    // Reset adjoints, initialize derivative, and evaluate adjoint
    dco::ga1s<double>::global_tape->zero_adjoints();
    double inc = 1.0;
    dco::derivative(x[i]) += inc;
    dco::ga1s<double>::global_tape->sparse_interpret() = true;
    dco::ga1s<double>::global_tape->interpret_adjoint();
    if (ifail != 0)
    {
      exit_status = 3;
      goto END;
    }

    cout << "      dx/d(du) : ";
    cout.width(10);
    cout << " ";
    for (int j = 0; j < n1; j++)
    {
      double dd = dco::derivative(du[j]);
      cout.width(10);
      cout << dd;
    }

    cout << "\n      dx/d(d)  : ";
    for (int j = 0; j < n; j++)
    {
      double dd = dco::derivative(d[j]);
      cout.width(10);
      cout << dd;
    }

    cout << "\n      dx/d(dl) : ";
    for (int j = 0; j < n1; j++)
    {
      double dd = dco::derivative(dl[j]);
      cout.width(10);
      cout << dd;
    }
    cout << "\n      dx/d(b)  : ";
    for (int j = 0; j < n; j++)
    {
      double dd = dco::derivative(b[j]);
      cout.width(10);
      cout << dd;
    }
    cout << endl;
  }

END:

  ifail = 0;

  dco::ga1s<double>::tape_t::remove(dco::ga1s<double>::global_tape);

  return exit_status;
}