Example description
/* F08KE_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 = 0;
  NagError  fail;
  INIT_FAIL(fail);

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

  // Read matrix dimensions
  Integer m, n;
  cin >> m;
  cin >> n;

  // Allocate arrays containing A and its factorized form, B
  // and the solution X.
  Integer           lda = m, lwork=64*(m+n);
  nagad_t1w_w_rtype *a=0, *a_in=0, *d=0, *e=0, *taup=0, *tauq, *work=0;
  double            *dd=0, *de = 0;
  a     = new nagad_t1w_w_rtype [m*n];
  a_in  = new nagad_t1w_w_rtype [m*n];
  d     = new nagad_t1w_w_rtype [n];
  e     = new nagad_t1w_w_rtype [n-1];
  taup  = new nagad_t1w_w_rtype [n];
  tauq  = new nagad_t1w_w_rtype [n];
  work  = new nagad_t1w_w_rtype [lwork];
  dd    = new double [m*n];
  de    = new double [m*n-n];

  // Read the matrix A, register and copy
  double ddd;
  for (int i = 0; i<m; i++) {
    for (int j = 0; j<n; j++) {
      Integer k = i + j*m;
      cin >> ddd;
      a_in[k] = ddd;
    }
  }

  // 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<m; ++i) {
    nagad_t1w_inc_derivative(&a_in[i],inc);
    for (int j=0;j<m*n;++j) {
      a[j] = a_in[j];
    }
    // reduce A to bidiagonal form
    ifail = 0;
    f08ke_t1w_f_(ad_handle,m,n,a,lda,d,e,tauq,taup,work,lwork,ifail);
    nagad_t1w_set_derivative(&a_in[i],zero);
    for(int j=0;j<n;++j) {
      dd[j+i*n] = nagad_t1w_get_derivative(d[j]);
    }
    for(int j=0;j<n-1;++j) {
      de[j+i*(n-1)] = nagad_t1w_get_derivative(e[j]);
    }
  }

  // Print bidiagonal form
  cout.precision(4);
  cout << " Diagonal:" << endl;
  cout.width(12); cout << " ";
  for (int i=0; i<n; i++) {
    cout.width(11); cout << nagad_t1w_get_value(d[i]);
  }
  cout << endl;                 
  if (m>n) {
    cout << " Superdiagonal:" << endl;
  } else {
    cout << " Subdiagonal:" << endl;
  }
  cout.width(12); cout << " ";
  for (int i=0; i<n-1; i++) {
    cout.width(11); cout << nagad_t1w_get_value(e[i]);
  }

  cout << "\n\n Derivatives calculated: First order tangents\n";
  cout << " Computational mode    : algorithmic\n";
  
  cout << "\n Derivatives of d/e w.r.t first column of A\n";

  cout << "\n\n";
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,m,dd,n,
         "  dD_i/dA_j1",0,&fail);
  cout << "\n";
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n-1,m,de,n-1,
         " dE_i/DA_j1",0,&fail);

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

  delete [] a;
  delete [] a_in;
  delete [] d;
  delete [] e;
  delete [] taup;
  delete [] tauq;
  delete [] work;
  delete [] dd;
  delete [] de;
  return exit_status;
}