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

#include <dco_light.hpp>
#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;
  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
  nagad_a1w_ir_create();

  // Read the matrix A, register and copy
  double ddd;
  for (int i = 0; i<n; i++) {
    cin >> ddd;
    d_in[i] = ddd;
    nagad_a1w_ir_register_variable(&d_in[i]);
    d[i] = d_in[i];
  }
  for (int i = 0; i<n-1; i++) {
    cin >> ddd;
    e_in[i] = ddd;
    nagad_a1w_ir_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;
  x10aa_a1w_f_(ad_handle,ifail);

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

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

  for (int i = 0; i<n*n; i++) {
    ur[i] = nagad_a1w_get_value(u[i]);
    vtr[i] = nagad_a1w_get_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++) {
    nagad_a1w_ir_zero_adjoints();
    double inc = 1.0;
    nagad_a1w_inc_derivative(&d[i],inc);
    ifail = 0;
    nagad_a1w_ir_interpret_adjoint(ifail);
    for (int j=0; j<n; j++) {
      Integer k = i + j*n;
      dsdd[k] = nagad_a1w_get_derivative(d_in[j]);
    }
    for (int j=0; j<n-1; j++) {
      Integer k = i + j*n;
      dsde[k] = nagad_a1w_get_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);

  // Remove computational data object and tape
  ifail = 0;
  x10ab_a1w_f_(ad_handle,ifail);
  nagad_a1w_ir_remove();

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