# NAG AD Library Routine Document

## 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. Further implementations, for example for higher order differentiation or using the tangent linear approach, may become available at later marks of the NAG AD Library. The method of codifying AD implementations in routine name and corresponding argument types is described in the NAG AD Library Introduction.

## 1Purpose

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.

## 2Specification

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 (In) :: ad_handle
 Subroutine f ( ad_handle, x, ret, iuser, ruser)
 Integer, Intent (Inout) :: iuser(*) Type (nagad_a1w_w_rtype), Intent (Inout) :: x, ruser(*) Type (nagad_a1w_w_rtype), Intent (Out) :: ret Type (c_ptr), Intent (In) :: 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, nagad_a1w_w_rtype &x, nagad_a1w_w_rtype &ret, Integer iuser[], nagad_a1w_w_rtype ruser[]),nagad_a1w_w_rtype &x, Integer iuser[], nagad_a1w_w_rtype ruser[], Integer &ifail)

## 3Description

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.1Symbolic 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 NAG AD Library Introduction for API description on how to use symbolic adjoints.
The symbolic adjoints assumes
(i) successful computation of primal problem ($\mathbf{ifail}=0$ or $2$ on exit of c05ayf), i.e.,
 $fx^,p=0 ,$ (1)
where $\stackrel{^}{x}$ is a solution;
(ii) the first derivative at the solution is not equal zero
 $∇x fx^,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.2.3 in NAG AD Library Introduction and recall what primal and adjoint callbacks need to calculate in case of an algorithmic adjoint.
Assuming the original user-supplied function evaluates
 $z=fx,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:
(a) Function value evaluation only
(b) Function value evaluation and adjoint computation w.r.t. x only (corresponds to $x$ in the following equation), i.e.,
 $x 1 += ∇x fx,p T z1$ (4)
(c) Function value evaluation and adjoint computation w.r.t. $p$ only, i.e.,
 $p 1 += ∇p fx,p T z1 .$ (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.1Mathematical Background

The symbolic adjoint computes
 $z = - x1 ∇x fx^,p$
followed by an adjoint projection through the user-supplied adjoint routine
 $p 1 = p1 + ∇p fx^,p·z .$
Both ${\nabla }_{x}f\left(x,p\right)$ as well as ${\nabla }_{p}f\left(x,p\right)$ 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.2Usable 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.

## 4References

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

## 5Arguments

c05ay_a1w_f provides access to all the arguments available in the primal routine. There are also additional arguments specific to AD. A tooltip popup for each argument can be found by hovering over the argument name in Section 2 and a summary of the arguments are provided below:
• ad_handle – a handle to the AD configuration data object, as created by x10aa_a1w_f. Symbolic adjoint mode may be selected by calling x10ac_a1w_f with this handle.
• a$a$, the lower bound of the interval.
• b$b$, the upper bound of the interval.
• eps – the termination tolerance on $x$ (see [Description]).
• eta – a value such that if $|f\left(x\right)|\le \mathbf{eta}$, $x$ is accepted as the zero.
• f – this argument must evaluate the function $f$ whose zero is to be determined.
• x – on exit: if the function exits successfully or ifail = 2, this argument is the final approximation to the zero.
• iuser – may be used to pass information to user-supplied argument(s).
• ruser – may be used to pass information to user-supplied argument(s).
• ifail – on entry: ifail must be set to $\mathrm{0}$, $-\mathrm{1}\text{ or }\mathrm{1}$. on exit: ifail = 0 unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

c05ay_a1w_f preserves all error codes from c05ayf and in addition can return:
$\mathbf{ifail}=-89$
An unexpected AD error has been triggered by this routine. Please contact NAG.
See Section 5.2 in the NAG AD Library Introduction for further information.
$\mathbf{ifail}=-899$
Dynamic memory allocation failed for AD.
See Section 5.1 in the NAG AD Library Introduction for further information.
In symbolic mode the following may be returned:
$\mathbf{ifail}=4$
${\nabla }_{x}f\left(x,p\right)$ is zero. The adjoints have not been computed.

Not applicable.

## 8Parallelism and Performance

c05ay_a1w_f is not threaded in any implementation.

None.

## 10Example

The following examples are variants of the example for c05ayf , modified to demonstrate calling the NAG AD Library.
 Language Source File Data Results Fortan c05ay_a1w_fe.f90 c05ay_a1w_fe.d c05ay_a1w_fe.r C++ c05ay_a1w_hcppe.cpp c05ay_a1w_hcppe.d c05ay_a1w_hcppe.r
We assume the original user-supplied routine to compute
 $z = fx,p = e-x - x·p .$
Thus, in symbolic mode, the callback modes need to calculate
 (a) $z=f\left(x,p\right)={e}^{-x}-x·p$ (b) ${x}_{\left(1\right)}+={\left[{\nabla }_{x}f\left(x,p\right)\right]}^{\mathrm{T}}·{z}_{\left(1\right)}=\left({-e}^{-x}-p\right)·{z}_{\left(1\right)}$. (c) ${p}_{\left(1\right)}+={\left[{\nabla }_{p}f\left(x,p\right)\right]}^{\mathrm{T}}·{z}_{\left(1\right)}=-x·{z}_{\left(1\right)}$.
Please refer to Section 4.3 in NAG AD Library Introduction for details on implementation of the callback.