NAG Library Manual, Mark 30
Interfaces:  FL   CL   CPP   AD 

NAG AD Library Introduction
Example description
/* E04NC_A1W_F C++ Header Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 * Mark 30.0, 2024.
 */

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

int main()
{
  // Scalars
  int exit_status = 0;

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

  // Create AD tape
  dco::ga1s<double>::global_tape = dco::ga1s<double>::tape_t::create();

  // Create AD configuration data object
  Integer           ifail = 0;
  nag::ad::handle_t ad_handle;

  // Skip first line of data file
  string mystr;
  getline(cin, mystr);

  // Read problem sizes
  Integer m, n, nclin;
  cin >> m;
  cin >> n;
  cin >> nclin;

  Integer liwork = n;
  Integer lda = m, sda = n, ldc = nclin, lb = n + nclin;
  Integer lwork;

  lwork = 9 * n;
  if (nclin > 0)
  {
    lwork = lwork + 2 * n * n + 6 * nclin;
  }
  nagad_a1w_w_rtype *a = 0, *b_in = 0, *b = 0, *bl = 0, *bu = 0, *c = 0;
  nagad_a1w_w_rtype *cvec = 0, *clamda = 0, *work = 0, *x = 0, *rwsav = 0;
  Integer *          istate = 0, *iwork = 0, *iwsav = 0, *kx;
  logical *          lwsav = 0;
  double *           db = 0, *dbl = 0, *dbu = 0;
  a      = new nagad_a1w_w_rtype[lda * sda];
  b      = new nagad_a1w_w_rtype[m];
  b_in   = new nagad_a1w_w_rtype[m];
  bl     = new nagad_a1w_w_rtype[lb];
  bu     = new nagad_a1w_w_rtype[lb];
  c      = new nagad_a1w_w_rtype[nclin * n];
  clamda = new nagad_a1w_w_rtype[lb];
  x      = new nagad_a1w_w_rtype[n];
  kx     = new Integer[n];
  work   = new nagad_a1w_w_rtype[lwork];
  rwsav  = new nagad_a1w_w_rtype[475];
  lwsav  = new logical[120];
  istate = new Integer[lb];
  iwork  = new Integer[liwork];
  iwsav  = new Integer[610];
  db     = new double[m];
  dbl    = new double[lb];
  dbu    = new double[lb];

  // Read problem parameters and register for differentiation
  double yr;
  for (int i = 0; i < m; i++)
  {
    for (int j = 0; j < n; j++)
    {
      Integer k = i + j * m;
      cin >> yr;
      a[k] = yr;
    }
  }
  for (int i = 0; i < m; i++)
  {
    cin >> yr;
    b_in[i] = yr;
  }
  for (int i = 0; i < nclin; i++)
  {
    for (int j = 0; j < n; j++)
    {
      Integer k = i + j * nclin;
      cin >> yr;
      c[k] = yr;
    }
  }
  for (int i = 0; i < lb; i++)
  {
    cin >> yr;
    bl[i] = yr;
  }
  for (int i = 0; i < lb; i++)
  {
    cin >> yr;
    bu[i] = yr;
  }
  for (int i = 0; i < n; i++)
  {
    cin >> yr;
    x[i] = yr;
  }

  for (int i = 0; i < m; i++)
  {
    dco::ga1s<double>::global_tape->register_variable(b_in[i]);
  }
  for (int i = 0; i < lb; i++)
  {
    dco::ga1s<double>::global_tape->register_variable(bl[i]);
    dco::ga1s<double>::global_tape->register_variable(bu[i]);
  }

  // Initialize sav arrays
  ifail = 0;
  char cwsav[1];
  nag::ad::e04wb("E04NCA", cwsav, 1, lwsav, 120, iwsav, 610, rwsav, 475, ifail);
  // Set option via string
  Integer inform = 0;
  nag::ad::e04ne("Print Level = -1", lwsav, iwsav, rwsav, inform);

  // Solve the problem
  for (int i = 0; i < m; i++)
  {
    b[i] = b_in[i];
  }
  Integer           iter;
  nagad_a1w_w_rtype obj;
  ifail = -1;
  nag::ad::e04nc(ad_handle, m, n, nclin, ldc, lda, c, bl, bu, cvec, istate, kx,
                 x, a, b, iter, obj, clamda, iwork, liwork, work, lwork, lwsav,
                 iwsav, rwsav, ifail);

  // Primal results
  double inc = 1.0;
  cout.setf(ios::scientific, ios::floatfield);
  if (ifail == 0 || ifail > 1)
  {
    cout.precision(4);
    cout << "\n Optimal objective function value = ";
    cout.width(12);
    cout << dco::value(obj) << endl;
    cout << "\n Solution point: " << endl;
    for (int i = 0; i < n; i++)
    {
      cout.width(8);
      cout << i;
      cout.width(12);
      cout << dco::value(x[i]) << endl;
    }
  }
  else
  {
    cout << "nag::ad::e04nc failed with ifail = " << ifail << endl;
    goto END;
  }

  cout << "\n Derivatives calculated: First order adjoints\n";
  cout << " Computational mode    : algorithmic\n\n";
  cout << " Derivatives:\n\n";

  // Setup evaluation of derivatives of fsumsq via adjoints.
  dco::derivative(obj) += inc;
  dco::ga1s<double>::global_tape->sparse_interpret() = true;
  dco::ga1s<double>::global_tape->interpret_adjoint();

  for (int i = 0; i < m; ++i)
  {
    db[i] = dco::derivative(b_in[i]);
  }
  for (int i = 0; i < lb; ++i)
  {
    dbl[i] = dco::derivative(bl[i]);
    dbu[i] = dco::derivative(bu[i]);
  }
  cout << "\nDerivatives of obj w.r.t. B, BL and BU\n\n";
  NagError fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, 1, m, db, 1,
         "  dobj/dB", 0, &fail);
  cout << endl;
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, 1, lb, dbl, 1,
         "  dobj/dBL", 0, &fail);
  cout << endl;
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, 1, lb, dbu, 1,
         "  dobj/dBU", 0, &fail);
END:

  dco::ga1s<double>::tape_t::remove(dco::ga1s<double>::global_tape);

  delete[] a;
  delete[] b;
  delete[] b_in;
  delete[] bl;
  delete[] bu;
  delete[] c;
  delete[] clamda;
  delete[] x;
  delete[] kx;
  delete[] work;
  delete[] rwsav;
  delete[] lwsav;
  delete[] istate;
  delete[] iwork;
  delete[] iwsav;
  delete[] db;
  delete[] dbl;
  delete[] dbu;
  return exit_status;
}