# NAG AD Library Routine Document

## e04gb_a1w_f (lsq_uncon_quasi_deriv_comp_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

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

## 2Specification

Fortran Interface
 Subroutine e04gb_a1w_f ( ad_handle, m, n, lsqlin, lsqfun, lsqmon, iprint, maxcal, eta, xtol, stepmx, x, fsumsq, fvec, fjac, ldfjac, s, v, ldv, niter, nf, iuser, ruser, ifail)
 Integer, Intent (In) :: m, n, iprint, maxcal, ldfjac, ldv Integer, Intent (Inout) :: iuser(*), ifail Integer, Intent (Out) :: niter, nf Type (nagad_a1w_w_rtype), Intent (In) :: eta, xtol, stepmx Type (nagad_a1w_w_rtype), Intent (Inout) :: x(n), fjac(ldfjac,n), v(ldv,n), ruser(*) Type (nagad_a1w_w_rtype), Intent (Out) :: fsumsq, fvec(m), s(n) Type (c_ptr), Intent (In) :: ad_handle External :: lsqlin, lsqfun, lsqmon
 Integer, Intent (Out) :: selct Type (c_ptr), Intent (In) :: ad_handle
 Subroutine lsqfun ( ad_handle, iflag, m, n, xc, fvec, fjac, ldfjac, iuser, ruser)
 Integer, Intent (In) :: m, n, ldfjac Integer, Intent (Inout) :: iflag, iuser(*) Type (nagad_a1w_w_rtype), Intent (Inout) :: xc(n), fjac(ldfjac,n), ruser(*), fvec(m) Type (c_ptr), Intent (In) :: ad_handle
 Subroutine lsqmon ( ad_handle, m, n, xc, fvec, fjac, ldfjac, s, igrade, niter, nf, iuser, ruser)
 Integer, Intent (In) :: m, n, ldfjac, igrade, niter, nf Integer, Intent (Inout) :: iuser(*) Type (nagad_a1w_w_rtype), Intent (Inout) :: xc(n), fvec(m), fjac(ldfjac,n), s(n), ruser(*) Type (c_ptr), Intent (In) :: ad_handle

## 3Description

e04gbf is a comprehensive quasi-Newton algorithm for finding an unconstrained minimum of a sum of squares of $m$ nonlinear functions in $n$ variables $\left(m\ge n\right)$. First derivatives are required. The routine is intended for functions which have continuous first and second derivatives (although it will usually work even if the derivatives have occasional discontinuities). For further information see Section 3 in the documentation for e04gbf .

e04gb_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.
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 the case of an algorithmic adjoint.
Assuming the original user-supplied function evaluates
 $z,g = fx,p, ∇x fx,p ,$ (1)
where $p$ is given by the w or by use of COMMON globals. The variables $x$, $z$ and $g$ correspond to xc, fvec and fjac of lsqfun. The symbolic adjoint of e04gbf then also requires the following capability / modes:
(a) Function value evaluation only.
(b) Function value evaluation and adjoint computation w.r.t. xc only (corresponds to $x$ in the following equation), i.e.,
 $x1 + = ∇x fx,p T z1 + ∇ x 2 fx,p T g1$ (2)
(c) Function value evaluation and adjoint computation w.r.t. $p$ only, i.e.,
 $p1 + = ∇p fx,p T z1 + ∇ x,p 2 fx,p T g1 .$ (3)
Here $p$ is a placeholder for any user variable either passed via the user segment of w or via COMMON global variables.

#### 3.1.1Mathematical Background

To be more specific, the symbolic adjoint solves
 $∇ x 2 Fx,p z = -x1$ (4)
 $p1k = ∑ j=1 n ∂2 Fx,p ∂xj ∂pk zj = 2 ∑ j=1 n ∑ i=1 m ∂ fi ∂ pk ∂ fi ∂ xj zj + fi ∂2 fi ∂xj ∂pk zj .$ (5)
