# NAG C Library Function Document

## 1Purpose

nag_opt_sdp_read_sdpa (e04rdc) reads in a linear semidefinite programming problem (SDP) from a file in sparse SDPA format and returns it in the form which is usable by functions nag_opt_handle_init (e04rac) (initialization), nag_opt_handle_set_linobj (e04rec) (linear objective function), nag_opt_handle_set_linmatineq (e04rnc) (linear matrix constraints), nag_opt_handle_solve_pennon (e04svc) (solver) and nag_opt_handle_free (e04rzc) (deallocation) from the NAG optimization modelling suite.

## 2Specification

 #include #include
 void nag_opt_sdp_read_sdpa (Nag_FileID infile, Integer maxnvar, Integer maxnblk, Integer maxnnz, Integer filelst, Integer *nvar, Integer *nblk, Integer *nnz, double cvec[], Integer nnza[], Integer irowa[], Integer icola[], double a[], Integer blksizea[], NagError *fail)

## 3Description

nag_opt_sdp_read_sdpa (e04rdc) is capable of reading linear semidefinite programming problems (SDP) from a text file in sparse SDPA format. The problem is captured and returned in the following form:
 $minimize x∈ℝn cTx (a) subject to ∑ i=1 n xi Ai - A0 ⪰ 0 , (b)$ (1)
where ${A}_{i}$ denotes symmetric matrices and $c$ is a vector. The expression $S⪰0$ stands for a constraint on the eigenvalues of a symmetric matrix $S$, namely, all the eigenvalues should be non-negative, i.e., the matrix $S$ should be positive semidefinite.
Please note that this form covers even general linear SDP formulations with multiple linear matrix inequalities and a set of standard linear constraints. A set of ${m}_{A}$ linear matrix inequalities
 $∑ i=1 n xi Aik - A0k ⪰ 0 , k=1,…,mA$ (2)
can be equivalently expressed as one matrix inequality (1)(b) in the following block diagonal form where the matrices ${A}_{i}^{1},{A}_{i}^{2},\dots ,{A}_{i}^{{m}_{A}}$ create the diagonal blocks of ${A}_{i}$:
 $∑ i=1 n xi Ai1 Ai2 ⋱ AimA - A01 A02 ⋱ A0mA ⪰ 0 .$
In addition, notice that if all matrices ${A}_{i}^{k}$ belonging to the same block, say block $k$, are themselves diagonal matrices (or have dimension $1×1$), the associated matrix inequality
 $∑ i=1 n xi Aik - A0k ⪰ 0$ (3)
defines actually a standard linear constraint
 $Bx≥l$
where $l$ and columns of the matrix $B$ are formed by the diagonals of matrices ${A}_{0}^{k}$ and ${A}_{1}^{k},\dots ,{A}_{n}^{k}$, respectively. Precisely, ${l}_{i}={\left({A}_{0}^{k}\right)}_{ii}$ and ${b}_{ij}={\left({A}_{j}^{k}\right)}_{ii}$. See Section 10.

### 3.1Sparse SDPA file format

The problem data is written in an ASCII input file in a SDPA sparse format which was first introduced in Fujisawa et al. (1998). In the description below we follow closely the specification from Borchers (1999).
The format is line oriented. If more elements are required on the line they need to be separated by a space, a tab or any of the special characters ‘,’, ‘(’, ‘)’, ‘{’ or ‘}’. The file consists of six sections:
1. Comments. The file can begin with arbitrarily many lines of comments. Each line of comments must begin with ‘"’ or ‘*’.
2. The first line after the comments contains integer $n$, the number of variables. The rest of this line is ignored.
3. The second line after the comments contains integer ${m}_{A}$, the number of blocks in the block diagonal structure of the matrices. Additional text on this line after ${m}_{A}$ is ignored.
4. The third line after the comments contains a vector of ${m}_{A}$ integers that give the sizes of the individual blocks. Negative numbers may be used to indicate that a block is actually a diagonal submatrix. Thus a block size of ‘$-5$’ indicates a $5$ by $5$ block in which only the diagonal elements are nonzero.
5. The fourth line after the comments contains an $n$-dimensional real vector defining the objective function vector $c$.
6. The remaining lines of the file contain nonzero entries in the constraint matrices, with one entry per line. The format for each line is
 $matno blkno i j entry$
