E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentE04RFF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

E04RFF is a part of the NAG optimization modelling suite and defines the linear or the quadratic objective function of the problem.

## 2  Specification

 SUBROUTINE E04RFF ( HANDLE, NNZC, IDXC, C, NNZH, IROWH, ICOLH, H, IFAIL)
 INTEGER NNZC, IDXC(NNZC), NNZH, IROWH(NNZH), ICOLH(NNZH), IFAIL REAL (KIND=nag_wp) C(NNZC), H(NNZH) TYPE (C_PTR) HANDLE

## 3  Description

After the initialization routine E04RAF has been called, E04RFF 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 been defined previously by E04REF, E04RFF or by E04RGF. This objective function will typically be used for quadratic programming problems (QP)
 $minimize x∈ℝn 12 xTHx + cTx (a) subject to lB≤Bx≤uB (b) lx≤x≤ux (c)$ (1)
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)$ (2)
The matrix $H$ is a sparse symmetric $n$ by $n$ matrix. It does not need to be positive definite. See E04RAF for more details.

None.

## 5  Arguments

1:     $\mathrm{HANDLE}$ – TYPE (C_PTR)Input
On entry: the handle to the problem. It needs to be initialized by E04RAF and must not be changed.
2:     $\mathrm{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.
Constraint: ${\mathbf{NNZC}}\ge 0$.
3:     $\mathrm{IDXC}\left({\mathbf{NNZC}}\right)$ – INTEGER arrayInput
4:     $\mathrm{C}\left({\mathbf{NNZC}}\right)$ – REAL (KIND=nag_wp) arrayInput
On entry: the nonzero elements of the sparse vector $c$. ${\mathbf{IDXC}}\left(i\right)$ must contain the index of ${\mathbf{C}}\left(\mathit{i}\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 E04RAF.
Constraints:
• $1\le {\mathbf{IDXC}}\left(\mathit{i}\right)\le n$, for $\mathit{i}=1,2,\dots ,{\mathbf{NNZC}}$;
• ${\mathbf{IDXC}}\left(\mathit{i}\right)<{\mathbf{IDXC}}\left(\mathit{i}+1\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{NNZC}}-1$.
5:     $\mathrm{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.
Constraint: ${\mathbf{NNZH}}\ge 0$.
6:     $\mathrm{IROWH}\left({\mathbf{NNZH}}\right)$ – INTEGER arrayInput
7:     $\mathrm{ICOLH}\left({\mathbf{NNZH}}\right)$ – INTEGER arrayInput
8:     $\mathrm{H}\left({\mathbf{NNZH}}\right)$ – REAL (KIND=nag_wp) arrayInput
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\right)$ where $i={\mathbf{IROWH}}\left(l\right)$, $j={\mathbf{ICOLH}}\left(\mathit{l}\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}\right)\le {\mathbf{ICOLH}}\left(\mathit{l}\right)\le n$, for $\mathit{l}=1,2,\dots ,{\mathbf{NNZH}}$.
9:     $\mathrm{IFAIL}$ – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}1$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
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 E04RAF or it has been corrupted.
${\mathbf{IFAIL}}=2$
The problem cannot be modified in this phase any more, the solver has already been called.
${\mathbf{IFAIL}}=3$
The objective function has already been defined.
${\mathbf{IFAIL}}=6$
On entry, ${\mathbf{NNZC}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{NNZC}}\ge 0$.
On entry, ${\mathbf{NNZH}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{NNZH}}\ge 0$.
${\mathbf{IFAIL}}=7$
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{IDXC}}\left(i\right)=〈\mathit{\text{value}}〉$ and ${\mathbf{IDXC}}\left(i+1\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{IDXC}}\left(\mathit{i}\right)<{\mathbf{IDXC}}\left(i+1\right)$ (ascending order).
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{IDXC}}\left(i\right)=〈\mathit{\text{value}}〉$ and $n=〈\mathit{\text{value}}〉$.
Constraint: $1\le {\mathbf{IDXC}}\left(i\right)\le n$.
${\mathbf{IFAIL}}=8$
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{ICOLH}}\left(\mathit{i}\right)=〈\mathit{\text{value}}〉$ and $n=〈\mathit{\text{value}}〉$.
Constraint: $1\le {\mathbf{ICOLH}}\left(\mathit{i}\right)\le n$.
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{IROWH}}\left(\mathit{i}\right)=〈\mathit{\text{value}}〉$ and ${\mathbf{ICOLH}}\left(\mathit{i}\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{IROWH}}\left(\mathit{i}\right)\le {\mathbf{ICOLH}}\left(\mathit{i}\right)$ (elements within the upper triangle).
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{IROWH}}\left(\mathit{i}\right)=〈\mathit{\text{value}}〉$ and $n=〈\mathit{\text{value}}〉$.
Constraint: $1\le {\mathbf{IROWH}}\left(\mathit{i}\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.
${\mathbf{IFAIL}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{IFAIL}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{IFAIL}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

Not applicable.

## 8  Parallelism and Performance

E04RFF is not threaded in any implementation.

None.

## 10  Example

This example demonstrates how to use nonlinear semidefinite programming to find a nearest correlation matrix satisfying additional requirements. This is a viable alternative to routines G02AAF, G02ABF, G02AJF or G02ANF 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 (E04RHF) or linear constraints (E04RJF) can be readily added to further bind individual elements of the new correlation matrix or new matrix inequalities (E04RNF) 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 .$

### 10.1  Program Text

Program Text (e04rffe.f90)

### 10.2  Program Data

Program Data (e04rffe.d)

### 10.3  Program Results

Program Results (e04rffe.r)