E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentE04HDF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

E04HDF checks that a subroutine for calculating second derivatives of an objective function is consistent with a subroutine for calculating the corresponding first derivatives.

## 2  Specification

 SUBROUTINE E04HDF ( N, FUNCT, H, X, G, HESL, LH, HESD, IW, LIW, W, LW, IFAIL)
 INTEGER N, LH, IW(LIW), LIW, LW, IFAIL REAL (KIND=nag_wp) X(N), G(N), HESL(LH), HESD(N), W(LW) EXTERNAL FUNCT, H

## 3  Description

Routines for minimizing a function $F\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)$ of the variables ${x}_{1},{x}_{2},\dots ,{x}_{n}$ may require you to provide a subroutine to evaluate the second derivatives of $F$. E04HDF is designed to check the second derivatives calculated by such user-supplied subroutines. As well as the routine to be checked (H), you must supply a subroutine (FUNCT) to evaluate the first derivatives, and a point $x={\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)}^{\mathrm{T}}$ at which the checks will be made. Note that E04HDF checks routines of the form required for E04LBF.
E04HDF first calls user-supplied subroutines FUNCT and H to evaluate the first and second derivatives of $F$ at $x$. The user-supplied Hessian matrix ($H$, say) is projected onto two orthogonal vectors $y$ and $z$ to give the scalars ${y}^{\mathrm{T}}Hy$ and ${z}^{\mathrm{T}}Hz$ respectively. The same projections of the Hessian matrix are also estimated by finite differences, giving
 $p=yTgx+hy-yTgx/h and q=zTgx+hz-zTgx/h$
respectively, where $g\left(\right)$ denotes the vector of first derivatives at the point in brackets and $h$ is a small positive scalar. If the relative difference between $p$ and ${y}^{\mathrm{T}}Hy$ or between $q$ and ${z}^{\mathrm{T}}Hz$ is judged too large, an error indicator is set.

None.

## 5  Parameters

1:     N – INTEGERInput
On entry: the number $n$ of independent variables in the objective function.
Constraint: ${\mathbf{N}}\ge 1$.
2:     FUNCT – SUBROUTINE, supplied by the user.External Procedure
FUNCT must evaluate the function and its first derivatives at a given point. (E04LBF gives you the option of resetting parameters of FUNCT to cause the minimization process to terminate immediately. E04HDF will also terminate immediately, without finishing the checking process, if the parameter in question is reset.)
The specification of FUNCT is:
 SUBROUTINE FUNCT ( IFLAG, N, XC, FC, GC, IW, LIW, W, LW)
 INTEGER IFLAG, N, IW(LIW), LIW, LW REAL (KIND=nag_wp) XC(N), FC, GC(N), W(LW)
1:     IFLAG – INTEGERInput/Output
On entry: to FUNCT, IFLAG will be set to $2$.
On exit: if you set IFLAG to some negative number in FUNCT and return control to E04HDF, E04HDF will terminate immediately with IFAIL set to your setting of IFLAG.
2:     N – INTEGERInput
On entry: the number $n$ of variables.
3:     XC(N) – REAL (KIND=nag_wp) arrayInput
On entry: the point $x$ at which the function and first derivatives are required.
4:     FC – REAL (KIND=nag_wp)Output
On exit: unless FUNCT resets IFLAG, FC must be set to the value of the objective function $F$ at the current point $x$.
5:     GC(N) – REAL (KIND=nag_wp) arrayOutput
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:     IW(LIW) – INTEGER arrayWorkspace
7:     LIW – INTEGERInput
8:     W(LW) – REAL (KIND=nag_wp) arrayWorkspace
9:     LW – INTEGERInput
These parameters are present so that FUNCT will be of the form required by E04LBF. FUNCT is called with E04HDF's parameters IW, LIW, W, LW as these parameters. If the advice given in E04LBF 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 $5×{\mathbf{N}}$ elements of W.
FUNCT must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which E04HDF is called. Parameters denoted as Input must not be changed by this procedure.
E04HCF should be used to check the first derivatives calculated by FUNCT before E04HDF is used to check the second derivatives, since E04HDF assumes that the first derivatives are correct.
3:     H – SUBROUTINE, supplied by the user.External Procedure
H must evaluate the second derivatives of the function at a given point. (As with FUNCT, a parameter can be set to cause immediate termination.)
The specification of H is:
 SUBROUTINE H ( IFLAG, N, XC, FHESL, LH, FHESD, IW, LIW, W, LW)
 INTEGER IFLAG, N, LH, IW(LIW), LIW, LW REAL (KIND=nag_wp) XC(N), FHESL(LH), FHESD(N), W(LW)