where $\mathbit{matno}$ is the number $\left(0,\dots ,n\right)$ of the matrix to which this entry belongs and $\mathbit{blkno}$ specifies the block number $k=1,2,\dots ,{m}_{A}$ within this matrix. Together, they uniquely identify the block ${A}_{\mathbit{matno}}^{\mathbit{blkno}}$. Integers $\mathbit{i}$ and $\mathbit{j}$ are one-based indices which specify a location of the entry within the block. Note that since all matrices are assumed to be symmetric, only entries in the upper triangle of a matrix should be supplied. Finally, $\mathbit{entry}$ should give the real value of the entry in the matrix. Precisely, ${\left({A}_{\mathbit{matno}}^{\mathbit{blkno}}\right)}_{\mathbit{i}\mathbit{j}}={\left({A}_{\mathbit{matno}}^{\mathbit{blkno}}\right)}_{\mathbit{j}\mathbit{i}}=\mathbit{entry}$.
In the text below and in the file listing (filelst) we use the word ‘token’ as a reference to a group of contiguous characters without a space or any other delimeters.

### 3.2Recommendation on how best to use nag_opt_sdp_read_sdpa (e04rdc)

 (a) The input file with the problem needs to be opened for reading by nag_open_file (x04acc) (${\mathbf{mode}}=0$). Setting ${\mathbf{filelst}}=1$ might help with possible file formatting errors. (b) Unless the dimension of the problem (or its overestimate) is known in advance, call nag_opt_sdp_read_sdpa (e04rdc) initially with ${\mathbf{maxnvar}}=0$, ${\mathbf{maxnblk}}=0$ and ${\mathbf{maxnnz}}=0$. In this case the exact size of the problem is computed and returned in nvar, nblk and nnz. No other data will be stored and none of the arrays cvec, nnza, irowa, icola, a, blksizea will be referenced and may be NULL. Then the exact storage can be allocated and the file reopened. When nag_opt_sdp_read_sdpa (e04rdc) is called for the second time, the problem is read in and stored in appropriate arrays. (c) The example in Section 10 shows a typical sequence of calls to solve the problem read in by nag_opt_sdp_read_sdpa (e04rdc). First an empty handle needs to be initialized by nag_opt_handle_init (e04rac) with nvar variables. This should be followed by calls to nag_opt_handle_set_linobj (e04rec) and nag_opt_handle_set_linmatineq (e04rnc) to formulate the objective function and the constraints, respectively. The arguments of both functions use the same naming and storage as in nag_opt_sdp_read_sdpa (e04rdc) so the variables can be passed unchanged; only dima in nag_opt_handle_set_linmatineq (e04rnc) is new and should equal to sum ${\mathbf{blksizea}}\left[0\right]+\cdots +{\mathbf{blksizea}}\left[{\mathbf{nblk}}-1\right]$ and nnzasum in nag_opt_handle_set_linmatineq (e04rnc) is the same as nnz in nag_opt_sdp_read_sdpa (e04rdc). You may at this point want to modify option settings using nag_opt_handle_opt_set (e04zmc). If dual variables (Lagrangian multipliers) are required from the solver, sufficient space needs to be allocated. The size is equal to the sum of the number of elements of dense triangular matrices for each block. For further details, see the argument ua of the solver nag_opt_handle_solve_pennon (e04svc). The solver should be called and then followed, finally, by a call to nag_opt_handle_free (e04rzc) to deallocate memory associated with the problem.

## 4References

Borchers B (1999) SDPLIB 1.2, A Library of semidefinite programming test problems. Optimization Methods and Software 11(1) 683–690 http://euler.nmt.edu/~brian/sdplib/
Fujisawa K, Kojima M and Nakata K (1998) SDPA (Semidefinite Programming Algorithm) User's Manual Technical Report B-308 Department of Mathematical and Computing Sciences, Tokyo Institute of Technology.

## 5Arguments

