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

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


int main(void)
{
  /* Scalars */
  Integer exit_status = 0;

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

  // Create AD tape
  nagad_a1w_ir_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
  nagad_a1w_w_rtype             *a = 0, *tau = 0, *work = 0, *a_in = 0;
  double   *l = 0, *dlda = 0;
  a    = new nagad_a1w_w_rtype [m * n];
  work = new nagad_a1w_w_rtype [lwork];
  a_in = new nagad_a1w_w_rtype [m * n];
  tau  = new nagad_a1w_w_rtype[tau_len];
  l    = new double [m * m];
  dlda = new double [m * n];

  // 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 < n; i ++) {
    nagad_a1w_ir_register_variable(&a_in[i*m]);
  }
  for (int i = 0; i < m * n; i ++) {
    a[i] = a_in[i];
  }

  f08ah_a1w_f_(ad_handle, m, n, a, pda, tau, work, lwork, ifail);
  if (ifail != 0) {
    printf("Error from F08AH_A1W_F .\n%" NAG_IFMT " ", ifail);
    exit_status = 1;
    goto END;
  }

  for (int i = 0; i < m*m; ++i) {
    l[i] = nagad_a1w_get_value(a[i]);
  }

  for (int i = 0; i < m; ++i) {
    double d_one = 1.0;
    int ld = i*(m+1);
    nagad_a1w_ir_zero_adjoints();
    nagad_a1w_inc_derivative(&a[ld],d_one);

    nagad_a1w_ir_interpret_adjoint(ifail);

    for (int j=0; j<n; ++j) {
      int k = j*m + i;
      dlda[k] = nagad_a1w_get_derivative(a_in[j*m]);
    }
  }
  NagError  fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor,Nag_LowerMatrix,Nag_NonUnitDiag,m,m,l,m,
         "L from Q factorization of A",0,&fail);
  printf("\nDerivatives of diagonal of L w.r.t. first row of A\n");
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,m,n,dlda,m,
         "dL_ii/dA_j1",0,&fail);

 END:

  // Remove computational data object and tape
  x10ab_a1w_f_(ad_handle,ifail);
  nagad_a1w_ir_remove();
  delete [] a;
  delete [] a_in;
  delete [] tau;
  delete [] l;
  delete [] dlda;
  return exit_status;
}