# NAG FL Interfacee04rsf (handle_​set_​qconstr)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

e04rsf is a part of the NAG optimization modelling suite and defines a new, or edits an existing, quadratic objective function or constraint of the problem.

## 2Specification

Fortran Interface
 Subroutine e04rsf ( s, nnzr, idxr, r, nnzq, q, idqc,
 Integer, Intent (In) :: nnzr, idxr(nnzr), nnzq, irowq(nnzq), icolq(nnzq) Integer, Intent (Inout) :: idqc, ifail Real (Kind=nag_wp), Intent (In) :: s, r(nnzr), q(nnzq) Type (c_ptr), Intent (In) :: handle
#include <nag.h>
 void e04rsf_ (void **handle, const double *s, const Integer *nnzr, const Integer idxr[], const double r[], const Integer *nnzq, const Integer irowq[], const Integer icolq[], const double q[], Integer *idqc, Integer *ifail)
The routine may be called by the names e04rsf or nagf_opt_handle_set_qconstr.

## 3Description

After the handle has been initialized (e.g., e04raf has been called), e04rsf may be used to edit a model by adding or replacing a quadratic objective function or constraint of the form
 $12 xTQx + rTx$ (1)
and
 $12 xTQx + rTx + s≤0 ,$ (2)
respectively. If a factor $F$ of $Q$ such that $Q={F}^{\mathrm{T}}F$ is available, e04rtf should be called instead.
The matrix $Q$ is a sparse symmetric $n×n$ matrix. Typically e04rsf would be used together with Second-order Cone Programming (SOCP) solver e04ptf if $Q$ is positive semidefinite which implies the problem is convex. Otherwise, if the problem is nonconvex, a general nonlinear optimization solver (such as e04stf) may be used. It is also acceptable if $Q$ is a zero matrix, in which case the corresponding objective function or constraint becomes linear. Note that it is possible to temporarily disable and enable individual constraints in the model by e04tcf and e04tbf, respectively. See Section 3.1 in the E04 Chapter Introduction for more details about the NAG optimization modelling suite.

None.

## 5Arguments

1: $\mathbf{handle}$Type (c_ptr) Input
On entry: the handle to the problem. It needs to be initialized (e.g., by e04raf) and must not be changed between calls to the NAG optimization modelling suite.
2: $\mathbf{s}$Real (Kind=nag_wp) Input
On entry: the constant term in quadratic constraint.
If ${\mathbf{idqc}}=-1$, s will not be referenced.
3: $\mathbf{nnzr}$Integer Input
On entry: the number of nonzero elements in the sparse vector $r$.
If ${\mathbf{nnzr}}=0$, $r$ is considered to be zero and the arrays idxr and r will not be referenced.
Constraint: ${\mathbf{nnzr}}\ge 0$.
4: $\mathbf{idxr}\left({\mathbf{nnzr}}\right)$Integer array Input
5: $\mathbf{r}\left({\mathbf{nnzr}}\right)$Real (Kind=nag_wp) array Input
On entry: the nonzero elements of the sparse vector $r$. ${\mathbf{idxr}}\left(i\right)$ must contain the index of ${\mathbf{r}}\left(\mathit{i}\right)$ in the vector, for $\mathit{i}=1,2,\dots ,{\mathbf{nnzr}}$. No particular order is expected, but elements should not repeat. Note that $n$ is the current number of variables in the problem.
Constraint: $1\le {\mathbf{idxr}}\left(\mathit{i}\right)\le n$, for $\mathit{i}=1,2,\dots ,{\mathbf{nnzr}}$.
6: $\mathbf{nnzq}$Integer Input
On entry: the number of nonzero elements in the upper triangle of the matrix $Q$.
If ${\mathbf{nnzq}}=0$, the matrix $Q$ is considered to be zero, the objective function or constraint is linear and irowq, icolq and q will not be referenced.
Constraint: ${\mathbf{nnzq}}\ge 0$.
7: $\mathbf{irowq}\left({\mathbf{nnzq}}\right)$Integer array Input
8: $\mathbf{icolq}\left({\mathbf{nnzq}}\right)$Integer array Input
9: $\mathbf{q}\left({\mathbf{nnzq}}\right)$Real (Kind=nag_wp) array Input
On entry: arrays irowq, icolq and q store the nonzeros of the upper triangle of the matrix $Q$ in coordinate storage (CS) format (see Section 2.1.1 in the F11 Chapter Introduction). irowq specifies one-based row indices, icolq specifies one-based column indices and q specifies the values of the nonzero elements in such a way that ${Q}_{ij}={\mathbf{q}}\left(l\right)$ where $i={\mathbf{irowq}}\left(l\right)$, $j={\mathbf{icolq}}\left(\mathit{l}\right)$, for $\mathit{l}=1,2,\dots ,{\mathbf{nnzq}}$. No particular order is expected, but elements should not repeat.
Constraint: $1\le {\mathbf{irowq}}\left(\mathit{l}\right)\le {\mathbf{icolq}}\left(\mathit{l}\right)\le n$, for $\mathit{l}=1,2,\dots ,{\mathbf{nnzq}}$.
10: $\mathbf{idqc}$Integer Input/Output
On entry:
${\mathbf{idqc}}=0$
A new quadratic constraint is created.
${\mathbf{idqc}}>0$
Specifies the index of an existing constraint to be replaced. i.e., replaces the idqcth constraint.
${\mathbf{idqc}}=-1$
A new quadratic objective is created and will replace any previously defined objective function.
Constraint: ${\mathbf{idqc}}\ge -1$.
On exit: if ${\mathbf{idqc}}=0$ on entry, then idqc is overwritten with the index of the new quadratic constraint. By definition, this is the number of quadratic constraints already defined plus one. Otherwise, idqc stays unchanged.
11: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $-1$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{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).

## 6Error 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 properly initialized or it has been corrupted.
${\mathbf{ifail}}=2$
The problem cannot be modified right now, the solver is running.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{idqc}}=⟨\mathit{\text{value}}⟩$.
The given idqc does not match with any quadratic constraint already defined.
${\mathbf{ifail}}=5$
On entry, ${\mathbf{idqc}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{idqc}}\ge -1$.
${\mathbf{ifail}}=6$
nnzr and nnzq cannot be zero at the same time.
On entry, ${\mathbf{nnzq}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nnzq}}\ge 0$.
On entry, ${\mathbf{nnzr}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nnzr}}\ge 0$.
${\mathbf{ifail}}=7$
On entry, $i=⟨\mathit{\text{value}}⟩$, ${\mathbf{idxr}}\left(i\right)=⟨\mathit{\text{value}}⟩$ and $n=⟨\mathit{\text{value}}⟩$.
Constraint: $1\le {\mathbf{idxr}}\left(i\right)\le n$.
On entry, more than one element of idxr has index $⟨\mathit{\text{value}}⟩$.
Constraint: each element of idxr must have a unique index.
${\mathbf{ifail}}=8$
On entry, $i=⟨\mathit{\text{value}}⟩$, ${\mathbf{icolq}}\left(\mathit{i}\right)=⟨\mathit{\text{value}}⟩$ and $n=⟨\mathit{\text{value}}⟩$.
Constraint: $1\le {\mathbf{icolq}}\left(\mathit{i}\right)\le n$.
On entry, $i=⟨\mathit{\text{value}}⟩$, ${\mathbf{irowq}}\left(\mathit{i}\right)=⟨\mathit{\text{value}}⟩$ and ${\mathbf{icolq}}\left(\mathit{i}\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{irowq}}\left(\mathit{i}\right)\le {\mathbf{icolq}}\left(\mathit{i}\right)$ (elements within the upper triangle).
On entry, $i=⟨\mathit{\text{value}}⟩$, ${\mathbf{irowq}}\left(\mathit{i}\right)=⟨\mathit{\text{value}}⟩$ and $n=⟨\mathit{\text{value}}⟩$.
Constraint: $1\le {\mathbf{irowq}}\left(\mathit{i}\right)\le n$.
On entry, more than one element of q has row index $⟨\mathit{\text{value}}⟩$ and column index $⟨\mathit{\text{value}}⟩$.
Constraint: each element of q must have a unique row and column index.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

Not applicable.

## 8Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
e04rsf is not threaded in any implementation.

None.

## 10Example

This example demonstrates how to define and solve a convex quadratically constrained quadratic programming problem via SOCP. See also e04rtf where the quadratic term is defined by its factor.
This example solves a quadratically constrained quadratic programming problem
 $minimize x∈ℝ3 12 xT Q0 x + r0T x subject to 12 xT Q1 x + r1T x + s1 ≤ 0 ,$
where
 $Q0= ( 0.4930.3820.270 0.3820.4750.448 0.2700.4480.515 ) Q1= ( 0.7370.4531.002 0.4530.3160.635 1.0020.6351.590 ) , r0= ( 0.847 0.08 0.505 ) r1= ( 0.065 0.428 0.097 )$
and ${s}_{1}=1.276$.
The optimal solution (to five significant figures) is
 $x*=(1.1742,-4.2569,0.98144)T,$
and the objective function value is $2.5714$.

### 10.1Program Text

Program Text (e04rsfe.f90)

### 10.2Program Data

Program Data (e04rsfe.d)

### 10.3Program Results

Program Results (e04rsfe.r)