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

NAG AD Library Introduction
Example description
/* F07CD_T1W_F C++ Header Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 * Mark 28.5, 2022.
 */

#include <dco.hpp>
#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;
  NagError          fail;
  INIT_FAIL(fail);

  cout << "F07CD_T1W_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_t1w_w_rtype *dl = 0, *d = 0, *du = 0, *du2 = 0, *b = 0;
  nagad_t1w_w_rtype *dlf = 0, *df = 0, *duf = 0, *x = 0;
  double *           sol = 0, *dxdu = 0, *dxdd = 0, *dxdl = 0, *dxdb = 0;
  Integer *          ipiv = 0;
  Integer            n1 = n - 1, n2 = n - 2;
  dl   = new nagad_t1w_w_rtype[n1];
  d    = new nagad_t1w_w_rtype[n];
  du   = new nagad_t1w_w_rtype[n1];
  du2  = new nagad_t1w_w_rtype[n2];
  dlf  = new nagad_t1w_w_rtype[n1];
  df   = new nagad_t1w_w_rtype[n];
  duf  = new nagad_t1w_w_rtype[n1];
  b    = new nagad_t1w_w_rtype[n * nrhs];
  ipiv = new Integer[n];
  x    = new nagad_t1w_w_rtype[n * n];
  sol  = new double[n * n];
  dxdu = new double[n * n1];
  dxdd = new double[n * n];
  dxdl = new double[n * n1];
  dxdb = new double[n * n];

  // 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;
  }
  for (int i = 0; i < n; i++)
  {
    cin >> dd;
    d[i] = dd;
  }
  for (int i = 0; i < n1; i++)
  {
    cin >> dd;
    dl[i] = dd;
  }
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < nrhs; j++)
    {
      cin >> dd;
      int k = i + j * n;
      b[k]  = dd;
    }
  }

  // Create AD configuration data object
  ifail = 0;

  double inc = 1.0, zero = 0.0;
  for (int i = 0; i < 4 * n - 2; ++i)
  {
    int k = i;
    if (i < n1)
    {
      dco::derivative(du[i]) = inc;
    }
    else if (i < n + n1)
    {
      k                     = i - n1;
      dco::derivative(d[k]) = inc;
    }
    else if (i < n + n1 + n1)
    {
      k                      = i - n - n1;
      dco::derivative(dl[k]) = inc;
    }
    else
    {
      k                     = i - n - n1 - n1;
      dco::derivative(b[k]) = inc;
    }
    for (int j = 0; j < n1; ++j)
    {
      dlf[j] = dl[j];
      df[j]  = d[j];
      duf[j] = du[j];
      x[j]   = b[j];
    }
    df[n1] = d[n1];
    x[n1]  = b[n1];
    // 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);

    if (i == 0)
    {
      // Print primal solution
      for (int j = 0; j < n * nrhs; ++j)
      {
        sol[j] = dco::value(x[j]);
      }
      cout << "\n\n";
      x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, nrhs, sol, n,
             "  Solution", 0, &fail);
    }
    if (i < n1)
    {
      dco::derivative(du[k]) = zero;
      for (int j = 0; j < n; ++j)
      {
        dxdu[j + k * n] = dco::derivative(x[j]);
      }
    }
    else if (i < n + n1)
    {
      dco::derivative(d[k]) = zero;
      for (int j = 0; j < n; ++j)
      {
        dxdd[j + k * n] = dco::derivative(x[j]);
      }
    }
    else if (i < n + n1 + n1)
    {
      dco::derivative(dl[k]) = zero;
      for (int j = 0; j < n; ++j)
      {
        dxdl[j + k * n] = dco::derivative(x[j]);
      }
    }
    else
    {
      dco::derivative(b[k]) = zero;
      for (int j = 0; j < n; ++j)
      {
        dxdb[j + k * n] = dco::derivative(x[j]);
      }
    }
  }
  cout << "\n\n Derivatives calculated: First order tangents\n";
  cout << " Computational mode    : algorithmic\n";
  cout << "\n Derivatives of first solution column w.r.t. inputs:\n";

  cout << "\n";
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n1, dxdu, n,
         "  d(du(i))/dx(j)", 0, &fail);
  cout << "\n";
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, dxdd, n,
         "  d(d(i))/dx(j)", 0, &fail);
  cout << "\n";
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n1, dxdl, n,
         "  d(dl(i))/dx(j)", 0, &fail);
  cout << "\n";
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, dxdb, n,
         "  d(b(i))/dx(j)", 0, &fail);

  ifail = 0;

  delete[] dl;
  delete[] d;
  delete[] du;
  delete[] du2;
  delete[] dlf;
  delete[] df;
  delete[] duf;
  delete[] b;
  delete[] ipiv;
  delete[] x;
  delete[] sol;
  delete[] dxdu;
  delete[] dxdd;
  delete[] dxdl;
  delete[] dxdb;
  return exit_status;
}