1:    $\mathbf{infile}$Nag_FileIDInput
On entry: the file identifier associated with the sparse SDPA data file. Note:  that the file needs to be opened in read mode by nag_open_file (x04acc) with ${\mathbf{mode}}=0$.
2:    $\mathbf{maxnvar}$IntegerInput
On entry: the upper limit for the number of variables in the problem. If it is set to zero, cvec and nnza will not be referenced and may be NULL.
Constraint: ${\mathbf{maxnvar}}\ge 0$.
3:    $\mathbf{maxnblk}$IntegerInput
On entry: the upper limit for the number of matrix constraints (i.e., the number of diagonal blocks within the matrix). If it is set to zero, blksizea will not be referenced and may be NULL.
Constraint: ${\mathbf{maxnblk}}\ge 0$.
4:    $\mathbf{maxnnz}$IntegerInput
On entry: the upper limit on the sum of nonzeros in all matrices ${A}_{\mathit{i}}^{\mathit{k}}$, for $\mathit{i}=0,1,\dots ,{\mathbf{nvar}}$ and $\mathit{k}=1,2,\dots ,{\mathbf{nblk}}$. If it is set to zero, irowa, icola and a will not be referenced and may be NULL.
Constraint: ${\mathbf{maxnnz}}\ge 0$.
5:    $\mathbf{filelst}$IntegerInput
On entry: if ${\mathbf{filelst}}\ne 0$, a listing of the input data is sent to stdout. This can be useful for debugging the data file.
If ${\mathbf{filelst}}=0$, no listing is produced.
6:    $\mathbf{nvar}$Integer *Output
7:    $\mathbf{nblk}$Integer *Output
8:    $\mathbf{nnz}$Integer *Output
On exit: the actual number of the variables $n$, matrix constraints ${m}_{A}$ and number of nonzeros of the problem in the file. This also indicates the exact memory needed in cvec, nnza, irowa, icola, a and blksizea.
9:    $\mathbf{cvec}\left[{\mathbf{maxnvar}}\right]$doubleOutput
On exit: ${\mathbf{cvec}}\left[\mathit{i}-1\right]$, for $\mathit{i}=1,2,\dots ,{\mathbf{nvar}}$, stores the dense vector $c$ of the linear objective function. If ${\mathbf{maxnvar}}=0$, cvec is not referenced and may be NULL.
10:  $\mathbf{nnza}\left[{\mathbf{maxnvar}}+1\right]$IntegerOutput
On exit: ${\mathbf{nnza}}\left[\mathit{i}\right]$, for $\mathit{i}=0,1,\dots ,{\mathbf{nvar}}$, stores the number of nonzero elements in matrices ${A}_{i}$. If ${\mathbf{maxnvar}}=0$, nnza is not referenced and may be NULL.
11:  $\mathbf{irowa}\left[{\mathbf{maxnnz}}\right]$IntegerOutput
12:  $\mathbf{icola}\left[{\mathbf{maxnnz}}\right]$IntegerOutput
13:  $\mathbf{a}\left[{\mathbf{maxnnz}}\right]$doubleOutput
On exit: irowa, icola and a store the nonzeros in the upper triangle of matrices ${A}_{\mathit{i}}$, for $\mathit{i}=0,1,\dots ,{\mathbf{nvar}}$, in the coordinate storage, i.e., ${\mathbf{irowa}}\left[\mathit{j}-1\right]$ are one-based row indices, ${\mathbf{icola}}\left[\mathit{j}-1\right]$ are one-based column indices and ${\mathbf{a}}\left[\mathit{j}-1\right]$ are the values of the nonzero elements, for $\mathit{j}=1,2,\dots ,{\mathbf{nnz}}$. See Section 9. If ${\mathbf{maxnnz}}=0$, the arrays are not referenced and may be NULL.
14:  $\mathbf{blksizea}\left[{\mathbf{maxnblk}}\right]$IntegerOutput
On exit: ${\mathbf{blksizea}}\left[\mathit{k}-1\right]$, for $\mathit{k}=1,2,\dots ,{\mathbf{nblk}}$, stores the sizes of the diagonal blocks in matrices ${A}_{i}$ from the top to the bottom. If ${\mathbf{maxnblk}}=0$, blksizea is not referenced and may be NULL.
15:  $\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.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_DIAG_ELEMENTS
Invalid structural data found on line $〈\mathit{\text{value}}〉$.
The specified element belongs to a diagonal block but is not diagonal.
The row index is $〈\mathit{\text{value}}〉$ and column index is $〈\mathit{\text{value}}〉$.
NE_DUPLICATE_ELEMENT
An entry in the constraints with $\mathbit{matno}=〈\mathit{\text{value}}〉$, $\mathbit{blkno}=〈\mathit{\text{value}}〉$, row index $〈\mathit{\text{value}}〉$ and column index $〈\mathit{\text{value}}〉$ was defined more than once. All entries need to be unique.
NE_FILE_INCOMPLETE
A premature end of the input stream. The part defining the dimensions of the blocks was not found.
A premature end of the input stream. The part defining the nonzero entries was not found.
A premature end of the input stream. The part defining the number of blocks was not found.
A premature end of the input stream. The part defining the number of variables was not found.
A premature end of the input stream. The part defining the objective function was not found.
NE_FILEID
On entry, ${\mathbf{infile}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{infile}}\ge 0$.
NE_INT
An invalid number of blocks was given on line $〈\mathit{\text{value}}〉$.
The number stated there is $〈\mathit{\text{value}}〉$ and needs to be at least $1$.
An invalid number of variables was given on line $〈\mathit{\text{value}}〉$.
The number stated there is $〈\mathit{\text{value}}〉$ and needs to be at least $1$.
NE_INT_ARG
On entry, ${\mathbf{maxnblk}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{maxnblk}}\ge 0$.
On entry, ${\mathbf{maxnnz}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{maxnnz}}\ge 0$.
On entry, ${\mathbf{maxnvar}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{maxnvar}}\ge 0$.
NE_INT_ARRAY
An invalid size of the block number $〈\mathit{\text{value}}〉$ was given on line $〈\mathit{\text{value}}〉$.
The number stated there is $〈\mathit{\text{value}}〉$ and needs to be nonzero.
NE_INT_MAX
At least one of maxnvar, maxnblk or maxnnz is too small. Suggested values are returned in nvar, nblk and nnz, respectively.
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
Invalid structural data found on line $〈\mathit{\text{value}}〉$.
The given column index is out of bounds, it must respect the size of the block. Its value $〈\mathit{\text{value}}〉$ must be between $〈\mathit{\text{value}}〉$ and $〈\mathit{\text{value}}〉$ (inclusive).
Invalid structural data found on line $〈\mathit{\text{value}}〉$.
The given row index is out of bounds, it must respect the size of the block. Its value $〈\mathit{\text{value}}〉$ must be between $〈\mathit{\text{value}}〉$ and $〈\mathit{\text{value}}〉$ (inclusive).
Invalid structural data found on line $〈\mathit{\text{value}}〉$.
The specified nonzero element is not in the upper triangle.
The row index is $〈\mathit{\text{value}}〉$ and column index is $〈\mathit{\text{value}}〉$.
NE_INVALID_FORMAT
The token on line $〈\mathit{\text{value}}〉$ at position $〈\mathit{\text{value}}〉$ to $〈\mathit{\text{value}}〉$ was not recognized as a valid integer.
The token on line $〈\mathit{\text{value}}〉$ at position $〈\mathit{\text{value}}〉$ to $〈\mathit{\text{value}}〉$ was not recognized as a valid real number.
The token on line $〈\mathit{\text{value}}〉$ starting at position $〈\mathit{\text{value}}〉$ was too long and was not recognized.
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.
The input stream seems to be empty. No data was read. This might indicate a problem with opening the file, check that nag_open_file (x04acc) was used correctly.
NE_OUT_OF_RANGE
Invalid structural data found on line $〈\mathit{\text{value}}〉$.
The given block number is out of bounds. Its value $〈\mathit{\text{value}}〉$ must be between $1$ and ${m}_{A}$ (inclusive).
Invalid structural data found on line $〈\mathit{\text{value}}〉$.
The given matrix number is out of bounds. Its value $〈\mathit{\text{value}}〉$ must be between $0$ and $n$ (inclusive).
Reading from the stream caused an unknown error on line $〈\mathit{\text{value}}〉$.
NE_WRONG_NUM_TOKENS
Not enough data was given on line $〈\mathit{\text{value}}〉$ specifying block sizes.
Expected ${m}_{A}$ tokens but found only $〈\mathit{\text{value}}〉$.
Not enough data was given on line $〈\mathit{\text{value}}〉$ specifying nonzero matrix elements.
Expected $〈\mathit{\text{value}}〉$ tokens but found only $〈\mathit{\text{value}}〉$.
Not enough data was given on line $〈\mathit{\text{value}}〉$ specifying the objective function.
Expected $n$ tokens but found only $〈\mathit{\text{value}}〉$.

