Example description
/* C05QD_T1W_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 <nagx02.h>
#include <nagx04.h>
#include <iostream>
#include <string>
using namespace std;

int main (void)
{
  // Scalars
  int               exit_status = 0;
  const Integer     mode = 2, n = 7;

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

  // problem parameters and starting value
  nagad_t1w_w_rtype ruser[5], x[7];

  ruser[0] = -1.0;
  ruser[1] =  3.0;
  ruser[2] = -2.0;
  ruser[3] = -2.0;
  ruser[4] = -1.0;

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

  // Call AD routine
  nagad_t1w_w_rtype diag[n], epsfcn, fjac[n*n], factor, fvec[n],
                    qtf[n], r[n*(n+1)/2], rwsav[4*n+10], xtol;
  double dr[5*n];
  Integer           irevcm, iwsav[17], ml, mu;

  xtol = sqrt(X02AJC);
  ml = 1;
  mu = 1;
  epsfcn = 0.;
  factor = 100.;

  for (int j=0; j<5; ++j) {
    for (int i=0; i<n; ++i) {
      x[i] = -1.0;
      diag[i] = 1.;
    }

    dco::derivative(ruser[j]) = 0.5;

    irevcm = 0;

    do {
      ifail = 0;
      c05qd_t1w_f_(ad_handle,irevcm,n,x,fvec,xtol,ml,mu,epsfcn,mode,
                   diag,factor,fjac,r,qtf,iwsav,rwsav,ifail);

      switch (irevcm) {
      case 1:
        // Monitoring exit
        continue;
      case 2:
        for (int i=0; i<n; ++i) {
          fvec[i] = (ruser[1] + ruser[2]*x[i])*x[i] - ruser[4];
        }
        for (int i=1; i<n; ++i) {
          fvec[i] = fvec[i] + ruser[0]*x[i-1];
        }
        for (int i=0; i<n-1; ++i) {
          fvec[i] = fvec[i] + ruser[3]*x[i+1];
        }
        break;
      }

    } while (irevcm != 0);

    for (int i=0; i<n; ++i) {
      dr[j*n + i] = 2.*dco::derivative(x[i]);
    }

    dco::derivative(ruser[j]) = 0.;
  }

  cout.setf(ios::scientific,ios::floatfield);
  cout.precision(4);
  cout << "           Solution:\n";
  for (int i=0; i<n; ++i) {
    cout.width(10);
    cout << i+1;
    cout.width(20);
    cout << dco::value(x[i]) << endl;
  }

  cout << "\n Derivatives calculated: First order tangents\n";
  cout << " Computational mode    : algorithmic\n";
  cout << "\n Derivatives are of solution w.r.t function params\n\n";

  NagError  fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,5,dr,n,
         "    dx/druser",0,&fail);

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

  return exit_status;
}