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

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

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

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

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

  // Read matrix dimensions
  Integer m, n;
  cin >> m >> n;

  // Allocate arrays containing A and its factorized form, B
  // and the solution X.
  Integer lda = m, lwork = 64 * (m + n);
  double *a = 0, *d = 0, *e = 0, *taup = 0, *tauq, *work = 0;
  a    = new double[m * n];
  d    = new double[n];
  e    = new double[n - 1];
  taup = new double[n];
  tauq = new double[n];
  work = new double[lwork];

  // Read the matrix A, register and copy
  double ddd;
  for (int i = 0; i < m; i++)
  {
    for (int j = 0; j < n; j++)
    {
      Integer k = i + j * m;
      cin >> a[k];
    }
  }

  // reduce A to bidiagonal form
  ifail = 0;
  nag::ad::f08ke(ad_handle, m, n, a, lda, d, e, tauq, taup, work, lwork, ifail);

  // Print bidiagonal form
  cout.precision(4);
  cout << " Diagonal:" << endl;
  cout.width(12);
  cout << " ";
  for (int i = 0; i < n; i++)
  {
    cout.width(11);
    cout << d[i];
  }
  cout << endl;
  if (m > n)
  {
    cout << " Superdiagonal:" << endl;
  }
  else
  {
    cout << " Subdiagonal:" << endl;
  }
  cout.width(12);
  cout << " ";
  for (int i = 0; i < n - 1; i++)
  {
    cout.width(11);
    cout << e[i];
  }
  cout << endl;

  delete[] a;
  delete[] d;
  delete[] e;
  delete[] taup;
  delete[] tauq;
  delete[] work;
  return exit_status;
}