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

NAG AD Library Introduction
Example description
/* F07AR_A1W_F C++ Header Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 * Mark 30.1, 2024.
 */

#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           ifail = 0;

  cout << "F07AW_A1W_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.
  nagad_a1w_w_ctype *a = 0, *a_in = 0, *work = 0;
  nagad_a1w_w_rtype *a_r = 0, *a_i = 0;
  Complex *          ar    = 0;
  Integer *          ipiv  = 0;
  Integer            lwork = 64 * n;
  a                        = new nagad_a1w_w_ctype[n * n];
  a_in                     = new nagad_a1w_w_ctype[n * n];
  a_r                      = new nagad_a1w_w_rtype[n * n];
  a_i                      = new nagad_a1w_w_rtype[n * n];
  work                     = new nagad_a1w_w_ctype[lwork];
  ipiv                     = new Integer[n];
  ar                       = new Complex[n * n];

  // Create AD tape
  dco::ga1s<double>::global_tape = dco::ga1s<double>::tape_t::create();

  // Read the matrix A, register and copy
  double            dr, di;
  nagad_a1w_w_rtype aa;
  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < n; ++j)
    {
      Integer k = i + j * n;
      cin >> dr >> di;
      a_r[k] = dr;
      a_i[k] = di;
      dco::ga1s<double>::global_tape->register_variable(a_r[k]);
      dco::ga1s<double>::global_tape->register_variable(a_i[k]);
      a[k].real(a_r[k]);
      a[k].imag(a_i[k]);
      ar[k].re = dr;
      ar[k].im = di;
    }
  }

  // Print matrix A
  NagError fail;
  INIT_FAIL(fail);
  x04dac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, ar, n, "  A",
         0, &fail);

  // Create AD configuration data object
  ifail = 0;

  // Factorize the matrix A
  ifail = 0;
  nag::ad::f07ar(ad_handle, n, n, a, n, ipiv, ifail);

  // Invert A
  ifail = 0;
  nag::ad::f07aw(ad_handle, n, a, n, ipiv, work, lwork, ifail);

  // Print Inverse
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < n; j++)
    {
      int               k = i + j * n;
      nagad_a1w_w_rtype akr, aki;
      akr      = real(a[k]);
      aki      = imag(a[k]);
      ar[k].re = dco::value(akr);
      ar[k].im = dco::value(aki);
    }
  }
  cout << endl;
  // NagError  fail;
  INIT_FAIL(fail);
  x04dac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, ar, n,
         "  Inverse", 0, &fail);

  cout << "\n\n Derivatives calculated: First order adjoints\n";
  cout << " Computational mode    : algorithmic\n";
  cout << "\n Derivatives of inverse diagonal w.r.t diagonal of A:\n";

  // Obtain derivatives
  for (int i = 0; i < n; i++)
  {

    // Reset adjoints, initialize derivative, and evaluate adjoint
    dco::ga1s<double>::global_tape->zero_adjoints();
    double  inc = 1.0;
    Integer k   = i * n + i;

    nagad_a1w_w_rtype dar;
    dar = real(a[k]);
    dco::derivative(dar) += inc;
    ifail                                              = 0;
    dco::ga1s<double>::global_tape->sparse_interpret() = true;
    dco::ga1s<double>::global_tape->interpret_adjoint();

    for (int j = 0; j < n; j++)
    {
      Integer k        = j + j * n;
      double  dr       = dco::derivative(a_r[k]);
      double  di       = dco::derivative(a_i[k]);
      ar[i + j * n].re = dr;
      ar[i + j * n].im = di;
    }
  }
  // Print derivatives
  cout << endl;
  INIT_FAIL(fail);
  x04dac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, ar, n,
         "   d(real(ai(i,i)))/da(j,j)", 0, &fail);

  for (int i = 0; i < n; i++)
  {

    // Reset adjoints, initialize derivative, and evaluate adjoint
    dco::ga1s<double>::global_tape->zero_adjoints();
    double  inc = 1.0;
    Integer k   = i * n + i;

    nagad_a1w_w_rtype dar;
    dar = imag(a[k]);
    dco::derivative(dar) += inc;
    ifail                                              = 0;
    dco::ga1s<double>::global_tape->sparse_interpret() = true;
    dco::ga1s<double>::global_tape->interpret_adjoint();

    for (int j = 0; j < n; j++)
    {
      Integer k        = j + j * n;
      double  dr       = dco::derivative(a_r[k]);
      double  di       = dco::derivative(a_i[k]);
      ar[i + j * n].re = dr;
      ar[i + j * n].im = di;
    }
  }
  // Print derivatives
  cout << endl;
  INIT_FAIL(fail);
  x04dac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, ar, n,
         "   d(real(ai(i,i)))/da(j,j)", 0, &fail);

  ifail = 0;

  dco::ga1s<double>::tape_t::remove(dco::ga1s<double>::global_tape);

  delete[] a;
  delete[] a_in;
  delete[] a_r;
  delete[] a_i;
  delete[] work;
  delete[] ipiv;
  delete[] ar;
  return exit_status;
}