NAG AD Library
c05ay_a1w_f (contfn_brent_a1w)

Note: a1w denotes that first order adjoints are computed in working precision; this has the corresponding argument type nagad_a1w_w_rtype. Also available is the t1w (first order tangent linear) mode, the interface of which is implied by replacing a1w by t1w throughout this document. Additionally, the p0w (passive interface, as alternative to the FL interface) mode is available and can be inferred by replacing of active types by the corresponding passive types. The method of codifying AD implementations in the routine name and corresponding argument types is described in the NAG AD Library Introduction.
Settings help

AD Name Style:


AD Specification Language:

1 Purpose

c05ay_a1w_f is the adjoint version of the primal routine c05ayf. Depending on the value of ad_handle, c05ay_a1w_f uses algorithmic differentiation or symbolic adjoints to compute adjoints of the primal.

2 Specification

Fortran Interface
Subroutine c05ay_a1w_f ( ad_handle, a, b, eps, eta, f, x, iuser, ruser, ifail)
Integer, Intent (Inout) :: iuser(*), ifail
External :: f
Type (nagad_a1w_w_rtype), Intent (In) :: a, b, eps, eta
Type (nagad_a1w_w_rtype), Intent (Inout) :: ruser(*)
Type (nagad_a1w_w_rtype), Intent (Out) :: x
Type (c_ptr), Intent (Inout) :: ad_handle
C++ Header Interface
#include <nagad.h>
void c05ay_a1w_f_ ( void *&ad_handle, const nagad_a1w_w_rtype &a, const nagad_a1w_w_rtype &b, const nagad_a1w_w_rtype &eps, const nagad_a1w_w_rtype &eta,
void (NAG_CALL f)(void *&ad_handle, const nagad_a1w_w_rtype &x, nagad_a1w_w_rtype &retval, Integer iuser[], nagad_a1w_w_rtype ruser[]),
nagad_a1w_w_rtype &x, Integer iuser[], nagad_a1w_w_rtype ruser[], Integer &ifail)
The routine may be called by the names c05ay_a1w_f or nagf_roots_contfn_brent_a1w. The corresponding t1w and p0w variants of this routine are also available.

3 Description

c05ay_a1w_f is the adjoint version of the primal routine c05ayf.
c05ayf locates a simple zero of a continuous function in a given interval using Brent's method, which is a combination of nonlinear interpolation, linear extrapolation and bisection. For further information see Section 3 in the documentation for c05ayf.

3.1 Symbolic Adjoints

c05ay_a1w_f can provide symbolic adjoints by setting the symbolic mode as described in Section 3.2.2 in the X10 Chapter introduction. Please see Section 4 in the Introduction to the NAG AD Library for API description on how to use symbolic adjoints.
The symbolic adjoints assumes
  1. (i)successful computation of primal problem (ifail=0 or 2 on exit of c05ayf), i.e.,
    f(x^,p)=0,(1)
    where x^ is a solution;
  2. (ii)the first derivative at the solution is not equal zero
    xf(x^,p)0.(2)
In comparison to the algorithmic adjoint, the user-supplied primal and adjoint callbacks need specific implementation to support symbolic adjoint computation. Please see Section 4.5 in the Introduction to the NAG AD Library and recall what primal and adjoint callbacks need to calculate in case of an algorithmic adjoint.
Assuming the original user-supplied function evaluates
z=f(x,p),(3)
where p is given by ruser or by use of COMMON globals. The variables x and z correspond to x of user-supplied subroutine f and return the value of f respectively.
The symbolic adjoint of c05ayf then requires in addition the following capability / modes:
  1. (a)Function value evaluation only
  2. (b)Function value evaluation and adjoint computation w.r.t. x only (corresponds to x in the following equation), i.e.,
    x(1)+=[xf(x,p)]Tz(1)(4)
  3. (c)Function value evaluation and adjoint computation w.r.t. p only, i.e.,
    p(1)+=[pf(x,p)]Tz(1).(5)
Here p is a placeholder for any user variable either passed via ruser or via COMMON global variables.
The library routine communicates via ad_config to the primal callback which version to provide, given the following API.

3.1.1 Mathematical Background

The symbolic adjoint computes
z=-x(1)xf(x^,p) 
followed by an adjoint projection through the user-supplied adjoint routine
p(1)=p(1)+pf(x^,p)·z. 
Both xf(x,p) as well as pf(x,p) are computed using the user-supplied adjoint routine.
Please see Du Toit and Naumann (2017), Naumann et al. (2017) and Giles (2017) for reference.

3.1.2 Usable Adjoints

You can set or access the adjoints of output argument x. The adjoints of all other output arguments are ignored.
c05ay_a1w_f increments the adjoints of the variable p, where p is given by the argument ruser or by use of COMMON globals (see (3)).
The adjoints of all other input parameters are not referenced.

4 References