The Hessian ${\nabla }_{x}^{2}F\left(x,p\right)$ as well as the mixed derivative tensor $\frac{{d}^{2}F\left(x,p\right)}{d{x}_{j}d{p}_{k}}$ is 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 arguments x, fvec, fjac and fsumsq. The adjoints of all other output arguments are ignored.
e04gb_a1w_f increments the adjoints of the variable $p$, where $p$ is given by the argument w or by use of COMMON globals (see (1)).

## 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

e04gb_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.
• m – the number $m$ of residuals, ${f}_{i}\left(x\right)$, and the number $n$ of variables, ${x}_{j}$.
• n – the number $m$ of residuals, ${f}_{i}\left(x\right)$, and the number $n$ of variables, ${x}_{j}$.
• lsqlin – this argument enables you to specify whether the linear minimizations (i.e., minimizations of $F\left({x}^{\left(k\right)}+{\alpha }^{\left(k\right)}{p}^{\left(k\right)}\right)$ with respect to ${\alpha }^{\left(k\right)}$) are to be performed by a routine which just requires the evaluation of the ${f}_{i}\left(x\right)$ (routine), or by a routine which also requires the first derivatives of the ${f}_{i}\left(x\right)$ (routine).
• lsqfunlsqfun must calculate the vector of values ${f}_{i}\left(x\right)$ and Jacobian matrix of first derivatives $\frac{\partial {f}_{i}}{\partial {x}_{j}}$ at any point $x$.
• lsqmon – If $\mathbf{iprint}\ge \mathrm{0}$, you must supply lsqmon which is suitable for monitoring the minimization process.
• iprint – the frequency with which lsqmon is to be called.
• maxcal – enables you to limit the number of times that lsqfun is called by routine.
• eta – every iteration of routine involves a linear minimization (i.e., minimization of $F\left({x}^{\left(k\right)}+{\alpha }^{\left(k\right)}{p}^{\left(k\right)}\right)$ with respect to ${\alpha }^{\left(k\right)}$).
• xtol – the accuracy in $x$ to which the solution is required.
• stepmx – an estimate of the Euclidean distance between the solution and the starting point supplied by you.
• x – on entry: $\mathbf{x}\left(\mathit{j}\right)$ must be set to a guess at the $\mathit{j}$th component of the position of the minimum, for $\mathit{j}=\mathrm{1},2, \dots ,n$. on exit: the final point ${x}^{\left(k\right)}$.
• fsumsq – on exit: the value of $F\left(x\right)$, the sum of squares of the residuals ${f}_{i}\left(x\right)$, at the final point given in x.
• fvec – on exit: the value of the residual ${f}_{\mathit{i}}\left(x\right)$ at the final point given in x, for $\mathit{i}=\mathrm{1},2, \dots ,m$.
• fjac – on exit: the value of the first derivative $\frac{\partial {f}_{\mathit{i}}}{\partial {x}_{\mathit{j}}}$ evaluated at the final point given in x, for $\mathit{j}=\mathrm{1},2, \dots ,n$, for $\mathit{i}=\mathrm{1},2, \dots ,m$.
• ldfjac – the first dimension of the array fjac.
• s – on exit: the singular values of the Jacobian matrix at the final point.
• v – on exit: the matrix $V$ associated with the singular value decomposition. $J=US{V}^{T}$. of the Jacobian matrix at the final point, stored by columns.
• ldv – the first dimension of the array v.
• niter – on exit: the number of iterations which have been performed in routine.
• nf – on exit: the number of times that the residuals have been evaluated (i.e., the number of calls of lsqfun).
• 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

e04gb_a1w_f preserves all error codes from e04gbf and in addition can return:
$\mathbf{ifail}=-89$
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}=5$
In attempting to compute the symbolic adjoint a singular Hessian was encountered and the computation could not proceed.
$\mathbf{ifail}=6$
In attempting to compute the symbolic adjoint a Hessian was encountered with reciprocal condition number less than machine precision; the computation did therefore not proceed.

Not applicable.

## 8Parallelism and Performance

e04gb_a1w_f is not threaded in any implementation.