# NAG CPP Interfacenagcpp::opt::handle_set_nlnhess (e04rl)

## 1Purpose

handle_set_nlnhess is a part of the NAG optimization modelling suite and defines the structure of the Hessians of the nonlinear objective and constraints, on assumption that they are present in the problem. Alternatively, it may be used to define the Hessian of the Lagrangian.

## 2Specification

```#include "e04/nagcpp_e04rl.hpp"
#include "e04/nagcpp_class_CommE04RA.hpp"
```
```template <typename COMM, typename IROWH, typename ICOLH>

void function handle_set_nlnhess(COMM &comm, const types::f77_integer idf, const IROWH &irowh, const ICOLH &icolh, OptionalE04RL opt)```
```template <typename COMM, typename IROWH, typename ICOLH>

void function handle_set_nlnhess(COMM &comm, const types::f77_integer idf, const IROWH &irowh, const ICOLH &icolh)```

## 3Description

After the initialization function handle_​init has been called and an objective function $f$ or nonlinear constraint function ${g}_{i}$ has been registered with handle_​set_​nlnconstr and handle_​set_​nlnobj, handle_set_nlnhess can be used to define the sparsity structure of the Hessians, $H$, of those functions (i.e., the second partial derivatives with respect to the decision variables) or a linear combination of them, called the Lagrangian.
• Defining ${\nabla }^{2}f\equiv \left(\begin{array}{cccc}\frac{{\partial }^{2}f}{{\partial }^{2}{x}_{1}}& \frac{{\partial }^{2}f}{\partial {x}_{2}\partial {x}_{1}}& \dots & \frac{{\partial }^{2}f}{\partial {x}_{n}\partial {x}_{1}}\\ \frac{{\partial }^{2}f}{\partial {x}_{1}\partial {x}_{2}}& \frac{{\partial }^{2}f}{{\partial }^{2}{x}_{2}}& \dots & \frac{{\partial }^{2}f}{\partial {x}_{n}\partial {x}_{2}}\\ ⋮& ⋮& \ddots & ⋮\\ \frac{{\partial }^{2}f}{\partial {x}_{1}\partial {x}_{n}}& \frac{{\partial }^{2}f}{\partial {x}_{2}\partial {x}_{n}}& \dots & \frac{{\partial }^{2}f}{{\partial }^{2}{x}_{n}}\end{array}\right)$;
• the Hessian of the Lagrangian function $\equiv \sigma {\nabla }^{2}f+\sum _{i=1}^{m}{\lambda }_{i}{\nabla }^{2}{g}_{i}$;
• the Hessian of the objective function $\equiv {\nabla }^{2}f$;
• the Hessian of the constraint functions $\equiv {\nabla }^{2}{g}_{i}$.
Each of the symmetric $n×n$ Hessian matrices will have its own sparsity structure, in general. These structures can be given in separate handle_set_nlnhess calls, or merged together in the Lagrangian and given in one call.
The nonzero values of the Hessians at particular points in the decision variable space will be communicated to the NLP solver by user-supplied functions (e.g., hess for handle_​solve_​ipopt).
Some NLP solvers (e.g., handle_​solve_​ipopt) expect either all of the Hessians (for objective and nonlinear constraints) to be supplied or none and they will terminate with an error indicator if only some but not all of the Hessians have been introduced by handle_set_nlnhess.
Some NLP solvers (e.g., handle_​solve_​ipopt, again) will automatically switch to using internal approximations for the Hessians if none have been introduced by handle_set_nlnhess. This usually results in a slower convergence (more iterations to the solution) and might even result in no solution being attainable within the ordinary tolerances.
See Section 3.1 in the E04 Chapter Introduction for more details about the NAG optimization modelling suite.

None.

## 5Arguments

