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

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

#include <dco.hpp>
#include <iostream>
#include <math.h>
#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           ifail = 0;

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

  // Read problem size and parameters
  Integer m, n;
  cin >> m >> n;
  double alphar, betar;
  cin >> alphar >> betar;
  nagad_t1w_w_rtype alpha, beta;
  alpha = alphar;
  beta  = betar;

  // Allocate arrays containing A and its factorized form, B
  // and the solution X.
  nagad_t1w_w_rtype *a = 0, *b = 0, *y = 0;
  double *           ar = 0, *br = 0, *yr = 0, *da = 0, *db = 0;
  a  = new nagad_t1w_w_rtype[m * n];
  b  = new nagad_t1w_w_rtype[m];
  y  = new nagad_t1w_w_rtype[n];
  ar = new double[m * n];
  br = new double[m];
  yr = new double[n];
  da = new double[n];
  db = new double[n];

  // Read the matrix A
  double dd;
  for (int i = 0; i < m; ++i)
  {
    for (int j = 0; j < n; ++j)
    {
      cin >> dd;
      Integer k = i + j * m;
      ar[k]     = dd;
      a[k]      = dd;
    }
  }
  for (int i = 0; i < m; ++i)
  {
    cin >> dd;
    br[i] = dd;
    b[i]  = dd;
  }
  for (int i = 0; i < n; ++i)
  {
    yr[i] = 1.0;
    y[i]  = 1.0;
  }

  // Create AD configuration data object
  ifail = 0;

  for (int i = 0; i < n; ++i)
  {
    yr[i] = dco::value(y[i]);
  }

  //  y = beta*y + alpha*A^T*b
  double inc             = 1.0;
  dco::derivative(alpha) = inc;
  ifail                  = 0;
  nag::ad::f06pa(ad_handle, "T", m, n, alpha, a, m, b, 1, beta, y, 1, ifail);

  for (int i = 0; i < n; ++i)
  {
    yr[i] = dco::value(y[i]);
  }

  cout << endl;
  NagError fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, 1, yr, n,
         "  Array y after update", 0, &fail);

  cout << "\n\n Derivatives calculated: First order tangents\n";
  cout << " Computational mode    : algorithmic\n";
  cout << "\n Derivatives of y w.r.t alpha and beta:\n";

  for (int i = 0; i < n; ++i)
  {
    da[i] = dco::derivative(y[i]);
  }
  double zero            = 0.0;
  dco::derivative(alpha) = zero;

  for (int i = 0; i < n; ++i)
  {
    y[i] = 1.0;
  }
  inc                   = 1.0;
  dco::derivative(beta) = inc;

  ifail = 0;
  nag::ad::f06pa(ad_handle, "T", m, n, alpha, a, m, b, 1, beta, y, 1, ifail);

  for (int i = 0; i < n; ++i)
  {
    db[i] = dco::derivative(y[i]);
  }
  // Print derivatives
  cout << endl;
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, 1, n, da, 1,
         "           dy/dalpha", 0, &fail);
  cout << endl;
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, 1, n, db, 1,
         "           dy/dbeta", 0, &fail);

  delete[] a;
  delete[] b;
  delete[] y;
  delete[] ar;
  delete[] br;
  delete[] yr;
  delete[] da;
  delete[] db;

  return exit_status;
}