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

NAG AD Library Introduction
Example description
/* E04NE_P0W_F C++ Header Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 * Mark 28.3, 2022.
 */

#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 << "E04NE_P0W_F C++ Header Example Program Results\n\n";

  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;
  }
  double * a = 0, *b = 0, *bl = 0, *bu = 0, *c = 0;
  double * cvec = 0, *clamda = 0, *work = 0, *x = 0, *rwsav = 0;
  Integer *istate = 0, *iwork = 0, *iwsav = 0, *kx = 0;
  logical *lwsav = 0;
  a              = new double[lda * sda];
  b              = new double[m];
  bl             = new double[lb];
  bu             = new double[lb];
  c              = new double[nclin * n];
  clamda         = new double[lb];
  x              = new double[n];
  kx             = new Integer[n];
  work           = new double[lwork];
  rwsav          = new double[475];
  lwsav          = new logical[120];
  istate         = new Integer[lb];
  iwork          = new Integer[liwork];
  iwsav          = new Integer[610];

  // Read problem parameters and register for differentiation
  for (int i = 0; i < m; i++)
  {
    for (int j = 0; j < n; j++)
    {
      Integer k = i + j * m;
      cin >> a[k];
    }
  }
  for (int i = 0; i < m; i++)
  {
    cin >> b[i];
  }
  for (int i = 0; i < nclin; i++)
  {
    for (int j = 0; j < n; j++)
    {
      Integer k = i + j * nclin;
      cin >> c[k];
    }
  }
  for (int i = 0; i < lb; i++)
  {
    cin >> bl[i];
  }
  for (int i = 0; i < lb; i++)
  {
    cin >> bu[i];
  }
  for (int i = 0; i < n; i++)
  {
    cin >> x[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;
  nag::ad::e04ne("Print Level = -1", lwsav, iwsav, rwsav, inform);

  // Solve the problem
  Integer iter;
  double  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
  cout.setf(ios::scientific, ios::floatfield);
  if (ifail == 0 || ifail > 1)
  {
    cout.precision(4);
    cout << "\n Optimal objective function value = ";
    cout.width(12);
    cout << obj << endl;
    cout << "\n Solution point: " << endl;
    for (int i = 0; i < n; i++)
    {
      cout.width(8);
      cout << i;
      cout.width(12);
      cout << x[i] << endl;
    }
  }
  else
  {
    cout << "nag::ad::e04nc failed with ifail = " << ifail << endl;
  }

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