1: $\mathbf{comm}$CommE04RA Input/Output
Communication structure. An object of either the derived class CommE04RA or its base class NoneCopyableComm can be supplied. It is recommended that the derived class is used. If the base class is supplied it must first be initialized via a call to opt::handle_init (e04ra).
2: $\mathbf{idf}$types::f77_integer Input
On entry: specifies the quantities for which a sparsity structure is provided in nnzh, irowh and icolh.
${\mathbf{idf}}=-1$
The sparsity structure of the Hessian of the Lagrangian is provided.
${\mathbf{idf}}=0$
The sparsity structure of the Hessian of the objective function is provided.
${\mathbf{idf}}>0$
The sparsity structure of the Hessian of the idfth constraint function is provided.
The value of idf will also determine how an NLP solver will call the user-supplied functions that evaluate these nonzeros at particular points of the decision variable space, i.e., whether the solver will expect the nonzero values of the objective and constraint Hessians in separate calls or merged in the Lagrangian Hessian, in one call. See, for example, hess of handle_​solve_​ipopt.
Constraint: $-1\le {\mathbf{idf}}\le \mathit{ncnln}$.
Note: $\mathit{ncnln}$, the number of nonlinear constraints registered with the handle.
3: $\mathbf{irowh}\left({\mathbf{nnzh}}\right)$types::f77_integer array Input
On entry: arrays irowh and icolh store the nonzeros of the upper triangle of the matrix $H$ in coordinate storage (CS) format (see Section 2.1.1 in the F11 Chapter Introduction). irowh specifies one-based row indices, icolh specifies one-based column indices and specifies the values of the nonzero elements in such a way that ${h}_{ij}=\mathit{H}\left(l-1\right)$ where $i={\mathbf{irowh}}\left(l-1\right)$ and $j={\mathbf{icolh}}\left(\mathit{l}-1\right)$, for $\mathit{l}=1,2,\dots ,{\mathbf{nnzh}}$. No particular order is expected, but elements should not repeat.
Constraint: $1\le {\mathbf{irowh}}\left(\mathit{l}-1\right)\le {\mathbf{icolh}}\left(\mathit{l}-1\right)\le n$, for $\mathit{l}=1,2,\dots ,{\mathbf{nnzh}}$.
4: $\mathbf{icolh}\left({\mathbf{nnzh}}\right)$types::f77_integer array Input
On entry: arrays irowh and icolh store the nonzeros of the upper triangle of the matrix $H$ in coordinate storage (CS) format (see Section 2.1.1 in the F11 Chapter Introduction). irowh specifies one-based row indices, icolh specifies one-based column indices and specifies the values of the nonzero elements in such a way that ${h}_{ij}=\mathit{H}\left(l-1\right)$ where $i={\mathbf{irowh}}\left(l-1\right)$ and $j={\mathbf{icolh}}\left(\mathit{l}-1\right)$, for $\mathit{l}=1,2,\dots ,{\mathbf{nnzh}}$. No particular order is expected, but elements should not repeat.
Constraint: $1\le {\mathbf{irowh}}\left(\mathit{l}-1\right)\le {\mathbf{icolh}}\left(\mathit{l}-1\right)\le n$, for $\mathit{l}=1,2,\dots ,{\mathbf{nnzh}}$.
5: $\mathbf{opt}$OptionalE04RL Input/Output
Optional parameter container, derived from Optional.

1: $\mathbf{nnzh}$
The number of nonzero elements in the upper triangle of the matrix $H$

## 6Exceptions and Warnings

