Example description
/* F11XE_A1W_F C++ Header Example Program.
 *
 * Copyright 2020 Numerical Algorithms Group.
 * Mark 27.1, 2020.
 */
#include <dco_light.hpp>
#include <nag.h>
#include <nagx04.h>
#include <nagad.h>
#include <stdio.h>
#include <iostream>
using namespace std;

int main(void)
{
  int       exit_status = 0;
  void      *ad_handle = 0;
  Integer   ifail = 0;

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

  // Read order of matrix and number of nonzero entries
  Integer   n, nnz;
  cin >> n;
  cin >> nnz;

  nagad_a1w_w_rtype *a=0, *x=0, *y=0;
  double            *ar=0, *yr=0, *dydx=0;
  Integer           *icol=0, *irow=0;

  a        = new nagad_a1w_w_rtype [nnz];
  x        = new nagad_a1w_w_rtype [n];
  y        = new nagad_a1w_w_rtype [n];
  icol     = new Integer [nnz];
  irow     = new Integer [nnz];
  ar       = new double [nnz];
  yr       = new double [n];
  dydx     = new double [n*n];
      
  // Read the matrix A
  for (int i=0; i<nnz; i++) {
    cin >> ar[i] >> irow[i] >> icol[i];
    a[i] = ar[i];
  }
      
  // Create AD tape
  nagad_a1w_ir_create();

  // Read the vector x
  for (int i=0; i<n; i++) {
    cin >> yr[i];
    x[i] = yr[i];
    nagad_a1w_ir_register_variable(&x[i]);
  }

  // Create AD configuration data object
  ifail = 0;
  x10aa_a1w_f_(ad_handle,ifail);

  // Calculate matrix-vector product
  ifail = 0;
  f11xe_a1w_f_(ad_handle,n,nnz,a,irow,icol,"C",x,y,ifail,1);

  // Output results
  cout.setf(ios::scientific,ios::floatfield);
  cout.precision(4);
  cout << "  Matrix-vector product" << endl;
  for (int i=0; i<n; ++i) {
    cout.width(12);cout << nagad_a1w_get_value(y[i]) << endl;
  }

  cout << "\n Derivatives calculated: First order adjoints\n";
  cout << " Computational mode    : algorithmic\n";
  cout << "\n Derivatives of Y w.r.t X (A):\n";

  // Setup evaluation of derivatives via adjoints
  for (int i=0; i<n; i++) {
    // Reset adjoints, initialize derivative, and evaluate adjoint
    nagad_a1w_ir_zero_adjoints();
    double inc = 1.0;
    nagad_a1w_inc_derivative(&y[i],inc);
    ifail = 0;
    nagad_a1w_ir_interpret_adjoint(ifail);
      
    for (int j=0; j<n; j++) {
      Integer k = i + j*n;
      dydx[k] = nagad_a1w_get_derivative(x[j]);
    }
  }
  // Print derivatives
  cout << endl;
  NagError fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,n,dydx,n,
         "       dy_i/dx_j",0,&fail);

  // Remove computational data object and tape
  ifail = 0;
  x10ab_a1w_f_(ad_handle,ifail);
  nagad_a1w_ir_remove();

  delete [] a;
  delete [] x;
  delete [] y;
  delete [] icol;
  delete [] irow;
  delete [] ar;
  delete [] yr;
  delete [] dydx;

  return exit_status;
}