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

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

#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_T1W_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_t1w_w_rtype *a = 0, *a_in = 0, *d = 0, *e = 0, *taup = 0, *tauq,
                    *work = 0;
  double *dd = 0, *de = 0;
  a    = new nagad_t1w_w_rtype[m * n];
  a_in = new nagad_t1w_w_rtype[m * n];
  d    = new nagad_t1w_w_rtype[n];
  e    = new nagad_t1w_w_rtype[n - 1];
  taup = new nagad_t1w_w_rtype[n];
  tauq = new nagad_t1w_w_rtype[n];
  work = new nagad_t1w_w_rtype[lwork];
  dd   = new double[m * n];
  de   = new double[m * n - n];

  // 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;
    }
  }

  // Create AD configuration data object
  ifail = 0;

  double inc = 1.0, zero = 0.0;
  for (int i = 0; i < m; ++i)
  {
    dco::derivative(a_in[i]) = inc;
    for (int j = 0; j < m * n; ++j)
    {
      a[j] = a_in[j];
    }
    // reduce A to bidiagonal form
    ifail = 0;
    nag::ad::f08ke(ad_handle, m, n, a, lda, d, e, tauq, taup, work, lwork,
                   ifail);
    dco::derivative(a_in[i]) = zero;
    for (int j = 0; j < n; ++j)
    {
      dd[j + i * n] = dco::derivative(d[j]);
    }
    for (int j = 0; j < n - 1; ++j)
    {
      de[j + i * (n - 1)] = dco::derivative(e[j]);
    }
  }

  // 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 tangents\n";
  cout << " Computational mode    : algorithmic\n";

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

  cout << "\n\n";
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, m, dd, n,
         "  dD_i/dA_j1", 0, &fail);
  cout << "\n";
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n - 1, m, de, n - 1,
         " dE_i/DA_j1", 0, &fail);

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