# NAG C Library Function Document

## 1Purpose

nag_opt_handle_set_quadobj (e04rfc) is a part of the NAG optimization modelling suite and defines the linear or the quadratic objective function of the problem.

## 2Specification

 #include #include
 void nag_opt_handle_set_quadobj (void *handle, Integer nnzc, const Integer idxc[], const double c[], Integer nnzh, const Integer irowh[], const Integer icolh[], const double h[], NagError *fail)

## 3Description

After the initialization function nag_opt_handle_init (e04rac) has been called, nag_opt_handle_set_quadobj (e04rfc) may be used to define the objective function of the problem as a quadratic function ${c}^{\mathrm{T}}x+\frac{1}{2}{x}^{\mathrm{T}}Hx$ or a sparse linear function ${c}^{\mathrm{T}}x$ unless the objective function has already been defined by another function in the suite. This objective function will typically be used for linear programming (LP)
 $minimize x∈ℝn cTx (a) subject to lB≤Bx≤uB (b) lx≤x≤ux , (c)$ (1)
 $minimize x∈ℝn 12 xTHx + cTx (a) subject to lB≤Bx≤uB (b) lx≤x≤ux (c)$ (2)
or for semidefinite programming problems with bilinear matrix inequalities (BMI-SDP)
 $minimize x∈ℝn 12 xTHx + cTx (a) subject to ∑ i,j=1 n xi xj Qijk + ∑ i=1 n xi Aik - A0k ⪰ 0 , k=1,…,mA (b) lB≤Bx≤uB (c) lx≤x≤ux (d)$ (3)
The matrix $H$ is a sparse symmetric $n$ by $n$ matrix. It does not need to be positive definite. See nag_opt_handle_init (e04rac) for more details.
None.

## 5Arguments

1:    $\mathbf{handle}$void *Input
On entry: the handle to the problem. It needs to be initialized by nag_opt_handle_init (e04rac) and must not be changed.
2:    $\mathbf{nnzc}$IntegerInput
On entry: the number of nonzero elements in the sparse vector $c$.
If ${\mathbf{nnzc}}=0$, $c$ is considered to be zero and the arrays idxc and c will not be referenced and may be NULL.
Constraint: ${\mathbf{nnzc}}\ge 0$.
3:    $\mathbf{idxc}\left[{\mathbf{nnzc}}\right]$const IntegerInput
4:    $\mathbf{c}\left[{\mathbf{nnzc}}\right]$const doubleInput
On entry: the nonzero elements of the sparse vector $c$. ${\mathbf{idxc}}\left[i-1\right]$ must contain the index of ${\mathbf{c}}\left[\mathit{i}-1\right]$ in the vector, for $\mathit{i}=1,2,\dots ,{\mathbf{nnzc}}$. The elements are stored in ascending order. Note that $n$, the number of variables in the problem, was set in nvar during the initialization of the handle by nag_opt_handle_init (e04rac).
Constraints:
• $1\le {\mathbf{idxc}}\left[\mathit{i}-1\right]\le n$, for $\mathit{i}=1,2,\dots ,{\mathbf{nnzc}}$;
• ${\mathbf{idxc}}\left[\mathit{i}-1\right]<{\mathbf{idxc}}\left[\mathit{i}\right]$, for $\mathit{i}=1,2,\dots ,{\mathbf{nnzc}}-1$.
5:    $\mathbf{nnzh}$IntegerInput
On entry: the number of nonzero elements in the upper triangle of the matrix $H$.
If ${\mathbf{nnzh}}=0$, the matrix $H$ is considered to be zero, the objective function is linear and irowh, icolh and h will not be referenced and may be NULL.
Constraint: ${\mathbf{nnzh}}\ge 0$.
6:    $\mathbf{irowh}\left[{\mathbf{nnzh}}\right]$const IntegerInput
7:    $\mathbf{icolh}\left[{\mathbf{nnzh}}\right]$const IntegerInput
8:    $\mathbf{h}\left[{\mathbf{nnzh}}\right]$const doubleInput
On entry: arrays irowh, icolh and h 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 h specifies the values of the nonzero elements in such a way that ${h}_{ij}={\mathbf{h}}\left[l-1\right]$ where $i={\mathbf{irowh}}\left[l-1\right]$, $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}}$.
9:    $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
The objective function has already been defined.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_HANDLE
The supplied handle does not define a valid handle to the data structure for the NAG optimization modelling suite. It has not been initialized by nag_opt_handle_init (e04rac) or it has been corrupted.
NE_INT
On entry, ${\mathbf{nnzc}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nnzc}}\ge 0$.
On entry, ${\mathbf{nnzh}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nnzh}}\ge 0$.
NE_INTARR
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{idxc}}\left[i-1\right]=〈\mathit{\text{value}}〉$ and $n=〈\mathit{\text{value}}〉$.
Constraint: $1\le {\mathbf{idxc}}\left[i-1\right]\le n$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
NE_INVALID_CS
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{icolh}}\left[\mathit{i}-1\right]=〈\mathit{\text{value}}〉$ and $n=〈\mathit{\text{value}}〉$.
Constraint: $1\le {\mathbf{icolh}}\left[\mathit{i}-1\right]\le n$.
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{irowh}}\left[\mathit{i}-1\right]=〈\mathit{\text{value}}〉$ and ${\mathbf{icolh}}\left[\mathit{i}-1\right]=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{irowh}}\left[\mathit{i}-1\right]\le {\mathbf{icolh}}\left[\mathit{i}-1\right]$ (elements within the upper triangle).
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{irowh}}\left[\mathit{i}-1\right]=〈\mathit{\text{value}}〉$ and $n=〈\mathit{\text{value}}〉$.
Constraint: $1\le {\mathbf{irowh}}\left[\mathit{i}-1\right]\le n$.
On entry, more than one element of h has row index $〈\mathit{\text{value}}〉$ and column index $〈\mathit{\text{value}}〉$.
Constraint: each element of h must have a unique row and column index.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
NE_NOT_INCREASING
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{idxc}}\left[i-1\right]=〈\mathit{\text{value}}〉$ and ${\mathbf{idxc}}\left[i\right]=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{idxc}}\left[\mathit{i}-1\right]<{\mathbf{idxc}}\left[i\right]$ (ascending order).
NE_PHASE
The problem cannot be modified in this phase any more, the solver has already been called.

