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

NAG AD Library Introduction
Example description
/* F08KP_A1W_F C++ Header Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 * Mark 28.5, 2022.
 */

#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 << "F08KP_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, ldu = m, ldvt = n, lwork;
  nagad_a1w_w_ctype *a = 0, *u = 0, *vt = 0, *work = 0, dummy[1];
  nagad_a1w_w_rtype *ar = 0, *ai = 0, *s = 0, *rwork = 0;
  Complex *          uc = 0, *vtc = 0, *dsda = 0;
  Charlen            lena = 1;
  a                       = new nagad_a1w_w_ctype[m * n];
  ar                      = new nagad_a1w_w_rtype[m * n];
  ai                      = new nagad_a1w_w_rtype[m * n];
  s                       = new nagad_a1w_w_rtype[m];
  rwork                   = new nagad_a1w_w_rtype[5 * n];
  dsda                    = new Complex[n * m];
  u                       = new nagad_a1w_w_ctype[m * m];
  vt                      = new nagad_a1w_w_ctype[n * n];

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

  // Read the matrix A, register and copy
  double dd, di;
  for (int i = 0; i < m; i++)
  {
    for (int j = 0; j < n; j++)
    {
      cin >> dd >> di;
      Integer k = i + j * m;
      ar[k]     = dd;
      ai[k]     = di;
      if (j == 0)
      {
        dco::ga1s<double>::global_tape->register_variable(ar[k]);
        dco::ga1s<double>::global_tape->register_variable(ai[k]);
      }
      a[k].real(ar[k]);
      a[k].imag(ai[k]);
    }
  }

  // Create AD configuration data object
  ifail = 0;

  // Use routine workspace query to get optimal workspace.
  ifail = 0;
  lwork = -1;
  nag::ad::f08kp(ad_handle, "A", "A", m, n, a, lda, s, u, ldu, vt, ldvt, dummy,
                 lwork, rwork, ifail);

  lwork = (Integer)dco::value(real(dummy[0])) + 1;
  work  = new nagad_a1w_w_ctype[lwork];

  //  Compute the singular values and left and right singular vectors
  //  of A (A = U*S*(V**T), m < n)
  nag::ad::f08kp(ad_handle, "A", "A", m, n, a, lda, s, u, ldu, vt, ldvt, work,
                 lwork, rwork, ifail);

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

  // Copy primal values to array for printing
  uc  = new Complex[m * m];
  vtc = new Complex[n * n];

  for (int i = 0; i < m * m; i++)
  {
    uc[i].re = dco::value(real(u[i]));
    uc[i].im = dco::value(imag(u[i]));
  }
  for (int i = 0; i < n * n; i++)
  {
    vtc[i].re = dco::value(real(vt[i]));
    vtc[i].im = dco::value(imag(vt[i]));
  }

  cout << "\n\n";
  x04dac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, m, m, uc, m,
         "Left singular vectors by column", 0, &fail);
  cout << "\n";
  x04dac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, vtc, n,
         "Right singular vectors by row", 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 first column of A\n";
  // Obtain derivatives for each singular value w.r.t first column of A
  cout.setf(ios::scientific, ios::floatfield);
  cout.setf(ios::right);
  cout.precision(2);
  for (int i = 0; i < n; i++)
  {
    // Setup evaluation of derivatives via adjoints
    dco::ga1s<double>::global_tape->zero_adjoints();
    double inc = 1.0;
    dco::derivative(s[i]) += inc;
    ifail                                              = 0;
    dco::ga1s<double>::global_tape->sparse_interpret() = true;
    dco::ga1s<double>::global_tape->interpret_adjoint();

    // Get derivatives
    for (int j = 0; j < m; j++)
    {
      dsda[i + n * j].re = dco::derivative(ar[j]);
      dsda[i + n * j].im = dco::derivative(ai[j]);
    }
  }
  cout << "\n";
  x04dac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, m, dsda, n,
         " dS_i/dA_j1", 0, &fail);

  ifail = 0;

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

  delete[] a;
  delete[] ar;
  delete[] ai;
  delete[] s;
  delete[] u;
  delete[] vt;
  delete[] work;
  delete[] rwork;
  delete[] uc;
  delete[] vtc;
  delete[] dsda;

  return exit_status;
}