Errors or warnings detected by the function:
All errors and warnings have an associated numeric error code field, errorid, stored either as a member of the thrown exception object (see errorid), or as a member of opt.ifail, depending on how errors and warnings are being handled (see Error Handling for more details).
Raises: ErrorException
$\mathbf{errorid}=1$
comm::handle has not been initialized.
$\mathbf{errorid}=1$
comm::handle does not belong to the NAG optimization modelling suite,
has not been initialized properly or is corrupted.
$\mathbf{errorid}=1$
comm::handle has not been initialized properly or is corrupted.
$\mathbf{errorid}=2$
The problem cannot be modified in this phase any more, the solver
$\mathbf{errorid}=2$
Neither nonlinear objective nor nonlinear constraints are present.
The structure of the Hessian cannot be defined.
$\mathbf{errorid}=2$
No nonlinear objective has been defined, its Hessian cannot be set.
$\mathbf{errorid}=3$
The structure of the Hessian of the Lagrangian has already been defined.
$\mathbf{errorid}=3$
The structure of the individual Hessians has already been defined,
the Hessian of the Lagrangian cannot be defined.
$\mathbf{errorid}=3$
On entry, ${\mathbf{idf}}=⟨\mathit{value}⟩$.
The structure of the Hessian of nonlinear function linked to the given idf
$\mathbf{errorid}=6$
On entry, ${\mathbf{nnzh}}=⟨\mathit{value}⟩$.
Constraint: ${\mathbf{nnzh}}>0$.
$\mathbf{errorid}=7$
On entry, ${\mathbf{idf}}=⟨\mathit{value}⟩$.
Constraint: $⟨\mathit{value}⟩\le {\mathbf{idf}}\le ⟨\mathit{value}⟩$.
$\mathbf{errorid}=8$
On entry, $i=⟨\mathit{value}⟩$, ${\mathbf{irowh}}\left[i-1\right]=⟨\mathit{value}⟩$ and
$n=⟨\mathit{value}⟩$.
Constraint: $1\le {\mathbf{irowh}}\left[i-1\right]\le n$.
$\mathbf{errorid}=8$
On entry, $i=⟨\mathit{value}⟩$, ${\mathbf{icolh}}\left[i-1\right]=⟨\mathit{value}⟩$ and
$n=⟨\mathit{value}⟩$.
Constraint: $1\le {\mathbf{icolh}}\left[i-1\right]\le n$.
$\mathbf{errorid}=8$
On entry, $i=⟨\mathit{value}⟩$, ${\mathbf{irowh}}\left[i-1\right]=⟨\mathit{value}⟩$ and
${\mathbf{icolh}}\left[i-1\right]=⟨\mathit{value}⟩$.
Constraint: ${\mathbf{irowh}}\left[i-1\right]\le {\mathbf{icolh}}\left[i-1\right]$ (elements within the upper triangle).
$\mathbf{errorid}=8$
On entry, more than one element of structural matrix $H$ has row index
$⟨\mathit{\text{value}}⟩$ and column index $⟨\mathit{\text{value}}⟩$.
Constraint: each element of structural matrix $H$ must have a unique row
and column index.
$\mathbf{errorid}=10601$
On entry, argument $⟨\mathit{\text{value}}⟩$ must be a vector of size $⟨\mathit{\text{value}}⟩$ array.
Supplied argument has $⟨\mathit{\text{value}}⟩$ dimensions.
$\mathbf{errorid}=10601$
On entry, argument $⟨\mathit{\text{value}}⟩$ must be a vector of size $⟨\mathit{\text{value}}⟩$ array.
Supplied argument was a vector of size $⟨\mathit{\text{value}}⟩$.
$\mathbf{errorid}=10601$
On entry, argument $⟨\mathit{\text{value}}⟩$ must be a vector of size $⟨\mathit{\text{value}}⟩$ array.
The size for the supplied array could not be ascertained.
$\mathbf{errorid}=10602$
On entry, the raw data component of $⟨\mathit{\text{value}}⟩$ is null.
$\mathbf{errorid}=10603$
On entry, unable to ascertain a value for $⟨\mathit{\text{value}}⟩$.
$\mathbf{errorid}=10605$
On entry, the communication class $⟨\mathit{\text{value}}⟩$ has not been initialized correctly.
$\mathbf{errorid}=-99$
An unexpected error has been triggered by this routine.
$\mathbf{errorid}=-399$
Your licence key may have expired or may not have been installed correctly.
$\mathbf{errorid}=-999$
Dynamic memory allocation failed.

Not applicable.

## 8Parallelism and Performance

Please see the description for the underlying computational routine in this section of the FL Interface documentation.