/* C05RC_P0W_F C++ Header Example Program.
*
* Copyright 2019 Numerical Algorithms Group.
* Mark 27, 2019.
*/
#include <nag.h>
#include <nagad.h>
#include <stdio.h>
#include <math.h>
#include <nagx02.h>
#include <nagx04.h>
#include <iostream>
#include <string>
using namespace std;
extern "C"
{
static void (NAG_CALL fcn)(void* &ad_handle, const Integer &n,
const double x[], double fvec[], double fjac[],
Integer iuser[], double ruser[], Integer &iflag);
}
int main (void)
{
// Scalars
int exit_status = 0;
const Integer maxfev = 1000, n = 7, nprint = 0;
cout << "C05RC_P0W_F C++ Header Example Program Results\n\n";
// problem parameters and starting value
double ruser[5], x[7];
ruser[0] = -1.0;
ruser[1] = 3.0;
ruser[2] = -2.0;
ruser[3] = -2.0;
ruser[4] = -1.0;
for (int i=0; i<n; ++i) {
x[i] = -1.0;
}
// Call passive routine
double diag[n], fjac[n*n], factor, fvec[n], qtf[n], r[n*(n+1)/2], xtol;
Integer iuser[1], nfev, njev;
xtol = sqrt(X02AJC);
factor = 100.;
for (int i=0; i<n; ++i) {
diag[i] = 1.;
}
Integer ifail = 0, mode = 2;
void *ad_handle = 0;
c05rc_p0w_f_(ad_handle,fcn,n,x,fvec,fjac,xtol,maxfev,mode,
diag,factor,nprint,nfev,njev,r,qtf,iuser,ruser,ifail);
cout.setf(ios::scientific,ios::floatfield);
cout.precision(3);
cout << " Solution:\n";
for (int i=0; i<n; ++i) {
cout.width(8);
cout << i+1;
cout.width(18);
cout << x[i] << endl;
}
return exit_status;
}
static void (NAG_CALL fcn)(void* &ad_handle, const Integer &n,
const double x[], double fvec[], double fjac[],
Integer iuser[], double ruser[], Integer &iflag)
{
if (iflag!=2) {
for (int i=0; i<n; ++i) {
fvec[i] = (ruser[1] + ruser[2]*x[i])*x[i] - ruser[4];
}
for (int i=1; i<n; ++i) {
fvec[i] = fvec[i] + ruser[0]*x[i-1];
}
for (int i=0; i<n-1; ++i) {
fvec[i] = fvec[i] + ruser[3]*x[i+1];
}
} else {
for (int i=0; i<n*n; ++i) {
fjac[i] = 0.0;
}
fjac[0] = ruser[1] + 2.0*ruser[2]*x[0];
fjac[n] = ruser[3];
for (int i=1; i < n-1; ++i) {
int k = i*n + i;
fjac[k-n] = ruser[0];
fjac[k] = ruser[1] + 2.0*ruser[2]*x[i];
fjac[k+n] = ruser[3];
}
fjac[n*n-n-1] = ruser[0];
fjac[n*n-1] = ruser[1] + 2.0*ruser[2]*x[n-1];
}
iflag = 0;
return;
}