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

#include <nag.h>
#include <stdio.h>
#include <math.h>
#include <nag.h>
#include <dco.hpp>
#include <nagad.h>
#include <stdio.h>
#include <math.h>
#include <nag_stdlib.h>
#include <iostream>
#include <string>
using namespace std;

// typedef DCO_TYPE nagad_a1w_w_rtype
typedef double DCO_BASE_TYPE;
typedef dco::ga1s<DCO_BASE_TYPE> DCO_MODE;
typedef DCO_MODE::type DCO_TYPE;
typedef DCO_MODE::tape_t DCO_TAPE_TYPE;

int main(void)
{
  /* Scalars */
  Integer  exit_status = 0;
#ifdef NAG_LOAD_FP
  /* The following line is needed to force the Microsoft linker
     to load floating point support */
  float force_loading_of_ms_float_support = 0;
#endif /* NAG_LOAD_FP */

  cout << "F08AE_A1W_F C++ Header Example Program Results\n\n";

  // Create AD tape
  // nagad_a1w_ir_create();
  DCO_MODE::global_tape=DCO_TAPE_TYPE::create();

  void    *ad_handle = 0;
  Integer ifail = 0;
  x10aa_a1w_f_(ad_handle,ifail);

  // Skip heading in data file
  string mystr;
  getline (cin, mystr);
  
  Integer m, n;
  cin >> m; cin >> n;

  Integer pda = m;
  Integer lwork = 64 * n;

  Integer tau_len;
  if (m<n) {
    tau_len = m;
  } else {
    tau_len = n;
  }

  // Allocate arrays
  DCO_TYPE *a = 0, *tau = 0, *a_in = 0, *work = 0;
  double   *r = 0, *drda = 0;
  a    = new DCO_TYPE [m * n];
  a_in = new DCO_TYPE [m * n];
  tau  = new DCO_TYPE [tau_len];
  work = new DCO_TYPE [lwork];
  r    = new double [n * n];
  drda = new double [n * m];

  // Read A from data file
  double   tmp;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j){
      cin >> tmp;
      a_in[i+j*m] = tmp;
    }
  }

  for (int i = 0; i < m; i++) {
    // nagad_a1w_ir_register_variable(&a[i]);
    DCO_MODE::global_tape->register_variable(a_in[i]);
  }
  for (int i = 0; i < m*n; i++) {
    a[i] = a_in[i];
  }

  f08ae_a1w_f_(ad_handle, m, n, a, pda, tau, work, lwork, ifail);
  if (ifail != 0) {
    printf("Error from f08ae_a1w_f_ .\n%" NAG_IFMT  " ", ifail);
    exit_status = 1;
    goto END;
  }
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      // r[i+k*n] = a[i+k*m].value
      r[i+k*n] = dco::value(a[i+k*m]);
    }
  }
  for (int i = 0; i < n; ++i) {
    int l = i*(m+1);
    //nagad_a1w_ir_zero_adjoints();
    dco::a1w::global_ir->zero_adjoints();

    // double inc = 1.0;
    // nagad_a1w_inc_derivative(&a[l],inc);
    dco::derivative(a[l]) += 1.0;
    
    // nagad_a1w_ir_interpret_adjoint(ifail);
    DCO_MODE::global_tape->interpret_adjoint();
    
    for (int j=0; j<m; ++j) {
      int k = j*n + i;
      // drda[k] = nagad_a1w_get_derivative(a_in[j]);
      drda[k] = dco::derivative(a_in[j]);
    }
  }
  NagError  fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor,Nag_UpperMatrix,Nag_NonUnitDiag,n,n,r,n,
         "R from Q factorization of A",0,&fail);
  printf("\nDerivatives of diagonal of R w.r.t. first col of A\n");
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,m,drda,n,
         "dR_ii/dA_j1",0,&fail);

 END:
  // Remove computational data object and tape
  x10ab_a1w_f_(ad_handle,ifail);
  // nagad_a1w_ir_remove()
  DCO_TAPE_TYPE::remove(DCO_MODE::global_tape);

  delete [] a;
  delete [] a_in;
  delete [] tau;
  delete [] r;
  delete [] drda;
  
  return exit_status;
}