Not applicable.

None.

## 10Example

This example demonstrates how to use nonlinear semidefinite programming to find a nearest correlation matrix satisfying additional requirements. This is a viable alternative to functions nag_nearest_correlation (g02aac), nag_nearest_correlation_bounded (g02abc), nag_nearest_correlation_h_weight (g02ajc) or nag_nearest_correlation_shrinking (g02anc) as it easily allows you to add further constraints on the correlation matrix. In this case a problem with a linear matrix inequality and a quadratic objective function is formulated to find the nearest correlation matrix in the Frobenius norm preserving the nonzero pattern of the original input matrix. However, additional box bounds (nag_opt_handle_set_simplebounds (e04rhc)) or linear constraints (nag_opt_handle_set_linconstr (e04rjc)) can be readily added to further bind individual elements of the new correlation matrix or new matrix inequalities (nag_opt_handle_set_linmatineq (e04rnc)) to restrict its eigenvalues.
The problem is as follows (to simplify the notation only the upper triangular parts are shown). To a given $m$ by $m$ symmetric input matrix $G$
 $G = g11 ⋯ g1m ⋱ ⋮ gmm$
find correction terms ${x}_{1},\dots ,{x}_{n}$ which form symmetric matrix $\stackrel{-}{G}$
 $G- = g-11 g-12 ⋯ g-1m g-22 ⋯ g-2m ⋱ ⋮ g-mm = 1 g12+x1 g13+x2 ⋯ g1m+xi 1 g23+x3 1 ⋮ ⋱ 1 gm-1m+xn 1$
so that the following requirements are met:
(a) It is a correlation matrix, i.e., symmetric positive semidefinite matrix with a unit diagonal. This is achieved by the way $\stackrel{-}{G}$ is assembled and by a linear matrix inequality
 $G- = x1 0 1 0 ⋯ 0 0 0 ⋯ 0 0 ⋯ 0 ⋱ ⋮ 0 + x2 0 0 1 ⋯ 0 0 0 ⋯ 0 0 ⋯ 0 ⋱ ⋮ 0 + x3 0 0 0 ⋯ 0 0 1 ⋯ 0 0 ⋯ 0 ⋱ ⋮ 0 + ⋯ + xn 0 ⋯ 0 0 0 ⋱ ⋮ ⋮ ⋮ 0 0 0 0 1 0 - -1 -g12 -g13 ⋯ -g1m -1 -g23 ⋯ -g2m -1 ⋯ -g3m ⋱ ⋮ -1 ⪰ 0 .$
(b) $\stackrel{-}{G}$ is nearest to $G$ in the Frobenius norm, i.e., it minimizes the Frobenius norm of the difference which is equivalent to:
 $minimize ​12 ∑i≠j g- ij - gij 2 = ∑ i=1 n xi2 .$
(c) $\stackrel{-}{G}$ preserves the nonzero structure of $G$. This is met by defining ${x}_{i}$ only for nonzero elements ${g}_{ij}$.
For the input matrix
 $G = 2 -1 0 0 -1 2 -1 0 0 -1 2 -1 0 0 -1 2$
the result is
 $G- = 1.0000 -0.6823 0.0000 0.0000 -0.6823 1.0000 -0.5344 0.0000 0.0000 -0.5344 1.0000 -0.6823 0.0000 0.0000 -0.6823 1.0000 .$