1:     IFLAG – INTEGERInput/Output
On entry: is set to a non-negative number.
On exit: if H resets IFLAG to a negative number, E04HDF will terminate immediately with IFAIL set to your setting of IFLAG.
2:     N – INTEGERInput
On entry: the number $n$ of variables.
3:     XC(N) – REAL (KIND=nag_wp) arrayInput
On entry: the point $x$ at which the second derivatives of $F\left(x\right)$ are required.
4:     FHESL(LH) – REAL (KIND=nag_wp) arrayOutput
On exit: unless IFLAG is reset, H must place the strict lower triangle of the second derivative matrix of $F$ (evaluated at the point $x$) in FHESL, stored by rows, i.e., ${\mathbf{FHESL}}\left(\left(\mathit{i}-1\right)\left(\mathit{i}-2\right)/2+\mathit{j}\right)$ must be set to the value of $\frac{{\partial }^{2}F}{\partial {x}_{\mathit{i}}\partial {x}_{\mathit{j}}}$ at the point $x$, for $\mathit{i}=2,3,\dots ,n$ and $\mathit{j}=1,2,\dots ,\mathit{i}-1$. (The upper triangle is not required because the matrix is symmetric.)
5:     LH – INTEGERInput
On entry: the length of the array FHESL.
6:     FHESD(N) – REAL (KIND=nag_wp) arrayInput/Output
On entry: contains the value of $\frac{\partial F}{\partial {x}_{\mathit{j}}}$ at the point $x$, for $\mathit{j}=1,2,\dots ,n$. Routines written to take advantage of a similar feature of E04LBF can be tested as they stand by E04HDF.
On exit: unless IFLAG is reset, H must place the diagonal elements of the second derivative matrix of $F$ (evaluated at the point $x$) in FHESD, i.e., ${\mathbf{FHESD}}\left(\mathit{j}\right)$ must be set to the value of $\frac{{\partial }^{2}F}{\partial {x}_{\mathit{j}}^{2}}$ at the point $x$, for $\mathit{j}=1,2,\dots ,n$.
7:     IW(LIW) – INTEGER arrayWorkspace
8:     LIW – INTEGERInput
9:     W(LW) – REAL (KIND=nag_wp) arrayWorkspace
10:   LW – INTEGERInput
As in FUNCT, these parameters correspond to the parameters IW, LIW, W and LW of E04HDF. H must not change the first $5×{\mathbf{N}}$ elements of W.
H must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which E04HDF is called. Parameters denoted as Input must not be changed by this procedure.
4:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{X}}\left(\mathit{j}\right)$, for $\mathit{j}=1,2,\dots ,n$ must contain the coordinates of a suitable point at which to check the derivatives calculated by FUNCT. ‘Obvious’ settings, such as $0.0\text{​ 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 advisable that no two elements of X should be the same.
5:     G(N) – REAL (KIND=nag_wp) arrayOutput
On exit: unless you set IFLAG negative in the first call of FUNCT, ${\mathbf{G}}\left(\mathit{j}\right)$ contains the value of the first derivative $\frac{\partial F}{\partial {x}_{\mathit{j}}}$ at the point given in X, as calculated by FUNCT, for $\mathit{j}=1,2,\dots ,{\mathbf{N}}$.
6:     HESL(LH) – REAL (KIND=nag_wp) arrayOutput
On exit: unless you set IFLAG negative in H, HESL contains the strict lower triangle of the second derivative matrix of $F$, as evaluated by H at the point given in X, stored by rows.
7:     LH – INTEGERInput
On entry: the dimension of the array HESL as declared in the (sub)program from which E04HDF is called.
Constraint: ${\mathbf{LH}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{N}}×\left({\mathbf{N}}-1\right)/2\right)$.
8:     HESD(N) – REAL (KIND=nag_wp) arrayOutput
On exit: unless you set IFLAG negative in H, HESD contains the diagonal elements of the second derivative matrix of $F$, as evaluated by H at the point given in X.
9:     IW(LIW) – INTEGER arrayCommunication Array
This array is in the parameter list so that it can be used by other library routines for passing integer quantities to FUNCT or H. It is not examined or changed by E04HDF. In general you must provide an array IW, but are advised not to use it.
10:   LIW – INTEGERInput
On entry: the dimension of the array IW as declared in the (sub)program from which E04HDF is called.
Constraint: ${\mathbf{LIW}}\ge 1$.
11:   W(LW) – REAL (KIND=nag_wp) arrayCommunication Array
12:   LW – INTEGERInput
On entry: the dimension of the array W as declared in the (sub)program from which E04HDF is called.
Constraint: ${\mathbf{LW}}\ge 5×{\mathbf{N}}$.
13:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, because for this routine the values of the output parameters may be useful even if ${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Note: E04HDF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}<0$
A negative value of IFAIL indicates an exit from E04HDF because you have set IFLAG negative in FUNCT or H. The value of IFAIL will be the same as the value you set for IFLAG. The check on H will not have been completed.
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{N}}<1$, or ${\mathbf{LH}}<\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{N}}×\left({\mathbf{N}}-1\right)/2\right)$, or ${\mathbf{LIW}}<1$, or ${\mathbf{LW}}<5×{\mathbf{N}}$.
${\mathbf{IFAIL}}=2$
You should check carefully the derivation and programming of expressions for the second derivatives of $F\left(x\right)$, because it is very unlikely that H is calculating them correctly.

## 7  Accuracy

IFAIL is set to $2$ if
 $yTHy-p≥h×yTHy+1.0 or ​ zTHz-q≥h×zTHz+1.0$
where $h$ is set equal to $\sqrt{\epsilon }$ ($\epsilon$ being the machine precision as given by X02AJF) and other quantities are as defined in Section 3.

E04HDF calls H once and FUNCT three times.

## 9  Example

Suppose that it is intended to use E04LBF 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 second derivatives calculated by H required. (The call of E04HDF is preceded by a call of E04HCF to check FUNCT which calculates the first derivatives.)

### 9.1  Program Text

Program Text (e04hdfe.f90)

None.

### 9.3  Program Results

Program Results (e04hdfe.r)