# NAG FL Interfacee04hcf (check_​deriv)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

e04hcf checks that a subroutine for evaluating an objective function and its first derivatives produces derivative values which are consistent with the function values calculated.

## 2Specification

Fortran Interface
 Subroutine e04hcf ( n, x, f, g, iw, liw, w, lw,
 Integer, Intent (In) :: n, liw, lw Integer, Intent (Inout) :: iw(liw), ifail Real (Kind=nag_wp), Intent (In) :: x(n) Real (Kind=nag_wp), Intent (Inout) :: w(lw) Real (Kind=nag_wp), Intent (Out) :: f, g(n) External :: funct
#include <nag.h>
 void e04hcf_ (const Integer *n, void (NAG_CALL *funct)(Integer *iflag, const Integer *n, const double xc[], double *fc, double gc[], Integer iw[], const Integer *liw, double w[], const Integer *lw),const double x[], double *f, double g[], Integer iw[], const Integer *liw, double w[], const Integer *lw, Integer *ifail)
The routine may be called by the names e04hcf or nagf_opt_check_deriv.

## 3Description

Routines for minimizing a function of several variables may require you to supply a subroutine to evaluate the objective function $F\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)$ and its first derivatives. e04hcf is designed to check the derivatives calculated by such user-supplied subroutines. As well as the routine to be checked ( funct), you must supply a point $x={\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)}^{\mathrm{T}}$ at which the check will be made. Note that e04hcf checks routines of the form required for e04kdf and e04lbf.
e04hcf first calls funct to evaluate $F$ and its first derivatives ${g}_{\mathit{j}}=\frac{\partial F}{\partial {x}_{\mathit{j}}}$, for $\mathit{j}=1,2,\dots ,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 ${g}^{\mathrm{T}}{p}_{1}$ and ${g}^{\mathrm{T}}{p}_{2}$ respectively. The same components are also estimated by finite differences, giving quantities
 $vk=F(x+hpk)-F(x)h, k=1,2$
where $h$ is a small positive scalar. If the relative difference between ${v}_{1}$ and ${g}^{\mathrm{T}}{p}_{1}$ or between ${v}_{2}$ and ${g}^{\mathrm{T}}{p}_{2}$ is judged too large, an error indicator is set.
None.

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: the number $n$ of independent variables in the objective function.
Constraint: ${\mathbf{n}}\ge 1$.
2: $\mathbf{funct}$Subroutine, supplied by the user. External Procedure
funct must evaluate the function and its first derivatives at a given point. (The minimization routines mentioned in Section 3 gives you the option of resetting arguments of funct to cause the minimization process to terminate immediately. e04hcf will also terminate immediately, without finishing the checking process, if the argument in question is reset.)
The specification of funct is:
Fortran Interface
 Subroutine funct ( n, xc, fc, gc, iw, liw, w, lw)
 Integer, Intent (In) :: n, liw, lw Integer, Intent (Inout) :: iflag, iw(liw) Real (Kind=nag_wp), Intent (In) :: xc(n) Real (Kind=nag_wp), Intent (Inout) :: w(lw) Real (Kind=nag_wp), Intent (Out) :: fc, gc(n)
 void funct (Integer *iflag, const Integer *n, const double xc[], double *fc, double gc[], Integer iw[], const Integer *liw, double w[], const Integer *lw)
