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

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

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

// Function which calls NAG AD Library routines.
// Computes the solution to a system of linear equations Ax=b where A is a
// triagonal matrix of size n. Matrix A is stored in arrays l(n-1), d(n), u(n-1)
// that store the lower, the main and the upper diagonals.
template <typename T>
void func(std::vector<T> &l,
          std::vector<T> &d,
          std::vector<T> &u,
          std::vector<T> &x);

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

  // Problem dimension
  Integer n = 5;
  // Matrix A stored in diagonals
  std::vector<double> lv = {3.4, 3.6, 7.0, -6.0};
  std::vector<double> dv = {3.0, 2.3, -5.0, -0.9, 7.1};
  std::vector<double> uv = {2.1, -1.0, 1.9, 8.0};
  // Right-hand-side vector b
  std::vector<double> bv = {2.7, -0.5, 2.6, 0.6, 2.7};
  // Computed solution to the system Ax=b
  std::vector<double> xv(n);

  // f07ca modifies rhs b and returns solution x into the same array
  xv = bv;

  // Call the NAG AD Lib functions
  func(lv, dv, uv, xv);

  std::cout << "\n Solution point = ";
  for (int i = 0; i < n; i++)
  {
    std::cout.width(5);
    std::cout << xv[i];
  }
  std::cout << std::endl;

  return 0;
}

// Function which calls NAG AD Library routines.
template <typename T>
void func(std::vector<T> &l,
          std::vector<T> &d,
          std::vector<T> &u,
          std::vector<T> &x)
{
  Integer n = x.size(), nrhs = 1;

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

  // Solve the equations Ax = b for x
  ifail = 0;
  nag::ad::f07ca(ad_handle, n, nrhs, l.data(), d.data(), u.data(), x.data(), n,
                 ifail);

  ifail = 0;
}