/* E04DG_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 <string>
#include <iostream>
using namespace std;
extern "C"
{
static void NAG_CALL objfun(void* &ad_handle, Integer &mode,
const Integer &n, const double x[],
double &objf, double objgrd[],
const Integer &nstate, Integer iuser[],
double ruser[]);
}
int main(void)
{
int exit_status = 0;
double objf;
cout << "E04DG_P0W_F C++ Header Example Program Results\n\n";
Integer ifail = 0;
void *ad_handle = 0;
// Read problem parameters and register for differentiation
// Skip first line of data file
string mystr;
getline (cin, mystr);
Integer n;
cin >> n;
// passive routine fixed length array arguments
Integer iuser[1], iwsav[610];
double ruser[1], rwsav[475];
char cwsav[1];
logical lwsav[120];
const Charlen name_l = 6, cwsav_l = 1;
// passive routine variable length arrays
Integer *iwork = 0;
double *x = 0, *x_in = 0, *objgrd = 0, *work = 0;
iwork = new Integer [n+1];
x = new double [n];
x_in = new double [n];
objgrd = new double [n];
work = new double [13*n];
for (int i=0; i<n; i++) {
cin >> x[i];
}
// Initialize sav arrays
ifail = 0;
e04wb_p0w_f_("E04DGA",cwsav,1,lwsav,120,iwsav,610,rwsav,475,ifail,name_l,
cwsav_l);
// Solve the problem
Integer iter;
ifail = 0;
e04dg_p0w_f_(ad_handle,n,objfun,iter,objf,objgrd,x,iwork,work,iuser,
ruser,lwsav,iwsav,rwsav,ifail);
// Primal results
cout.setf(ios::scientific,ios::floatfield);
cout.precision(3);
cout << "\n Objective value = ";
cout.width(12); cout << objf;
cout << "\n Solution point = ";
for (int i=0; i<n; i++) {
cout.width(12); cout << x[i];
}
cout << "\n Estim gradient = ";
for (int i=0; i<n; i++) {
cout.width(12); cout << objgrd[i];
}
delete [] iwork;
delete [] x;
delete [] x_in;
delete [] objgrd;
delete [] work;
return exit_status;
}
static void NAG_CALL objfun(void* &ad_handle, Integer &mode,
const Integer &n, const double x[],
double &objf, double objgrd[],
const Integer &nstate, Integer iuser[],
double ruser[])
{
double x1, x2, y1, y2, expx1;
x1 = x[0];
x2 = x[1];
expx1 = exp(x1);
y1 = 2.0*x1;
y1 = y1 + x2;
y2 = x2 + 1.0;
x1 = y1*y1;
x2 = y2*y2;
x1 = x1 + x2;
objf = expx1*x1;
if (mode==2) {
y2 = y1 + y2;
y2 = 2.0*y2;
y1 = 4.0*y1;
y1 = expx1*y1;
objgrd[0] = y1 + objf;
objgrd[1] = expx1*y2;
}
return;
}