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

NAG AD Library Introduction
Example description
/* nag::ad::g02aa Passive Example Program.
 */

#include <dco.hpp>
#include <iostream>
#include <nagad.h>

std::stringstream filecontent("4 \
 2.0   -1.0    0.0    0.0 \
-1.0    2.0   -1.0    0.0 \
 0.0   -1.0    2.0   -1.0 \
 0.0    0.0   -1.0    2.0 ");

// Function which calls NAG AD routines.
// Computes the Nearest Correlation Matrix X for an input matrix G.
template <typename T> void func(std::vector<T> &g, std::vector<T> &x);

int main()
{
  std::cout << " nag::ad::g02aa Passive Example Program Results\n";

  Integer n;
  filecontent >> n;

  // Input Matrix G, whose NCM we want to compute
  std::vector<double> gv(n * n);
  for (int i = 0; i < n; ++i)
    {
      for (int j = 0; j < n; ++j)
        {
          filecontent >> gv[i + j * n];
        }
    }

  // Output NCM X
  std::vector<double> xv(n * n);

  // Call the NAG AD Lib functions
  func(gv, xv);

  std::cout.setf(std::ios::scientific, std::ios::floatfield);
  std::cout.precision(5);
  std::cout << "\n Nearest Correlation Matrix:\n\n";
  for (int i = 0; i < n; i++)
    {
      for (int j = 0; j < n; j++)
        {
          std::cout.width(13);
          std::cout << xv[i + j * n];
        }
      std::cout << std::endl;
    }

  return 0;
}

// Function which calls NAG AD routines.
template <typename T> void func(std::vector<T> &g, std::vector<T> &x)
{
  Integer n   = sqrt(g.size());
  Integer pdg = n;
  Integer pdx = n;

  // Set up method parameters
  T       errtol = 1.00e-7;
  Integer maxits = 200;
  Integer maxit  = 10;
  // Output variables
  Integer iter, feval;
  T       nrmgrd;
  // Create AD configuration data object
  Integer ifail     = 0;
  nag::ad::handle_t ad_handle;

  // Routine for computing the NCM of G.
  nag::ad::g02aa(ad_handle, g.data(), pdg, n, errtol, maxits, maxit, x.data(),
                 pdx, iter, feval, nrmgrd, ifail);
}