Not applicable.

## 8Parallelism and Performance

The following artificial example demonstrates how the elements of ${A}_{i}$ matrices are organized within arrays nnza, irowa, icola and a. For simplicity let us assume that ${\mathbf{nblk}}=1$, ${\mathbf{blksizea}}\left[0\right]=3$ and ${\mathbf{nvar}}=4$. Please note that the values of the elements were chosen to ease readability rather than to define a valid problem.
Let the matrix constraint (1)(b) be defined by
 $A0 = 0 0.1 0 0.1 0 0.2 0 0.2 0.3 ,$
 $A1 = 1.1 0 0 0 1.2 1.3 0 1.3 1.4 ,$
 $A2 ​ empty,$
 $A3 = 0 0 0 0 3.1 0 0 0 3.2 ,$
 $A4 = 4.1 4.2 4.3 4.2 0 0 4.3 0 0 .$
All matrices ${A}_{i}$ have to be symmetric and therefore only the elements in the upper triangles are stored. The table below shows how the arrays would be populated.
 irowa $\begin{array}{ccc}\phantom{0}1\phantom{.}\hfill & \phantom{0}2\phantom{.}\hfill & \phantom{0}3\phantom{.}\hfill \end{array}$ $\begin{array}{cccc}\phantom{0}1\phantom{.}& \phantom{0}2\phantom{.}& \phantom{0}2\phantom{.}& \phantom{0}3\phantom{.}\end{array}$ $\begin{array}{cc}\phantom{0}2\phantom{.}& \phantom{0}3\phantom{.}\end{array}$ $\begin{array}{ccc}\phantom{0}1\phantom{.}& \phantom{0}2\phantom{.}& \phantom{0}3\phantom{.}\end{array}$ icola $\begin{array}{ccc}\phantom{0}2\phantom{.}& \phantom{0}3\phantom{.}& \phantom{0}3\phantom{.}\end{array}$ $\begin{array}{cccc}\phantom{0}1\phantom{.}& \phantom{0}2\phantom{.}& \phantom{0}3\phantom{.}& \phantom{0}3\phantom{.}\end{array}$ $\begin{array}{cc}\phantom{0}2\phantom{.}& \phantom{0}3\phantom{.}\end{array}$ $\begin{array}{ccc}\phantom{0}1\phantom{.}& \phantom{0}1\phantom{.}& \phantom{0}1\phantom{.}\end{array}$ a $\underbrace{\begin{array}{ccc}0.1& 0.2& 0.3\end{array}}$ $\underbrace{\begin{array}{cccc}1.1& 1.2& 1.3& 1.4\end{array}}$ $\underbrace{\phantom{000.0}}$ $\underbrace{\begin{array}{cc}3.1& 3.2\end{array}}$ $\underbrace{\begin{array}{ccc}4.1& 4.2& 4.3\end{array}}$ ${A}_{0}$ ${A}_{1}$ ${A}_{2}$ ${A}_{3}$ ${A}_{4}$ nnza $3$ $4$ $0$ $2$ $3$
