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

NAG AD Library Introduction
Example description
/* F11JC_P0W_F C++ Header Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 * Mark 28.3, 2022.
 */
#include <dco.hpp>
#include <iostream>
#include <nag.h>
#include <nagad.h>
#include <nagx04.h>
#include <stdio.h>
using namespace std;

int main()
{
  int               exit_status = 0;
  nag::ad::handle_t ad_handle;
  Integer           ifail = 0;

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

  // Read order of matrix and number of nonzero entries
  Integer n, nnz;
  cin >> n;
  cin >> nnz;

  Integer  la     = 3 * nnz;
  Integer  liwork = 2 * la + 7 * n + 1;
  Integer  lwork  = 6 * n + 120;
  double * a = 0, *x = 0, *y = 0, *work = 0;
  Integer *icol = 0, *ipiv = 0, *irow = 0, *istr = 0, *iwork = 0;

  a     = new double[la];
  x     = new double[n];
  y     = new double[n];
  work  = new double[lwork];
  icol  = new Integer[la];
  ipiv  = new Integer[n];
  irow  = new Integer[la];
  istr  = new Integer[n + 1];
  iwork = new Integer[liwork];

  double  dtol, dscale, tol;
  Integer lfill, maxitn;
  cin >> lfill >> dtol;
  cin >> dscale;
  cin >> tol >> maxitn;

  // Read the matrix A
  for (int i = 0; i < nnz; i++)
  {
    cin >> a[i] >> irow[i] >> icol[i];
  }

  // Read the vector y
  for (int i = 0; i < n; i++)
  {
    cin >> y[i];
  }
  // Read initial vector x
  for (int i = 0; i < n; i++)
  {
    cin >> x[i];
  }
  // Calculate incomplete Cholesky factorization
  Integer nnzc, npivm;
  ifail = 0;
  nag::ad::f11ja(ad_handle, n, nnz, a, la, irow, icol, lfill, dtol, "N", dscale,
                 "M", ipiv, istr, nnzc, npivm, iwork, liwork, ifail);

  // Solve Ax = y
  double  rnorm;
  Integer itn;
  ifail = 0;
  nag::ad::f11jc(ad_handle, "CG", n, nnz, a, la, irow, icol, ipiv, istr, y, tol,
                 maxitn, x, rnorm, itn, work, lwork, ifail);

  // Output results
  cout << " Converged in " << itn << " iterations" << endl;
  cout << " Final residual norm = " << rnorm << endl;
  cout.setf(ios::scientific, ios::floatfield);
  cout.precision(4);
  cout << endl;
  cout << "  Solution vector" << endl;
  for (int i = 0; i < n; ++i)
  {
    cout.width(12);
    cout << x[i] << endl;
  }

  delete[] a;
  delete[] x;
  delete[] y;
  delete[] work;
  delete[] icol;
  delete[] ipiv;
  delete[] irow;
  delete[] istr;
  delete[] iwork;

  return exit_status;
}