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

NAG AD Library Introduction
Example description
/* F08KD_T1W_F C++ Header Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 * Mark 30.3, 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 << "F08KD_T1W_F C++ Header Example Program Results\n\n";
  // Skip heading in data file
  string mystr;
  getline(cin, mystr);

  // Read matrix dimensions and algorithmic mode
  Integer m, n, mode;
  cin >> m;
  cin >> n;
  cin >> mode;

  // Allocate arrays containing A and its factorized form, B
  // and the solution X.
  Integer            lda = m, ldu = m, ldvt = n, lwork;
  nagad_t1w_w_rtype *a = 0, *a_in = 0, *s = 0, *u = 0, *vt = 0, *work = 0;
  double *           ur = 0, *vtr = 0, *dsda = 0;
  Integer *          iwork = 0;
  Charlen            lena  = 1;
  a                        = new nagad_t1w_w_rtype[m * n];
  a_in                     = new nagad_t1w_w_rtype[m * n];
  s                        = new nagad_t1w_w_rtype[m];
  u                        = new nagad_t1w_w_rtype[m * m];
  vt                       = new nagad_t1w_w_rtype[n * n];
  iwork                    = new Integer[8 * n];
  dsda                     = new double[m * m];

  // Read the matrix A, register and copy
  double dd;
  for (int i = 0; i < m; i++)
  {
    for (int j = 0; j < n; j++)
    {
      cin >> dd;
      Integer k = i + j * m;
      a_in[k]   = dd;
    }
  }

  // Create AD configuration data object
  ifail = 0;

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

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

  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 < n * m; j++)
    {
      a[j] = a_in[j];
    }
    //  Compute the singular values and left and right singular vectors
    //  of A (A = U*S*(V**T), m < n)
    nag::ad::f08kd(ad_handle, "A", m, n, a, lda, s, u, ldu, vt, ldvt, work,
                   lwork, iwork, ifail);

    dco::derivative(a_in[i]) = zero;
    for (int j = 0; j < m; j++)
    {
      dsda[j + i * m] = dco::derivative(s[j]);
    }
  }

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

  // Copy primal values to array for printing
  ur  = new double[m * m];
  vtr = new double[n * n];

  for (int i = 0; i < m * m; i++)
  {
    ur[i] = dco::value(u[i]);
  }
  for (int j = 0; j < n; j++)
  {
    Integer k = j * n;
    for (int i = 0; i < m; i++)
    {
      vtr[k] = dco::value(vt[k]);
      k++;
    }
  }

  cout << "\n\n";
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, m, m, ur, ldu,
         "Left singular vectors by column", 0, &fail);
  cout << "\n";
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, m, n, vtr, ldvt,
         "Right singular vectors by row", 0, &fail);

  cout << "\n\n Derivatives calculated: First order tangents\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 < m; i++)
  {
    cout << "\n Singular value " << i + 1 << endl;
    // Get derivatives
    cout.width(10);
    cout << " ";
    for (int j = 0; j < m; j++)
    {
      cout.width(10);
      cout << dsda[i + j * m];
    }
    cout << endl;
  }

  ifail = 0;

  delete[] ur;
  delete[] vtr;
  delete[] work;
  delete[] a;
  delete[] a_in;
  delete[] s;
  delete[] u;
  delete[] vt;
  delete[] iwork;
  delete[] dsda;
  return exit_status;
}