# NAG CL Interfacef11zac (real_​gen_​sort)

## 1Purpose

f11zac sorts the nonzero elements of a real sparse nonsymmetric matrix, represented in coordinate storage format.

## 2Specification

 #include
 void f11zac (Integer n, Integer *nnz, double a[], Integer irow[], Integer icol[], Nag_SparseNsym_Dups dup, Nag_SparseNsym_Zeros zero, Integer istr[], NagError *fail)
The function may be called by the names: f11zac, nag_sparse_real_gen_sort or nag_sparse_nsym_sort.

## 3Description

f11zac takes a coordinate storage (CS) representation (see Section 2.1.1 in the F11 Chapter Introduction) of a real $n$ by $n$ sparse nonsymmetric matrix $A$, and reorders the nonzero elements by increasing row index and increasing column index within each row. Entries with duplicate row and column indices may be removed. Alternatively, duplicate entries may be summed, which facilitates spare matrix addition (see Section 9). Any entries with zero values may optionally be removed.
f11zac also returns a pointer array istr to the starting address of each row in $A$. This can be used to construct a compressed column storage (CCS) representation of the matrix (see Section 9).

None.

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: $n$, the order of the matrix $A$.
Constraint: ${\mathbf{n}}\ge 1$.
2: $\mathbf{nnz}$Integer * Input/Output
On entry: the number of elements supplied in the array a.
Constraint: ${\mathbf{nnz}}\ge 0$.
On exit: the number of elements with unique row and column indices.
3: $\mathbf{a}\left[\mathit{dim}\right]$double Input/Output
Note: the dimension, dim, of the array a must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nnz}}\right)$.
On entry: the nonzero elements of the matrix $A$. These may be in any order and there may be multiple nonzero elements with the same row and column indices.
On exit: the nonzero elements ordered by increasing row index, and by increasing column index within each row. Each nonzero element has a unique row and column index.
4: $\mathbf{irow}\left[\mathit{dim}\right]$Integer Input/Output
Note: the dimension, dim, of the array irow must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nnz}}\right)$.
On entry: the row indices corresponding to the elements supplied in the array a.
Constraint: $1\le {\mathbf{irow}}\left[\mathit{i}\right]\le {\mathbf{n}}$, for $\mathit{i}=0,1,\dots ,{\mathbf{nnz}}-1$.
On exit: the first nnz elements contain the row indices corresponding to the elements returned in the array a.
5: $\mathbf{icol}\left[\mathit{dim}\right]$Integer Input/Output
Note: the dimension, dim, of the array icol must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nnz}}\right)$.
On entry: the column indices corresponding to the elements supplied in the array a.
Constraint: $1\le {\mathbf{icol}}\left[\mathit{i}\right]\le {\mathbf{n}}$, for $\mathit{i}=0,1,\dots ,{\mathbf{nnz}}-1$.
On exit: the first nnz elements contain the column indices corresponding to the elements returned in the array a.
6: $\mathbf{dup}$Nag_SparseNsym_Dups Input
On entry: indicates how elements in a with duplicate row and column indices are to be treated.
${\mathbf{dup}}=\mathrm{Nag_SparseNsym_RemoveDups}$
Duplicate entries are removed, only the first entry is kept.
${\mathbf{dup}}=\mathrm{Nag_SparseNsym_SumDups}$
The relevant values in a are summed.
${\mathbf{dup}}=\mathrm{Nag_SparseNsym_FailDups}$
The function fails with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_NON_ZERO_DUP on detecting a duplicate.
Constraint: ${\mathbf{dup}}=\mathrm{Nag_SparseNsym_RemoveDups}$, $\mathrm{Nag_SparseNsym_SumDups}$ or $\mathrm{Nag_SparseNsym_FailDups}$.
7: $\mathbf{zero}$Nag_SparseNsym_Zeros Input
On entry: indicates how elements in a with zero values are to be treated.
${\mathbf{zero}}=\mathrm{Nag_SparseNsym_RemoveZeros}$
The entries are removed.
${\mathbf{zero}}=\mathrm{Nag_SparseNsym_KeepZeros}$
The entries are kept.
${\mathbf{zero}}=\mathrm{Nag_SparseNsym_FailZeros}$
The function fails with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_ZERO_COEFF on detecting a zero.
Constraint: ${\mathbf{zero}}=\mathrm{Nag_SparseNsym_RemoveZeros}$, $\mathrm{Nag_SparseNsym_KeepZeros}$ or $\mathrm{Nag_SparseNsym_FailZeros}$.
8: $\mathbf{istr}\left[{\mathbf{n}}+1\right]$Integer Output
On exit: ${\mathbf{istr}}\left[\mathit{i}-1\right]-1$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$, is the starting address in the arrays a, irow and icol of row $i$ of the matrix $A$. ${\mathbf{istr}}\left[{\mathbf{n}}\right]-1$ is the address of the last element in a plus one.
9: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
NE_BAD_PARAM
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
On entry, ${\mathbf{nnz}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nnz}}\ge 0$.
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 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_NON_ZERO_DUP
On entry, a duplicate entry has been found in row $i$ and column $j$: $i=〈\mathit{\text{value}}〉$, $j=〈\mathit{\text{value}}〉$.
NE_NONSYMM_MATRIX
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{icol}}\left[i-1\right]=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{icol}}\left[i-1\right]\ge 1$ and ${\mathbf{icol}}\left[i-1\right]\le {\mathbf{n}}$.
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{irow}}\left[i-1\right]=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{irow}}\left[i-1\right]\ge 1$ and ${\mathbf{irow}}\left[i-1\right]\le {\mathbf{n}}$.
NE_ZERO_COEFF
On entry, a zero entry has been found in row $i$ and column $j$: $i=〈\mathit{\text{value}}〉$, $j=〈\mathit{\text{value}}〉$.

