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

NAG AD Library Introduction
Example description
/* F07CA_A1T1W_F C++ Header Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 * Mark 27.2, 2021.
 */

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

int main(void)
{
  int               exit_status = 0;
  void              *ad_handle = 0;
  Integer           nrhs = 1, ifail = 0;
  double            inc = 1.0;
  nagad_t1w_w_rtype dt;

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

  // Read number of x values and algorithmic mode
  Integer n, mode;
  cin >> n;
  cin >> mode;

  // Allocate arrays containing A and its factorized form, B
  // and the solution X.
  nagad_a1t1w_w_rtype *dl=0, *d=0, *du=0, *b=0;
  nagad_a1t1w_w_rtype *dlf=0, *df=0, *duf=0, *x=0;
  Integer n1 = n-1;

  dl  = new nagad_a1t1w_w_rtype [n1];
  d   = new nagad_a1t1w_w_rtype [n];
  du  = new nagad_a1t1w_w_rtype [n1];
  b   = new nagad_a1t1w_w_rtype [n];
  dlf = new nagad_a1t1w_w_rtype [n1];
  df  = new nagad_a1t1w_w_rtype [n];
  duf = new nagad_a1t1w_w_rtype [n1];
  x   = new nagad_a1t1w_w_rtype [n];

  // Create AD tape
  nagad_a1t1w_ir_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;
    dt = dd;
    nagad_t1w_set_derivative(&dt,inc);
    du[i] = dt;
    nagad_a1t1w_ir_register_variable(&du[i]);
    duf[i] = du[i];
  }
  for (int i = 0; i<n; i++) {
    cin >> dd;
    dt = dd;
    nagad_t1w_set_derivative(&dt,inc);
    d[i] = dt;
    nagad_a1t1w_ir_register_variable(&d[i]);
    df[i] = d[i];
  }
  for (int i = 0; i<n1; i++) {
    cin >> dd;
    dt = dd;
    nagad_t1w_set_derivative(&dt,inc);
    dl[i] = dt;
    nagad_a1t1w_ir_register_variable(&dl[i]);
    dlf[i] = dl[i];
  }
  for (int i = 0; i<n; i++) {
    cin >> dd;
    dt = dd;
    nagad_t1w_set_derivative(&dt,inc);
    b[i] = dt;
    nagad_a1t1w_ir_register_variable(&b[i]);
    x[i] = b[i];
  }

  // Create AD configuration data object
  ifail = 0;
  nag::ad::x10aa(ad_handle,ifail);
  
  // Set AD computational mode
  ifail = 0;
  nag::ad::x10ac(ad_handle,mode,ifail);
  
  // Solve the equations Ax = b for x
  ifail = 0;
  nag::ad::f07ca(ad_handle,n,nrhs,dlf,df,duf,x,n,ifail);

  // Print primal solution
  cout << "  Solution:\n";
  cout.precision(4);
  cout.width(12); cout << " ";
  for (int i=0; i<n; i++) {
    nagad_t1w_w_rtype xv = nagad_a1t1w_get_value(x[i]);
    cout.width(10); cout << nagad_t1w_get_value(xv);
  }

  cout << "\n\n Derivatives calculated: First order adjoints\n";
  if (mode==nagad_symbolic) {
    cout << " Computational mode    : symbolic\n";
  } else {
    cout << " Computational mode    : algorithmic\n";
  }
  
  // Obtain derivatives for each output solution point

  cout.setf(ios::scientific,ios::floatfield);
  cout.setf(ios::right);
  cout.precision(2);
  // Set second derivative seeds all to 1 and evaluate adjoint
  for (int i=0; i<n; i++) {
    dt = inc;
    nagad_a1t1w_set_derivative(&x[i],dt);
  }
  ifail = 0;
  nagad_a1t1w_ir_interpret_adjoint_sparse(ifail);

  dd = 0.0;
  for (int j=0; j<n1; j++) {
    nagad_t1w_w_rtype tang = nagad_a1t1w_get_derivative(du[j]);
    dd += nagad_t1w_get_derivative(tang);
    tang = nagad_a1t1w_get_derivative(dl[j]);
    dd += nagad_t1w_get_derivative(tang);
  }
  for (int j=0; j<n; j++) {
    nagad_t1w_w_rtype tang = nagad_a1t1w_get_derivative(d[j]);
    dd += nagad_t1w_get_derivative(tang);
    tang = nagad_a1t1w_get_derivative(b[j]);
    dd += nagad_t1w_get_derivative(tang);
  }

  cout << "\n Sum of all Hessian elements of solution x w.r.t. l, d, u and b" << endl;
  cout << " sum_ij [d2x/dall2]_ij = " << dd << endl;

  // Remove computational data object and tape
  ifail = 0;
  nag::ad::x10ab(ad_handle,ifail);
  nagad_a1t1w_ir_remove();

  delete [] dl;
  delete [] d;
  delete [] du;
  delete [] b;
  delete [] dlf;
  delete [] df;
  delete [] duf;
  delete [] x;
 
  return exit_status;
}