See also Section 3 in nag_opt_handle_set_linmatineq (e04rnc) which accepts the same format.

## 10Example

The following example comes from Fujisawa et al. (1998).
Imagine that we want to store the following problem in a file in the SDPA format.
 $minimize x∈ℝ2 10x1 + 20x2 subject to 1 0 1 1 ⁢ x1 x2 ≥ 1 1.5 5 2 2 6 ⁢ x2 - 3 0 0 4 ⪰ 0 .$
There are two variables ($n=2$) in the problem. One linear matrix constraint and one block of linear constraints can be formed as (1) with two diagonal blocks (${m}_{A}=2$). Both blocks have dimension $2$ but the first one (defining linear constraints) is only diagonal, thus the sizes will be stated as $\begin{array}{cc}-2& 2\end{array}$.
The problem can be rewritten as
 $minimize x∈ℝ2 cTx subject to A1⁢x1 + A2⁢x2 - A0 ⪰ 0$
where
• $c={\left(\begin{array}{cc}10& 20\end{array}\right)}^{\mathrm{T}}$,
• ${A}_{0}=\left(\begin{array}{cccc}1& 0& 0& 0\\ 0& 1.5& 0& 0\\ 0& 0& 3& 0\\ 0& 0& 0& 4\end{array}\right)$,
• ${A}_{1}=\left(\begin{array}{cccc}1& 0& 0& 0\\ 0& 1& 0& 0\\ 0& 0& 0& 0\\ 0& 0& 0& 0\end{array}\right)$,
• ${A}_{2}=\left(\begin{array}{cccc}0& 0& 0& 0\\ 0& 1& 0& 0\\ 0& 0& 5& 2\\ 0& 0& 2& 6\end{array}\right)$.
The optimal solution is ${x}^{*}={\left(\begin{array}{cc}1.0& 1.0\end{array}\right)}^{\mathrm{T}}$ with the objective function value $30.0$. The optimal Lagrangian multipliers (dual variables) are $10.0$, $0.0$ and $\left(\begin{array}{cc}20/7\text{,}& -20/7\\ -20/7\text{,}& 20/7\end{array}\right)$.