E04HDF (PDF version)
E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

E04HDF

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.

 Contents

    1  Purpose
    7  Accuracy

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 Fx1,x2,,xn of the variables x1,x2,,xn 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=x1,x2,,xnT 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 yTHy and zTHz 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 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 yTHy or between q and zTHz is judged too large, an error indicator is set.

4  References

None.

5  Parameters

1:     N – INTEGERInput
On entry: the number n of independent variables in the objective function.
Constraint: N1.
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:     XCN – 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:     GCN – REAL (KIND=nag_wp) arrayOutput
On exit: unless FUNCT resets IFLAG, GCj must be set to the value of the first derivative F xj  at the point x, for j=1,2,,n.
6:     IWLIW – INTEGER arrayWorkspace
7:     LIW – INTEGERInput
8:     WLW – 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×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:     XCN – REAL (KIND=nag_wp) arrayInput
On entry: the point x at which the second derivatives of Fx are required.
4:     FHESLLH – 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., FHESLi-1i-2/2+j must be set to the value of 2F xixj  at the point x, for i=2,3,,n and j=1,2,,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:     FHESDN – REAL (KIND=nag_wp) arrayInput/Output
On entry: contains the value of F xj  at the point x, for j=1,2,,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., FHESDj must be set to the value of 2F xj2  at the point x, for j=1,2,,n.
7:     IWLIW – INTEGER arrayWorkspace
8:     LIW – INTEGERInput
9:     WLW – 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×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:     XN – REAL (KIND=nag_wp) arrayInput
On entry: Xj, for j=1,2,,n must contain 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 advisable that no two elements of X should be the same.
5:     GN – REAL (KIND=nag_wp) arrayOutput
On exit: unless you set IFLAG negative in the first call of FUNCT, Gj contains the value of the first derivative F xj  at the point given in X, as calculated by FUNCT, for j=1,2,,N.
6:     HESLLH – 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: LHmax1,N×N-1/2.
8:     HESDN – 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:     IWLIW – 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: LIW1.
11:   WLW – 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: LW5×N.
13:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ 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​ 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 IFAIL0 on exit, the recommended value is -1. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -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:
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.
IFAIL=1
On entry,N<1,
or LH < max1,N×N-1/2 ,
orLIW<1,
orLW<5×N.
IFAIL=2
You should check carefully the derivation and programming of expressions for the second derivatives of Fx, because it is very unlikely that H is calculating them correctly.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

7  Accuracy

IFAIL is set to 2 if
yTHy-ph×yTHy+1.0  or ​ zTHz-qh×zTHz+1.0  
where h is set equal to ε (ε being the machine precision as given by X02AJF) and other quantities are as defined in Section 3.

8  Parallelism and Performance

E04HDF is not threaded by NAG in any implementation.
E04HDF 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.

9  Further Comments

E04HDF calls H once and FUNCT three times.

10  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.)

10.1  Program Text

Program Text (e04hdfe.f90)

10.2  Program Data

None.

10.3  Program Results

Program Results (e04hdfe.r)


E04HDF (PDF version)
E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015