Not applicable.

## 8Parallelism and Performance

f11zac is not threaded in any implementation.

## 9Further Comments

The time taken for a call to f11zac is the sum of two contributions, where one is proportional to nnz and the other is proportional to n.
Note that the resulting matrix may have either rows or columns with no entries. If row $i$ has no entries then ${\mathbf{istr}}\left[i-1\right]={\mathbf{istr}}\left[i\right]$.
To transpose a matrix in CS format simply interchange irow and icol. If you need the elements to be sorted, then pass these interchanged arrays to f11zac.
Two sparse matrices can be added by concatenating the three pairs of CS format arrays, representing the two matrices, and passing these new arrays to f11zac, specifying that duplicates should be summed. This functionality is illustrated in Section 10.
It is also possible to use this function to convert between coordinate storage (CS) and compressed column storage (CCS) formats. To achieve this the CS format array holding the row indices must be passed as icol and the array holding the column indices must be passed as irow in a call to f11zac. On exit from f11zac, the CCS representation of the matrix is given by the output arrays a, icol, and istr, where icol holds irowix and istr holds icolzp as described in Section 2.1.3 in the F11 Chapter Introduction. This is illustrated in Section 10.

## 10Example

This example reads the CS representation of the real sparse matrices $B$ and $C$, and finds their sum, $A$, displaying the ordered elements in CS format. The matrix $A$ is then converted to CCS format and its $1$-norm found. The CCS format is converted back to CS format and checked to be identical to the original ordered CS representation. The transpose of $A$ is also found and displayed in CS format.
 $B= 2.00 1.00 0 0 0 0 0 1.00 -1.00 0 4.00 0 1.00 0 1.00 0 0 0 1.00 2.00 0 -2.00 0 0 3.00 and C= 4.00 0 2.00 0 3.00 0 -1.00 0 1.00 2.00 0 0 0 2.00 0 3.00 1.00 0 0 -3.00 0 -2.00 0 4.00 0 .$

### 10.1Program Text

Program Text (f11zace.c)

### 10.2Program Data

Program Data (f11zace.d)

### 10.3Program Results

Program Results (f11zace.r)