Example description
/* F08KP_T1W_F C++ Header Example Program.
 *
 * Copyright 2019 Numerical Algorithms Group.
 * Mark 27, 2019.
 */

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

int main(void)
{
  int       exit_status = 0;
  void      *ad_handle = 0;
  Integer   ifail = 0;
  NagError  fail;
  INIT_FAIL(fail);

  cout << "F08KP_T1W_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;
  cin >> n;

  // Allocate arrays containing A and its factorized form, B
  // and the solution X.
  Integer           lda = m, ldu = m, ldvt = n, lwork;
  nagad_t1w_w_ctype *a=0, *u=0, *vt=0, *work=0, dummy[1];
  nagad_t1w_w_rtype *ar=0, *ai=0, *s=0, *rwork=0;
  Complex           *uc=0, *vtc=0, *dsda=0;
  Charlen           lena = 1;
  a     = new nagad_t1w_w_ctype [m*n];
  ar    = new nagad_t1w_w_rtype [m*n];
  ai    = new nagad_t1w_w_rtype [m*n];
  s     = new nagad_t1w_w_rtype [m];
  rwork = new nagad_t1w_w_rtype [5*n];
  dsda  = new Complex           [n*m];
  u     = new nagad_t1w_w_ctype [m*m];
  vt    = new nagad_t1w_w_ctype [n*n];

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

  // Create AD configuration data object
  ifail = 0;
  x10aa_t1w_f_(ad_handle,ifail);

  // Use routine workspace query to get optimal workspace.
  ifail = 0;
  lwork = -1;
  f08kp_t1w_f_(ad_handle,"A","A",m,n,a,lda,s,u,ldu,vt,ldvt,dummy,lwork,rwork,
               ifail,lena,lena);

  lwork = (Integer) nagad_t1w_get_value(real(dummy[0])) + 1;
  work = new nagad_t1w_w_ctype [lwork];

  double inc = 1.0, zero = 0.0;
  for (int i = 0; i<2*m; i++) {
    if (i<m) {
      nagad_t1w_inc_derivative(&ar[i],inc);
    } else {
      nagad_t1w_inc_derivative(&ai[i-m],inc);
    }
    for (int j=0; j<m*n; ++j) {
      a[j].real(ar[j]);
      a[j].imag(ai[j]);
    }
    //  Compute the singular values and left and right singular vectors
    //  of A (A = U*S*(V**T), m < n)
    f08kp_t1w_f_(ad_handle,"A","A",m,n,a,lda,s,u,ldu,vt,ldvt,work,lwork,rwork,
                 ifail,lena,lena);

    if (i<m) {
      nagad_t1w_set_derivative(&ar[i],zero);
      for (int j=0; j<n; j++) {
        dsda[j+n*i].re = nagad_t1w_get_derivative(s[j]);
      }
    } else {
      nagad_t1w_set_derivative(&ai[i-m],zero);
      for (int j=0; j<n; j++) {
        dsda[j+n*(i-m)].im = nagad_t1w_get_derivative(s[j]);
      }
    }
  }

  // Print primal solution
  cout.precision(4);
  cout.width(12); cout << " ";
  cout << " Singular values:\n";
  for (int i=0; i<n; i++) {
    cout.width(11); cout << nagad_t1w_get_value(s[i]);
  }

  // Copy primal values to array for printing
  uc     = new Complex [m*m];
  vtc    = new Complex [n*n];

  for (int i=0; i<m*m; i++) {
    uc[i].re = nagad_t1w_get_value(real(u[i]));
    uc[i].im = nagad_t1w_get_value(imag(u[i]));
  }
  for (int i=0; i<n*n; i++) {
    vtc[i].re = nagad_t1w_get_value(real(vt[i]));
    vtc[i].im = nagad_t1w_get_value(imag(vt[i]));
  }
    
  cout << "\n\n";
  x04dac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,m,m,uc,m,
         "Left singular vectors by column",0,&fail);
  cout << "\n";
  x04dac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,n,vtc,n,
         "Right singular vectors by row",0,&fail);

  cout << "\n\n Derivatives calculated: First order tangents\n";
  cout << " Computational mode    : algorithmic\n";
  
  cout << "\n Derivatives of Singular values w.r.t first column of A\n";
  cout << "\n";
  x04dac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,m,dsda,n,
         " dS_i/dA_j1",0,&fail);

  // Remove computational data object
  ifail = 0;
  x10ab_t1w_f_(ad_handle,ifail);

  delete [] a;
  delete [] ar;
  delete [] ai;
  delete [] s;
  delete [] u;
  delete [] vt;
  delete [] work;
  delete [] rwork;
  delete [] uc;
  delete [] vtc;
  delete [] dsda;

  return exit_status;
}