NAG CL Interface
e04hcc (check_​deriv)

Settings help

CL Name Style:


1 Purpose

e04hcc checks that a user-defined C function for evaluating an objective function and its first derivatives produces derivative values which are consistent with the function values calculated.

2 Specification

#include <nag.h>
void  e04hcc (Integer n,
void (*objfun)(Integer n, const double x[], double *objf, double g[], Nag_Comm *comm),
const double x[], double *objf, double g[], Nag_Comm *comm, NagError *fail)
The function may be called by the names: e04hcc or nag_opt_check_deriv.

3 Description

The function e04kbc for minimizing a function of several variables requires you to supply a C function to evaluate the objective function F ( x 1 , x 2 ,, x n ) and its first derivatives. e04hcc is designed to check the derivatives calculated by such a user-supplied function. As well as the function to be checked (objfun), you must supply a point x = ( x 1 , x 2 ,, x n ) T at which the check is to be made.
e04hcc first calls the supplied function objfun to evaluate F and its first derivatives g j = F xj , for j=1,2,,n at x . The components of the user-supplied derivatives along two orthogonal directions (defined by unit vectors p 1 and p 2 , say) are then calculated; these will be gT p 1 and gT p 2 respectively. The same components are also estimated by finite differences, giving quantities
v k = F (x+ hp k ) - F (x) h ,   k = 1 , 2  
where h is a small positive scalar. If the relative difference between v 1 and gT p 1 or between v 2 and gT p 2 is judged too large, an error indicator is set.

4 References

None.

5 Arguments

1: n Integer Input
On entry: the number n of independent variables in the objective function.
Constraint: n1 .
2: objfun function, supplied by the user External Function
objfun must evaluate the objective function and its first derivatives at a given point. (The minimization function e04kbc gives you the option of resetting an argument, commflag , to terminate the minimization process immediately. e04hcc will also terminate immediately, without finishing the checking process, if the argument in question is reset to a negative value.)
The specification of objfun is:
void  objfun (Integer n, const double x[], double *objf, double g[], Nag_Comm *comm)
1: n Integer Input
On entry: the number n of variables.
2: x[n] const double Input
On entry: the point x at which F and its derivatives are required.
3: objf double * Output
On exit: objfun must set objf to the value of the objective function F at the current point x . If it is not possible to evaluate F then objfun should assign a negative value to commflag ; e04hcc will then terminate.
4: g[n] double Output
On exit: unless commflag is reset to a negative number, objfun must set g[j-1] to the value of the first derivative F xj at the current point x for j = 1 , 2 , , n
5: comm Nag_Comm *
Pointer to structure of type Nag_Comm; the following members are relevant to objfun.
flagIntegerInput/Output
On entry: commflag will be set to 2.
On exit: if objfun resets commflag to some negative number then e04hcc will terminate immediately with the error indicator NE_USER_STOP. If fail is supplied to e04hcc, fail.errnum will be set to your setting of commflag.
firstNag_BooleanInput
On entry: will be set to Nag_TRUE on the first call to objfun and Nag_FALSE for all subsequent calls.
nfIntegerInput
On entry: the number of calculations of the objective function; this value will be equal to the number of calls made to objfun including the current one.
userdouble *
iuserInteger *
pPointer 
The type Pointer will be void * with a C compiler that defines void * and char * otherwise. Before calling e04hcc these pointers may be allocated memory and initialized with various quantities for use by objfun when called from e04hcc.
Note: objfun should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by e04hcc. If your code inadvertently does return any NaNs or infinities, e04hcc is likely to produce unexpected results.
The array x must not be changed by objfun.
3: x[n] const double Input
On entry: x[j-1] , for j=1,2,,n, must be set to the coordinates of a suitable point at which to check the derivatives calculated by objfun. ‘Obvious’ settings, such as 0.0 or 1.0, should not be used since, at such particular points, incorrect terms may take correct values (particularly zero), so that errors could go undetected. Similarly, it is preferable that no two elements of x should be the same.
4: objf double * Output
On exit: unless you set commflag negative in the first call of objfun, objf contains the value of the objective function F (x) at the point given in x.
5: g[n] double Output
On exit: unless you set commflag negative in the first call of objfun, g[j-1] contains the value of the derivative F xj at the point given in x, as calculated by objfun, for j=1,2,,n.
6: comm Nag_Comm * Input/Output
Note: comm is a NAG defined type (see Section 3.1.1 in the Introduction to the NAG Library CL Interface).
On entry/exit: structure containing pointers for communication with the user-defined function; see the above description of objfun for details. If you do not need to make use of this communication feature the null pointer NAGCOMM_NULL may be used in the call to e04hcc; comm will then be declared internally for use in calls to objfun.
7: fail NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

6 Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_DERIV_ERRORS
Large errors were found in the derivatives of the objective function.
You should check carefully the derivation and programming of expressions for the derivatives of F (x) , because it is very unlikely that objfun is calculating them correctly.
NE_INT_ARG_LT
On entry, n=value.
Constraint: n1.
NE_USER_STOP
User requested termination, user flag value =value .
This exit occurs if you set commflag to a negative value in objfun. If fail is supplied the value of fail.errnum will be the same as your setting of commflag . The check on objfun will not have been completed.

7 Accuracy

fail is set to NE_DERIV_ERRORS if
( v k -gT p k ) 2 h × ( (gT p k ) 2 +1)  
for k=1 or 2. (See Section 3 for definitions of the quantities involved.) The scalar h is set equal to ε , where ε is the machine precision as given by X02AJC.

8 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
e04hcc is not threaded in any implementation.

9 Further Comments

The user-defined function objfun is called three times.
Before using e04hcc to check the calculation of first derivatives, you should be confident that objfun is calculating F correctly. The usual way of checking the calculation of the function is to compare values of F (x) calculated by objfun at non-trivial points x with values calculated independently. (‘Non-trivial’ means that, as when setting x before calling e04hcc, coordinates such as 0.0 or 1.0 should be avoided.)

10 Example

Suppose that it is intended to use e04kbc to minimize
F = ( x 1 +10 x 2 ) 2 + 5 ( x 3 - x 4 ) 2 + ( x 2 -2 x 3 ) 4 + 10 ( x 1 - x 4 ) 4 .  
The following program could be used to check the first derivatives calculated by the required function objfun. (The test of whether commflag 0 in objfun is present for when objfun is called by e04kbc. e04hcc will always call objfun with commflag set to 2.)

10.1 Program Text

Program Text (e04hcce.c)

10.2 Program Data

None.

10.3 Program Results

Program Results (e04hcce.r)