Example description
/* F07CE_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 <string>
#include <iostream>
using namespace std;

int main(void)
{
  int       exit_status = 0;
  void      *ad_handle = 0;
  Integer   nrhs = 1, ifail = 0;
  NagError  fail;
  INIT_FAIL(fail);

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

  // Read problem size and number of right-hand-sides 
  Integer n;
  cin >> n;
  cin >> nrhs;

  // Allocate arrays containing A and its factorized form, B
  // and the solution X.
  nagad_t1w_w_rtype *dl=0, *d=0, *du=0, *du2=0, *b=0;
  nagad_t1w_w_rtype *dlf=0, *df=0, *duf=0, *x=0;
  double            *sol=0, *dxdu=0, *dxdd=0, *dxdl=0, *dxdb=0;
  Integer           *ipiv = 0;
  Integer           n1 = n-1, n2 = n-2;
  dl   = new nagad_t1w_w_rtype [n1];
  d    = new nagad_t1w_w_rtype [n];
  du   = new nagad_t1w_w_rtype [n1];
  du2  = new nagad_t1w_w_rtype [n2];
  dlf  = new nagad_t1w_w_rtype [n1];
  df   = new nagad_t1w_w_rtype [n];
  duf  = new nagad_t1w_w_rtype [n1];
  b    = new nagad_t1w_w_rtype [n*nrhs];
  ipiv = new Integer           [n];
  x    = new nagad_t1w_w_rtype [n*n];
  sol  = new double            [n*n];
  dxdu = new double            [n*n1];
  dxdd = new double            [n*n];
  dxdl = new double            [n*n1];
  dxdb = new double            [n*n];

  // Read the tridiagonal matrix A and right hand side B, register and copy
  double dd;
  for (int i = 0; i<n1; i++) {
    cin >> dd;
    du[i] = dd;
  }
  for (int i = 0; i<n; i++) {
    cin >> dd;
    d[i] = dd;
  }
  for (int i = 0; i<n1; i++) {
    cin >> dd;
    dl[i] = dd;
  }
  for (int i = 0; i<n; i++) {
    for (int j = 0; j<nrhs; j++) {
      cin >> dd;
      int k = i + j*n;
      b[k] = dd;
    }
  }

  // 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<4*n-2; ++i) {
    int k = i;
    if (i<n1) {
      nagad_t1w_inc_derivative(&du[i],inc);
    } else if (i<n+n1) {
      k = i - n1;
      nagad_t1w_inc_derivative(&d[k],inc);
    } else if (i<n+n1+n1) {
      k = i - n - n1;
      nagad_t1w_inc_derivative(&dl[k],inc);
    } else {
      k = i - n - n1 - n1;
      nagad_t1w_inc_derivative(&b[k],inc);
    }
    for (int j=0; j<n1; ++j) {
      dlf[j] = dl[j];
      df[j]  = d[j];
      duf[j] = du[j];
      x[j]   = b[j];
    }
    df[n1] = d[n1]; x[n1] = b[n1];
    // Factorize the tridiagonal matrix A
    ifail = 0;
    f07cd_t1w_f_(ad_handle,n,dlf,df,duf,du2,ipiv,ifail);

    // Solve the equations Ax = b for x
    ifail = 0;
    f07ce_t1w_f_(ad_handle,"N",n,nrhs,dlf,df,duf,du2,ipiv,x,n,ifail,1);

    if (i==0) {
      // Print primal solution
      for (int j=0; j<n*nrhs; ++j) {
        sol[j] = nagad_t1w_get_value(x[j]);
      }
      cout << "\n\n";
      x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,nrhs,sol,n,
         "  Solution",0,&fail);
    }
    if (i<n1) {
      nagad_t1w_set_derivative(&du[k],zero);
      for (int j=0; j<n; ++j) {
        dxdu[j+k*n] = nagad_t1w_get_derivative(x[j]);
      }
    } else if (i<n+n1) {
      nagad_t1w_set_derivative(&d[k],zero);
      for (int j=0; j<n; ++j) {
        dxdd[j+k*n] = nagad_t1w_get_derivative(x[j]);
      }
    } else if (i<n+n1+n1) {
      nagad_t1w_set_derivative(&dl[k],zero);
      for (int j=0; j<n; ++j) {
        dxdl[j+k*n] = nagad_t1w_get_derivative(x[j]);
      }
    } else {
      nagad_t1w_set_derivative(&b[k],zero);
      for (int j=0; j<n; ++j) {
        dxdb[j+k*n] = nagad_t1w_get_derivative(x[j]);
      }
    }
  }
  cout << "\n\n Derivatives calculated: First order tangents\n";
  cout << " Computational mode    : algorithmic\n";
  cout << "\n Derivatives of first solution column w.r.t. inputs:\n";
  
  cout << "\n";
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,n1,dxdu,n,
         "  d(du(i))/dx(j)",0,&fail);
  cout << "\n";
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,n,dxdd,n,
         "  d(d(i))/dx(j)",0,&fail);
  cout << "\n";
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,n1,dxdl,n,
         "  d(dl(i))/dx(j)",0,&fail);
  cout << "\n";
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,n,dxdb,n,
         "  d(b(i))/dx(j)",0,&fail);

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

  delete [] dl;
  delete [] d;
  delete [] du;
  delete [] du2;
  delete [] dlf;
  delete [] df;
  delete [] duf;
  delete [] b;
  delete [] ipiv;
  delete [] x;
  delete [] sol;
  delete [] dxdu;
  delete [] dxdd;
  delete [] dxdl;
  delete [] dxdb;
  return exit_status;
}