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

NAG AD Library Introduction
Example description
/* F08KE_A1W_F C++ Header Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 * Mark 30.0, 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;
  NagError          fail;
  INIT_FAIL(fail);

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

  // Read matrix dimensions
  Integer m, n;
  cin >> m;
  cin >> n;

  // Allocate arrays containing A and its factorized form, B
  // and the solution X.
  Integer            lda = m, lwork = 64 * (m + n);
  nagad_a1w_w_rtype *a = 0, *a_in = 0, *d = 0, *e = 0, *taup = 0, *tauq,
                    *work = 0;
  double *dd = 0, *de = 0;
  a    = new nagad_a1w_w_rtype[m * n];
  a_in = new nagad_a1w_w_rtype[m * n];
  d    = new nagad_a1w_w_rtype[n];
  e    = new nagad_a1w_w_rtype[n - 1];
  taup = new nagad_a1w_w_rtype[n];
  tauq = new nagad_a1w_w_rtype[n];
  work = new nagad_a1w_w_rtype[lwork];
  dd   = new double[m * n];
  de   = new double[m * n - n];

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

  // Read the matrix A, register and copy
  double ddd;
  for (int i = 0; i < m; i++)
  {
    for (int j = 0; j < n; j++)
    {
      Integer k = i + j * m;
      cin >> ddd;
      a_in[k] = ddd;
      dco::ga1s<double>::global_tape->register_variable(a_in[k]);
      a[k] = a_in[k];
    }
  }

  // Create AD configuration data object
  ifail = 0;

  // reduce A to bidiagonal form
  ifail = 0;
  nag::ad::f08ke(ad_handle, m, n, a, lda, d, e, tauq, taup, work, lwork, ifail);

  // Print bidiagonal form
  cout.precision(4);
  cout << " Diagonal:" << endl;
  cout.width(12);
  cout << " ";
  for (int i = 0; i < n; i++)
  {
    cout.width(11);
    cout << dco::value(d[i]);
  }
  cout << endl;
  if (m > n)
  {
    cout << " Superdiagonal:" << endl;
  }
  else
  {
    cout << " Subdiagonal:" << endl;
  }
  cout.width(12);
  cout << " ";
  for (int i = 0; i < n - 1; i++)
  {
    cout.width(11);
    cout << dco::value(e[i]);
  }

  cout << "\n\n Derivatives calculated: First order adjoints\n";
  cout << " Computational mode    : algorithmic\n";

  cout << "\n Derivatives of d/e w.r.t first column of A\n";

  for (int i = 0; i < n; i++)
  {
    dco::ga1s<double>::global_tape->zero_adjoints();
    double inc = 1.0;
    dco::derivative(d[i]) += inc;
    ifail                                              = 0;
    dco::ga1s<double>::global_tape->sparse_interpret() = true;
    dco::ga1s<double>::global_tape->interpret_adjoint();
    for (int j = 0; j < m; j++)
    {
      Integer k = i + j * n;
      dd[k]     = dco::derivative(a_in[j]);
    }
  }
  cout << "\n\n";
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, m, dd, n,
         "  dD_i/dA_j1", 0, &fail);
  for (int i = 0; i < n - 1; i++)
  {
    dco::ga1s<double>::global_tape->zero_adjoints();
    double inc = 1.0;
    dco::derivative(e[i]) += inc;
    ifail                                              = 0;
    dco::ga1s<double>::global_tape->sparse_interpret() = true;
    dco::ga1s<double>::global_tape->interpret_adjoint();
    for (int j = 0; j < m; j++)
    {
      Integer k = i + j * (n - 1);
      de[k]     = dco::derivative(a_in[j]);
    }
  }
  cout << "\n";
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n - 1, m, de, n - 1,
         " dE_i/DA_j1", 0, &fail);

  ifail = 0;

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

  delete[] a;
  delete[] a_in;
  delete[] d;
  delete[] e;
  delete[] taup;
  delete[] tauq;
  delete[] work;
  delete[] dd;
  delete[] de;
  return exit_status;
}