Brent R P (1973) Algorithms for Minimization Without Derivatives Prentice–Hall
Du Toit J, Naumann U (2017) Adjoint Algorithmic Differentiation Tool Support for Typical Numerical Patterns in Computational Finance
Giles M (2017) Collected Matrix Derivative Results for Forward and Reverse Mode Algorithmic Differentiation
Naumann U, Lotz J, Leppkes K and Towara M (2017) Algorithmic Differentiation of Numerical Methods: Tangent and Adjoint Solvers for Parameterized Systems of Nonlinear Equations

5 Arguments

In addition to the arguments present in the interface of the primal routine, c05ay_a1w_f includes some arguments specific to AD.
A brief summary of the AD specific arguments is given below. For the remainder, links are provided to the corresponding argument from the primal routine. A tooltip popup for all arguments can be found by hovering over the argument name in Section 2 and in this section.
1: ad_handle – Type (c_ptr) Input/Output
On entry: a handle to the AD configuration data object, as created by x10aa_a1w_f. Symbolic adjoint mode may be selected by calling x10aa_a1w_f with this handle.
2: aType (nagad_a1w_w_rtype) Input
3: bType (nagad_a1w_w_rtype) Input
4: epsType (nagad_a1w_w_rtype) Input
5: etaType (nagad_a1w_w_rtype) Input
6: f – Subroutine External Procedure
Note that f is a subroutine in this interface, returning the function value via the additional output parameter retval.
The specification of f is:
Fortran Interface
Subroutine f ( ad_handle, x, retval, iuser, ruser)
Integer, Intent (Inout) :: iuser(*)
Type (nagad_a1w_w_rtype), Intent (In) :: x
Type (nagad_a1w_w_rtype), Intent (Inout) :: ruser(*)
Type (nagad_a1w_w_rtype), Intent (Out) :: retval
Type (c_ptr), Intent (Inout) :: ad_handle
C++ Header Interface
#include <nagad.h>
void f ( void *&ad_handle, const nagad_a1w_w_rtype &x, nagad_a1w_w_rtype &retval, Integer iuser[], nagad_a1w_w_rtype ruser[])
1: ad_handle – Type (c_ptr) Input/Output
On entry: a handle to the AD configuration data object.
2: xType (nagad_a1w_w_rtype) Input
3: retvalType (nagad_a1w_w_rtype) Output
On exit: the value of f evaluated at x.
4: iuser – Integer array User Workspace
5: ruserType (nagad_a1w_w_rtype) array User Workspace
7: xType (nagad_a1w_w_rtype) Output
8: iuser(*) – Integer array User Workspace
9: ruser(*) – Type (nagad_a1w_w_rtype) array User Workspace
10: ifail – Integer Input/Output

6 Error Indicators and Warnings

c05ay_a1w_f preserves all error codes from c05ayf and in addition can return:
ifail=-89
An unexpected AD error has been triggered by this routine. Please contact NAG.
See Section 4.8.2 in the NAG AD Library Introduction for further information.
ifail=-899
Dynamic memory allocation failed for AD.
See Section 4.8.1 in the NAG AD Library Introduction for further information.
In symbolic mode the following may be returned:

7 Accuracy

Not applicable.

8 Parallelism and Performance

c05ay_a1w_f is not threaded in any implementation.

9 Further Comments

Please note that the algorithmic adjoint of Brent's method may be ill-conditioned. This means that derivatives of the zero returned in x, with respect to function parameters stored in ruser, may have limited accuracy when computed in algorithmic mode. This routine can be used in symbolic mode which will compute accurate derivatives.

10 Example

The following examples are variants of the example for c05ayf, modified to demonstrate calling the NAG AD Library.

10.1 Adjoint mode (a1w)

LanguageSource FileDataResults
Fortranc05ay_a1w_fe.f90c05ay_a1w_fe.dc05ay_a1w_fe.r
C++c05ay_a1w_hcppe.cppc05ay_a1w_hcppe.dc05ay_a1w_hcppe.r

10.2 Tangent mode (t1w)

LanguageSource FileDataResults
Fortranc05ay_t1w_fe.f90c05ay_t1w_fe.dc05ay_t1w_fe.r
C++c05ay_t1w_hcppe.cppc05ay_t1w_hcppe.dc05ay_t1w_hcppe.r

10.3 Passive mode (p0w)

LanguageSource FileDataResults
Fortranc05ay_p0w_fe.f90c05ay_p0w_fe.dc05ay_p0w_fe.r
C++c05ay_p0w_hcppe.cppc05ay_p0w_hcppe.dc05ay_p0w_hcppe.r
We assume the original user-supplied routine to compute
z=f(x,p)=e-x-x·p. 
Thus, in symbolic mode, the callback modes need to calculate
  1. (a)z=f(x,p)=e-x-x·p
  2. (b)x(1)+=[xf(x,p)]T·z(1)=(-e-x-p)·z(1).
  3. (c)p(1)+=[pf(x,p)]T·z(1)=-x·z(1).
Please refer to Section 4.6 in the Introduction to the NAG AD Library for details on implementation of the callback.