Settings help

## 1Purpose

c05ay is the AD Library version of the primal routine c05ayf. Based (in the C++ interface) on overload resolution, c05ay can be used for primal, tangent and adjoint evaluation. It supports tangents and adjoints of first and second order. The parameter ad_handle can be used to choose whether adjoints are computed using a symbolic adjoint or straightforward algorithmic differentiation. In addition, the routine has further optimisations when expert symbolic mode is selected.

## 2Specification

Fortran Interface
 Subroutine c05ay_AD_f ( ad_handle, a, b, eps, eta, f, x, iuser, ruser, ifail)
 Integer, Intent (Inout) :: iuser(*), ifail External :: f ADTYPE, Intent (In) :: a, b, eps, eta ADTYPE, Intent (Inout) :: ruser(*) ADTYPE, Intent (Out) :: x Type (c_ptr), Intent (Inout) :: ad_handle
Corresponding to the overloaded C++ function, the Fortran interface provides five routines with names reflecting the type used for active real arguments. The actual subroutine and type names are formed by replacing AD and ADTYPE in the above as follows:
#include <dco.hpp>
namespace nag {
}
}
The function is overloaded on ADTYPE which represents the type of active arguments. ADTYPE may be any of the following types:
double,
dco::ga1s<double>::type,
dco::gt1s<double>::type,
dco::gt1s<dco::gt1s<double>::type>::type,
dco::ga1s<dco::gt1s<double>::type>::type,

## 3Description

c05ay is the AD Library 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.

1. (i)successful computation of primal problem (${\mathbf{ifail}}={\mathbf{0}}$ or ${\mathbf{2}}$ on exit of c05ayf), i.e.,
 $f(x^,p)=0,$ (1)
where $\stackrel{^}{x}$ is a solution;
2. (ii)the first derivative at the solution is not equal zero
 $∇xf(x^,p)≠0.$ (2)
Here $p$ is a placeholder for any user variable either passed via ruser or via global variables.

#### 3.1.1Symbolic Mode

Symbolic mode may be selected by calling x10ac with mode set to nagad_symbolic prior to calling c05ay. No further changes are needed compared to using algorithmic mode.

#### 3.1.2Symbolic Expert Mode

Symbolic expert mode may be selected by calling x10ac with mode set to nagad_symbolic_expert prior to calling c05ay. In comparison to the symbolic mode, in nagad_symbolic_expert mode the user-supplied primal callback needs a specific implementation to support symbolic computation, but this can improve overall performance. See the example c05ay_a1_sym_expert_dcoe.cpp for details.

#### 3.1.3Mathematical Background

 $z=-x(1)∇xf(x^,p)$
 $p(1)=p(1)+∇pf(x^,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.

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

## 4References

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

## 5Arguments

In addition to the arguments present in the interface of the primal routine, c05ay 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.
On entry: a handle to the AD configuration data object, as created by x10aa. The computational mode may be selected by calling x10ac with this handle.
2: Input
3: Input
4: Input
5: 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(*) ADTYPE, Intent (In) :: x ADTYPE, Intent (Inout) :: ruser(*) ADTYPE, Intent (Out) :: retval Type (c_ptr), Intent (Inout) :: ad_handle
On entry: a handle to the AD configuration data object.
2: Input
On exit: the value of $f$ evaluated at x.
4: iuser – Integer array User Workspace
7: Output
8: liuser Input
User workspace dimension (C++ only), see x10af to specify the dimension from Fortran.
9: iuser($*$) – Integer array User Workspace
10: lruser Input
User workspace dimension (C++ only), see x10ae to specify the dimension from Fortran.
11: ruser($*$) – ADTYPE array User Workspace
12: ifail – Integer Input/Output

## 6Error Indicators and Warnings

c05ay preserves all error codes from c05ayf and in addition can return:
${\mathbf{ifail}}=-89$
See Section 4.8.2 in the NAG AD Library Introduction for further information.
${\mathbf{ifail}}=-199$
The routine was called using a mode that has not yet been implemented.
${\mathbf{ifail}}=-443$
This check is only made if the overloaded C++ interface is used with arguments not of type double.
${\mathbf{ifail}}=-444$
A C++ exception was thrown.
The error message will show the details of the C++ exception text.
${\mathbf{ifail}}=-899$
Dynamic memory allocation failed for AD.
See Section 4.8.1 in the NAG AD Library Introduction for further information.

Not applicable.

## 8Parallelism and Performance

c05ay is not threaded in any implementation.

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.

## 10Example

The following examples are variants of the example for c05ayf, modified to demonstrate calling the NAG AD Library.
Description of the primal example.
This example calculates an approximation to the zero of ${e}^{-x}-x$ within the interval $\left[0,1\right]$ using a tolerance of ${\mathbf{eps}}=\text{1.0E−5}$.

Language Source File Data Results
Fortran c05ay_a1w_fe.f90 c05ay_a1w_fe.d c05ay_a1w_fe.r
C++ c05ay_a1_algo_dcoe.cpp None c05ay_a1_algo_dcoe.r
C++ c05ay_a1_sym_dcoe.cpp None c05ay_a1_sym_dcoe.r
C++ c05ay_a1_sym_expert_dcoe.cpp None c05ay_a1_sym_expert_dcoe.r
C++ c05ay_a1t1_algo_dcoe.cpp None c05ay_a1t1_algo_dcoe.r
C++ c05ay_a1t1_sym_dcoe.cpp None c05ay_a1t1_sym_dcoe.r
C++ c05ay_a1t1_sym_expert_dcoe.cpp None c05ay_a1t1_sym_expert_dcoe.r

### 10.2Tangent modes

Language Source File Data Results
Fortran c05ay_t1w_fe.f90 c05ay_t1w_fe.d c05ay_t1w_fe.r
C++ c05ay_t1_dcoe.cpp None c05ay_t1_dcoe.r
C++ c05ay_t2_dcoe.cpp None c05ay_t2_dcoe.r

### 10.3Passive mode

Language Source File Data Results
Fortran c05ay_p0w_fe.f90 c05ay_p0w_fe.d c05ay_p0w_fe.r
C++ c05ay_passive_dcoe.cpp None c05ay_passive_dcoe.r