Example description
/* F08KD_A1W_F C++ Header Example Program.
 *
 * Copyright 2019 Numerical Algorithms Group.
 * Mark 27, 2019.
 */

#include <nag.h>
#include <nagx04.h>
#include <nagad.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
  int       exit_status = 0;
  void      *ad_handle = 0;
  Integer   ifail = 0;
  NagError  fail;
  INIT_FAIL(fail);

  cout << "F08KD_A1W_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_a1w_w_rtype *a=0, *a_in=0, *s=0, *u=0, *vt=0, *work=0;
  double            *ur=0, *vtr = 0;
  Integer           *iwork=0;
  Charlen           lena = 1;
  if (!(a     = NAG_ALLOC(m*n, nagad_a1w_w_rtype)) ||
      !(a_in  = NAG_ALLOC(m*n, nagad_a1w_w_rtype)) ||
      !(s     = NAG_ALLOC(m,   nagad_a1w_w_rtype)) ||
      !(u     = NAG_ALLOC(m*m, nagad_a1w_w_rtype)) ||
      !(vt    = NAG_ALLOC(n*n, nagad_a1w_w_rtype)) ||
      !(iwork = NAG_ALLOC(8*n, Integer))) {
    cout << "Allocation failure\n";
    exit_status = -1;
    goto END;
  }

  // Create AD tape
  nagad_a1w_ir_create();

  // 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].value = dd;
      a_in[k].id = 0;
      nagad_a1w_ir_register_variable(&a_in[k]);
      a[k] = a_in[k];
    }
  }

  // Create AD configuration data object
  ifail = 0;
  x10aa_a1w_f_(ad_handle,ifail);

  // Set AD computational mode
  ifail = 0;
  x10ac_a1w_f_(ad_handle,mode,ifail);

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

  lwork = (Integer) dummy[0].value + 1;
  if (!(work = NAG_ALLOC(lwork, nagad_a1w_w_rtype))) {
    cout << "Allocation failure\n";
    exit_status = -2;
    goto END;
  }

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

  // 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 << nagad_a1w_get_value(s[i]);
  }

  // Copy primal values to array for printing
  if (!(ur     = NAG_ALLOC(m*m, double)) ||
      !(vtr    = NAG_ALLOC(n*n, double))) {
    cout << "Allocation failure\n";
    exit_status = -3;
    goto END;
  }

  for (int i=0; i<m*m; i++) {
    ur[i] = nagad_a1w_get_value(u[i]);
  }
  for (int j=0; j<n; j++) {
    Integer k = j*n;
    for (int i=0; i<m; i++) {
      vtr[k] = nagad_a1w_get_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 adjoints\n";
  if (mode==nagad_symbolic) {
    cout << " Computational mode    : symbolic\n";
  } else {
    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;
    // Setup evaluation of derivatives via adjoints
    nagad_a1w_ir_zero_adjoints();
    double inc = 1.0;
    nagad_a1w_inc_derivative(&s[i],inc);
    ifail = -1;
    nagad_a1w_ir_interpret_adjoint(ifail);
    if (ifail != 0) {
      exit_status = 1;
      goto END;
    }
    // Get derivatives
    cout.width(10); cout << " ";
    for (int j=0; j<m; j++) {
      double dsda = nagad_a1w_get_derivative(a_in[j]);
      cout.width(10); cout << dsda;
    }
    cout << endl;
  }
 END:
  // Remove computational data object and tape
  ifail = 0;
  x10ab_a1w_f_(ad_handle,ifail);
  nagad_a1w_ir_remove();

  NAG_FREE(a);
  NAG_FREE(a_in);
  NAG_FREE(s);
  NAG_FREE(u);
  NAG_FREE(vt);
  NAG_FREE(iwork);
  NAG_FREE(work);
  NAG_FREE(ur);
  NAG_FREE(vtr);

  return exit_status;
}