1: $\mathbf{iflag}$Integer Input/Output
On entry: will be set to $2$.
On exit: if you reset iflag to a negative number in funct and return control to e04hcf, e04hcf will terminate immediately with ifail set to your setting of iflag.
2: $\mathbf{n}$Integer Input
On entry: the number $n$ of variables.
3: $\mathbf{xc}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the point $x$ at which $F$ and its derivatives are required.
4: $\mathbf{fc}$Real (Kind=nag_wp) Output
On exit: unless funct resets iflag, fc must be set to the value of the function $F$ at the current point $x$.
5: $\mathbf{gc}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: unless funct resets iflag, ${\mathbf{gc}}\left(\mathit{j}\right)$ must be set to the value of the first derivative $\frac{\partial F}{\partial {x}_{\mathit{j}}}$ at the point $x$, for $\mathit{j}=1,2,\dots ,n$.
6: $\mathbf{iw}\left({\mathbf{liw}}\right)$Integer array Workspace
7: $\mathbf{liw}$Integer Input
8: $\mathbf{w}\left({\mathbf{lw}}\right)$Real (Kind=nag_wp) array Workspace
9: $\mathbf{lw}$Integer Input
These arguments are present so that funct will be of the form required by the minimization routines mentioned in Section 3. funct is called with e04hcf's arguments iw, liw, w, lw as these arguments. If the advice given in the minimization routine documents is being followed, you will have no reason to examine or change any elements of iw or w. In any case, funct must not change the first $3×{\mathbf{n}}$ elements of w.
funct must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which e04hcf is called. Arguments denoted as Input must not be changed by this procedure.
Note: funct should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by e04hcf. If your code inadvertently does return any NaNs or infinities, e04hcf is likely to produce unexpected results.
3: $\mathbf{x}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\mathbf{x}}\left(\mathit{j}\right)$, for $\mathit{j}=1,2,\dots ,n$, must be set to the coordinates of a suitable point at which to check the derivatives calculated by funct. ‘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: $\mathbf{f}$Real (Kind=nag_wp) Output
On exit: unless you set iflag negative in the first call of funct, f contains the value of the objective function $F\left(x\right)$ at the point given by you in x.
5: $\mathbf{g}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: unless you set iflag negative in the first call of funct, ${\mathbf{g}}\left(\mathit{j}\right)$ contains the value of the derivative $\frac{\partial F}{\partial {x}_{\mathit{j}}}$ at the point given in x, as calculated by funct, for $\mathit{j}=1,2,\dots ,n$.
6: $\mathbf{iw}\left({\mathbf{liw}}\right)$Integer array Workspace
7: $\mathbf{liw}$Integer Input
This array is in the argument list so that it can be used by other library routines for passing integer quantities to funct. It is not examined or changed by e04hcf. Generally, you must provide an array iw but are advised not to use it.
Constraint: ${\mathbf{liw}}\ge 1$.
8: $\mathbf{w}\left({\mathbf{lw}}\right)$Real (Kind=nag_wp) array Workspace
9: $\mathbf{lw}$Integer Input
On entry: the dimension of the array w as declared in the (sub)program from which e04hcf is called.
Constraint: ${\mathbf{lw}}\ge 3×{\mathbf{n}}$.
10: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $-1$ is recommended since useful values can be provided in some output arguments even when ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
Note: in some cases e04hcf may return useful information.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{liw}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{liw}}\ge 1$.
On entry, ${\mathbf{lw}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lw}}\ge 3×{\mathbf{n}}$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=2$
Large errors were found in the derivatives of $F$ computed by funct.
${\mathbf{ifail}}<0$
User requested termination by setting iflag negative in funct.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

ifail is set to $2$ if
 $(vk-gTpk) 2≥h×( (gTpk) 2+1)$
for $k=1$ or $2$. (See Section 3 for definitions of the quantities involved.) The scalar $h$ is set equal to $\sqrt{\epsilon }$, where $\epsilon$ is the machine precision as given by x02ajf.

## 8Parallelism and Performance

e04hcf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

funct is called $3$ times.
Before using e04hcf to check the calculation of first derivatives, you should be confident that funct is calculating $F$ correctly. The usual way of checking the calculation of the function is to compare values of $F\left(x\right)$ calculated by funct at nontrivial points $x$ with values calculated independently. (‘Non-trivial’ means that, as when setting $x$ before calling e04hcf, coordinates such as $0.0$ or $1.0$ should be avoided.)
e04hcf only checks the derivatives calculated when ${\mathbf{iflag}}=2$. So, if funct is intended for use in conjunction with a minimization routine which may set iflag to $1$, you must check that, for given settings of the ${\mathbf{xc}}\left(j\right)$, funct produces the same values for the ${\mathbf{gc}}\left(j\right)$ when iflag is set to $1$ as when iflag is set to $2$.

## 10Example

Suppose that it is intended to use e04kdf to minimize
 $F= (x1+10x2) 2+5⁢ (x3-x4) 2+ (x2-2x3) 4+10⁢ (x1-x4) 4.$
The following program could be used to check the first derivatives calculated by funct. (The tests of whether ${\mathbf{iflag}}=0$ or $1$ in funct are present ready for when funct is called by e04kdf. e04hcf will always call funct with iflag set to 2.)

### 10.1Program Text

Program Text (e04hcfe.f90)

None.

### 10.3Program Results

Program Results (e04hcfe.r)