Example description
/* F06YA_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 <math.h>
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;

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

  cout << "F06YA_T1W_F C++ Header Example Program Results\n\n";
  // Skip heading in data file
  string mystr;
  getline (cin, mystr);

  // Read problem size and parameters
  Integer m, n, k;
  cin >> m >> n >> k;
  double alphar, betar;
  cin >> alphar >> betar;
  nagad_t1w_w_rtype alpha, beta;
  alpha = alphar;
  beta  = betar;
  
  // Allocate arrays containing A and its factorized form, B
  // and the solution X.
  nagad_t1w_w_rtype *a=0, *b=0, *c=0;
  double            *ar=0, *br=0, *cr=0, *da=0, *db=0;
  a    =  new nagad_t1w_w_rtype [k*m];
  b    =  new nagad_t1w_w_rtype [n*k];
  c    =  new nagad_t1w_w_rtype [m*n];
  ar   =  new double [k*m];
  br   =  new double [n*k];
  cr   =  new double [m*n];
  da   =  new double [m*n];
  db   =  new double [m*n];
  
  // Read the matrix A
  double dd;
  for (int i = 0; i<k; ++i) {
    for (int j = 0; j<m; ++j) {
      cin >> dd;
      Integer l = i + j*k;
      ar[l] = dd;
      a[l] = dd;
    }
  }
  for (int i = 0; i<n; ++i) {
    for (int j = 0; j<k; ++j) {
      cin >> dd;
      Integer l = i + j*n;
      br[l] = dd;
      b[l] = dd;
    }
  }
  for (int i = 0; i<m*n; ++i) {
    cr[i] = 1.0;
    c[i] = 1.0;
  }

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

  double inc = 1.0;
  nagad_t1w_inc_derivative(&alpha,inc);
  //  y = beta*y + alpha*A^T*b
  ifail = 0;
  f06ya_t1w_f_(ad_handle,"T","T",m,n,k,alpha,a,k,b,n,beta,c,m,ifail,1,1);

  for (int i = 0; i<m*n; ++i) {
    da[i] = nagad_t1w_get_derivative(c[i]);
    c[i] = 1.0;
  }
  for (int i = 0; i<m*k; ++i) {
    a[i] = ar[i];
  }
  for (int i = 0; i<n*k; ++i) {
    b[i] = br[i];
  }

  double zero = 0.0;
  nagad_t1w_set_derivative(&alpha,zero);
  
  nagad_t1w_inc_derivative(&beta,inc);
  //  y = beta*y + alpha*A^T*b
  ifail = 0;
  f06ya_t1w_f_(ad_handle,"T","T",m,n,k,alpha,a,k,b,n,beta,c,m,ifail,1,1);

  for (int i = 0; i<m*n; ++i) {
    cr[i] = nagad_t1w_get_value(c[i]);
    db[i] = nagad_t1w_get_derivative(c[i]);
  }

  cout << endl;
  NagError  fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,m,n,cr,m,
         "  Matrix C after update",0,&fail);

  cout << "\n\n Derivatives calculated: First order tangents\n";
  cout << " Computational mode    : algorithmic\n";
  cout << "\n Derivatives of C w.r.t alpha and beta:\n";

  // Print derivatives
  cout << endl;
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,m,n,da,m,
         "           dC/dalpha",0,&fail);
  cout << endl;
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,m,n,db,m,
         "           dC/dbeta",0,&fail);

  // Remove computational data object
  x10ab_t1w_f_(ad_handle,ifail);

  delete [] a;
  delete [] b;
  delete [] c;
  delete [] ar;
  delete [] br;
  delete [] cr;
  delete [] da;
  delete [] db;



  return exit_status;
}