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

NAG AD Library Introduction
Example description
/* F08ME_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;
  NagError          fail;
  INIT_FAIL(fail);

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

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

  // Allocate arrays containing A and its factorized form, B
  // and the solution X.
  Integer            ldc = 1, ldu = n, ldvt = n;
  nagad_a1w_w_rtype *c = 0, *d = 0, *e = 0, *d_in = 0, *e_in = 0, *u = 0,
                    *vt = 0, *work = 0;
  double *dr = 0, *er = 0, *dsdd = 0, *dsde = 0, *ur = 0, *vtr = 0;
  c    = new nagad_a1w_w_rtype[1];
  d    = new nagad_a1w_w_rtype[n];
  e    = new nagad_a1w_w_rtype[n - 1];
  d_in = new nagad_a1w_w_rtype[n];
  e_in = new nagad_a1w_w_rtype[n - 1];
  u    = new nagad_a1w_w_rtype[n * n];
  vt   = new nagad_a1w_w_rtype[n * n];
  work = new nagad_a1w_w_rtype[4 * n];
  dr   = new double[n];
  er   = new double[n - 1];
  dsdd = new double[n * n];
  dsde = new double[n * n - n];
  ur   = new double[n * n];
  vtr  = new double[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 < n; i++)
  {
    cin >> ddd;
    d_in[i] = ddd;
    dco::ga1s<double>::global_tape->register_variable(d_in[i]);
    d[i] = d_in[i];
  }
  for (int i = 0; i < n - 1; i++)
  {
    cin >> ddd;
    e_in[i] = ddd;
    dco::ga1s<double>::global_tape->register_variable(e_in[i]);
    e[i] = e_in[i];
  }

  // Initialize U and VT to be the unit matrix
  for (int i = 0; i < n * n; i++)
  {
    u[i]  = 0.0;
    vt[i] = 0.0;
  }
  for (int i = 0; i < n; i++)
  {
    u[i * n + i]  = 1.0;
    vt[i * n + i] = 1.0;
  }

  // Create AD configuration data object
  ifail = 0;

  // Calculate the SVD of bidiagonal matrix defined by d, e
  ifail = 0;
  nag::ad::f08me(ad_handle, "U", n, n, n, 0, d, e, u, ldu, vt, ldvt, c, ldc,
                 work, ifail);

  // Print singular values
  cout.precision(4);
  cout << " Singular values:" << endl;
  cout.width(12);
  cout << " ";
  for (int i = 0; i < n; i++)
  {
    cout.width(11);
    cout << dco::value(d[i]);
  }
  cout << endl;

  for (int i = 0; i < n * n; i++)
  {
    ur[i]  = dco::value(u[i]);
    vtr[i] = dco::value(vt[i]);
  }
  cout << endl;
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, ur, n,
         "     Left Singular values (columns)", 0, &fail);
  cout << endl;
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, vtr, n,
         "     Right Singular values (rows)", 0, &fail);

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

  cout << "\n Derivatives of singular values w.r.t input d and e\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 < n; j++)
    {
      Integer k = i + j * n;
      dsdd[k]   = dco::derivative(d_in[j]);
    }
    for (int j = 0; j < n - 1; j++)
    {
      Integer k = i + j * n;
      dsde[k]   = dco::derivative(e_in[j]);
    }
  }

  cout << endl;
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, dsdd, n,
         "  dS_i/dD_j", 0, &fail);
  cout << endl;
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n - 1, dsde, n,
         " dS_i/DE_j", 0, &fail);

  ifail = 0;

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

  delete[] c;
  delete[] d;
  delete[] e;
  delete[] d_in;
  delete[] e_in;
  delete[] u;
  delete[] vt;
  delete[] work;
  delete[] dr;
  delete[] er;
  delete[] dsdd;
  delete[] dsde;
  delete[] ur;
  delete[] vtr;
  return exit_status;
}