Example description
/* F08ME_T1W_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_T1W_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_t1w_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_t1w_w_rtype [1];
  d     = new nagad_t1w_w_rtype [n];
  e     = new nagad_t1w_w_rtype [n-1];
  d_in  = new nagad_t1w_w_rtype [n];
  e_in  = new nagad_t1w_w_rtype [n-1];
  u     = new nagad_t1w_w_rtype [n*n];
  vt    = new nagad_t1w_w_rtype [n*n];
  work  = new nagad_t1w_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];

  // Read the matrix A, register and copy
  double ddd;
  for (int i = 0; i<n; i++) {
    cin >> ddd;
    d_in[i] = ddd;
  }
  for (int i = 0; i<n-1; i++) {
    cin >> ddd;
    e_in[i] = ddd;
  }

  // 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_t1w_f_(ad_handle,ifail);

  double inc = 1.0, zero = 0.0;
  for (int i = 0; i<2*n-1; i++) {
    if (i<n) {
      nagad_t1w_inc_derivative(&d_in[i],inc);
    } else {
      nagad_t1w_inc_derivative(&e_in[i-n],inc);
    }

    for (int j = 0; j<n; j++) {
      d[j] = d_in[j];
    }
    for (int j = 0; j<n-1; j++) {
      e[j] = e_in[j];
    }

    // Initialize U and VT to be the unit matrix
    for (int j = 0; j<n*n; j++) {
      u[j] = 0.0;
      vt[j] = 0.0;
    }
    for (int j = 0; j<n; j++) {
      u[j*n+j] = 1.0;
      vt[j*n+j] = 1.0;
    }

    // Calculate the SVD of bidiagonal matrix defined by d, e
    ifail = 0;
    f08me_t1w_f_(ad_handle,"U",n,n,n,0,d,e,u,ldu,vt,ldvt,c,ldc,work,
                 ifail,1);
    if (i<n) {
      nagad_t1w_set_derivative(&d_in[i],zero);
      for (int j=0; j<n; j++) {
        Integer k = i*n + j;
        dsdd[k] = nagad_t1w_get_derivative(d[j]);
      }
    } else {
      nagad_t1w_set_derivative(&e_in[i-n],zero);
      for (int j=0; j<n; j++) {
        Integer k = (i-n)*n + j;
        dsde[k] = nagad_t1w_get_derivative(d[j]);
      }
    }
  }

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

  for (int i = 0; i<n*n; i++) {
    ur[i] = nagad_t1w_get_value(u[i]);
    vtr[i] = nagad_t1w_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 tangents\n";
  cout << " Computational mode    : algorithmic\n";

  cout << "\n Derivatives of singular values w.r.t input d and e\n";

  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
  ifail = 0;
  x10ab_t1w_f_(ad_handle,ifail);

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