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

NAG AD Library Introduction
Example description
/* F08KP_T1W_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 << "F08KP_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, ldu = m, ldvt = n, lwork;
  nagad_t1w_w_ctype *a = 0, *u = 0, *vt = 0, *work = 0, dummy[1];
  nagad_t1w_w_rtype *ar = 0, *ai = 0, *s = 0, *rwork = 0;
  Complex *          uc = 0, *vtc = 0, *dsda = 0;
  Charlen            lena = 1;
  a                       = new nagad_t1w_w_ctype[m * n];
  ar                      = new nagad_t1w_w_rtype[m * n];
  ai                      = new nagad_t1w_w_rtype[m * n];
  s                       = new nagad_t1w_w_rtype[m];
  rwork                   = new nagad_t1w_w_rtype[5 * n];
  dsda                    = new Complex[n * m];
  u                       = new nagad_t1w_w_ctype[m * m];
  vt                      = new nagad_t1w_w_ctype[n * n];

  // 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;
      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_t1w_w_ctype[lwork];

  double inc = 1.0, zero = 0.0;
  for (int i = 0; i < 2 * m; i++)
  {
    if (i < m)
    {
      dco::derivative(ar[i]) = inc;
    }
    else
    {
      dco::derivative(ai[i - m]) = inc;
    }
    for (int j = 0; j < m * n; ++j)
    {
      a[j].real(ar[j]);
      a[j].imag(ai[j]);
    }
    //  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);

    if (i < m)
    {
      dco::derivative(ar[i]) = zero;
      for (int j = 0; j < n; j++)
      {
        dsda[j + n * i].re = dco::derivative(s[j]);
      }
    }
    else
    {
      dco::derivative(ai[i - m]) = zero;
      for (int j = 0; j < n; j++)
      {
        dsda[j + n * (i - m)].im = dco::derivative(s[j]);
      }
    }
  }

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

  cout << "\n Derivatives of Singular values w.r.t first column of A\n";
  cout << "\n";
  x04dac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, m, dsda, n,
         " dS_i/dA_j1", 0, &fail);

  ifail = 0;

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