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

NAG AD Library Introduction
Example description
/* F01EF_P0W_F C++ Header Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 * Mark 29.0, 2023.
 */

#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 << "F01EF_P0W_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;

  // Allocate arrays containing A and its factorized form, B
  // and the solution X.
  double *a = 0;
  a         = new double[n * n];

  // Read the matrix A, register and copy
  for (int i = 0; i < n; ++i)
  {
    for (int j = i; j < n; ++j)
    {
      Integer k = i + j * n;
      cin >> a[k];
    }
  }
  ifail = 0;
  auto f = [&](nag::ad::handle_t &     ad_handle,
            Integer &               iflag,
            const Integer &         n,
            const double *x,
            double *fx)
          {
            for (int i = 0; i < n; ++i)
            {
              fx[i] = cos(x[i]);
            }
          };
  // Find f(A)
  Integer           iflag = 0;
  nag::ad::f01ef(ad_handle, "U", n, a, n, f, iflag, ifail);

  // Print f(A)
  cout << endl;
  NagError fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor, Nag_UpperMatrix, Nag_NonUnitDiag, n, n, a, n, "  f(A)",
         0, &fail);

  delete[] a;

  return exit_status;
}