Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_sparse_real_gen_sort (f11za)

## Purpose

nag_sparse_real_gen_sort (f11za) sorts the nonzero elements of a real sparse nonsymmetric matrix, represented in coordinate storage format.

## Syntax

[nnz, a, irow, icol, istr, ifail] = f11za(n, nnz, a, irow, icol, dup, zer)
[nnz, a, irow, icol, istr, ifail] = nag_sparse_real_gen_sort(n, nnz, a, irow, icol, dup, zer)

## Description

nag_sparse_real_gen_sort (f11za) takes a coordinate storage (CS) representation (see Section [Coordinate storage (CS) format] in the F11 Chapter Introduction) of a real n$n$ by n$n$ sparse nonsymmetric matrix A$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, or the values may be summed. Any entries with zero values may optionally be removed.
nag_sparse_real_gen_sort (f11za) also returns a pointer istr to the starting address of each row in A$A$.

None.

## Parameters

### Compulsory Input Parameters

1:     n – int64int32nag_int scalar
n$n$, the order of the matrix A$A$.
Constraint: n1${\mathbf{n}}\ge 1$.
2:     nnz – int64int32nag_int scalar
The number of nonzero elements in the matrix A$A$.
Constraint: nnz0${\mathbf{nnz}}\ge 0$.
3:     a( : $:$) – double array
Note: the dimension of the array a must be at least max (1,nnz)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nnz}}\right)$.
The nonzero elements of the matrix A$A$. These may be in any order and there may be multiple nonzero elements with the same row and column indices.
4:     irow( : $:$) – int64int32nag_int array
Note: the dimension of the array irow must be at least max (1,nnz)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nnz}}\right)$.
The row indices corresponding to the nonzero elements supplied in the array a.
Constraint: 1irow(i)n$1\le {\mathbf{irow}}\left(\mathit{i}\right)\le {\mathbf{n}}$, for i = 1,2,,nnz$\mathit{i}=1,2,\dots ,{\mathbf{nnz}}$.
5:     icol( : $:$) – int64int32nag_int array
Note: the dimension of the array icol must be at least max (1,nnz)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nnz}}\right)$.
The column indices corresponding to the nonzero elements supplied in the array a.
Constraint: 1icol(i)n$1\le {\mathbf{icol}}\left(\mathit{i}\right)\le {\mathbf{n}}$, for i = 1,2,,nnz$\mathit{i}=1,2,\dots ,{\mathbf{nnz}}$.
6:     dup – string (length ≥ 1)
Indicates how any nonzero elements with duplicate row and column indices are to be treated.
dup = 'R'${\mathbf{dup}}=\text{'R'}$
The entries are removed.
dup = 'S'${\mathbf{dup}}=\text{'S'}$
The relevant values in a are summed.
dup = 'F'${\mathbf{dup}}=\text{'F'}$
The function fails on detecting a duplicate, with ${\mathbf{ifail}}={\mathbf{3}}$.
Constraint: dup = 'R'${\mathbf{dup}}=\text{'R'}$, 'S'$\text{'S'}$ or 'F'$\text{'F'}$.
7:     zer – string (length ≥ 1)
Indicates how any elements with zero values in a are to be treated.
zer = 'R'${\mathbf{zer}}=\text{'R'}$
The entries are removed.
zer = 'K'${\mathbf{zer}}=\text{'K'}$
The entries are kept.
zer = 'F'${\mathbf{zer}}=\text{'F'}$
The function fails on detecting a zero, with ${\mathbf{ifail}}={\mathbf{4}}$.
Constraint: zer = 'R'${\mathbf{zer}}=\text{'R'}$, 'K'$\text{'K'}$ or 'F'$\text{'F'}$.

None.

iwork

### Output Parameters

1:     nnz – int64int32nag_int scalar
The number of nonzero elements with unique row and column indices.
2:     a( : $:$) – double array
Note: the dimension of the array a must be at least max (1,nnz)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nnz}}\right)$.
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.
3:     irow( : $:$) – int64int32nag_int array
Note: the dimension of the array irow must be at least max (1,nnz)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nnz}}\right)$.
The first nnz elements contain the row indices corresponding to the nonzero elements returned in the array a.
4:     icol( : $:$) – int64int32nag_int array
Note: the dimension of the array icol must be at least max (1,nnz)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nnz}}\right)$.
The first nnz elements contain the row indices corresponding to the nonzero elements returned in the array a.
5:     istr(n + 1${\mathbf{n}}+1$) – int64int32nag_int array
istr(i)${\mathbf{istr}}\left(\mathit{i}\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,{\mathbf{n}}$, is the starting address in the arrays a, irow and icol of row i$i$ of the matrix A$A$. istr(n + 1)${\mathbf{istr}}\left({\mathbf{n}}+1\right)$ is the address of the last nonzero element in A$A$ plus one.
6:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
 On entry, n < 1${\mathbf{n}}<1$, or nnz < 0${\mathbf{nnz}}<0$, or dup ≠ 'R','S'${\mathbf{dup}}\ne \text{'R'},\text{'S'}$, or 'F', or zer ≠ 'R','K'${\mathbf{zer}}\ne \text{'R'},\text{'K'}$, or 'F'.
ifail = 2${\mathbf{ifail}}=2$
On entry, a nonzero element has been supplied which does not lie within the matrix A$A$, i.e., one or more of the following constraints has been violated:
• 1irow(i)n$1\le {\mathbf{irow}}\left(i\right)\le {\mathbf{n}}$,
• 1icol(i)n$1\le {\mathbf{icol}}\left(i\right)\le {\mathbf{n}}$,
for i = 1,2,,nnz$i=1,2,\dots ,{\mathbf{nnz}}$
ifail = 3${\mathbf{ifail}}=3$
On entry, dup = 'F'${\mathbf{dup}}=\text{'F'}$ and nonzero elements have been supplied which have duplicate row and column indices.
ifail = 4${\mathbf{ifail}}=4$
On entry, zer = 'F'${\mathbf{zer}}=\text{'F'}$ and at least one matrix element has been supplied with a zero coefficient value.

## Accuracy

Not applicable.

The time taken for a call to nag_sparse_real_gen_sort (f11za) is proportional to nnz.
Note that the resulting matrix may have either rows or columns with no entries. If row i$i$ has no entries then istr(i) = istr(i + 1)${\mathbf{istr}}\left(i\right)={\mathbf{istr}}\left(i+1\right)$.

## Example

```function nag_sparse_real_gen_sort_example
n = int64(5);
nz = int64(15);
a = [4;
-2;
1;
-2;
-3;
1;
0;
1;
-1;
6;
2;
2;
1;
1;
2];
irow = [int64(3);5;4;4;5;1;1;3;2;5;1;4;2;3;4];
icol = [int64(1);2;4;2;5;2;5;5;4;5;1;2;3;3;5];
dup = 'S';
zero = 'R';
[nzOut, aOut, irowOut, icolOut, istr, ifail] = ...
nag_sparse_real_gen_sort(n, nz, a, irow, icol, dup, zero)
```
```

nzOut =

11

aOut =

2
1
1
-1
4
1
1
1
2
-2
3

irowOut =

1
1
2
2
3
3
3
4
4
5
5

icolOut =

1
2
3
4
1
3
5
4
5
2
5

istr =

1
3
5
8
10
12

ifail =

0

```
```function f11za_example
n = int64(5);
nz = int64(15);
a = [4;
-2;
1;
-2;
-3;
1;
0;
1;
-1;
6;
2;
2;
1;
1;
2];
irow = [int64(3);5;4;4;5;1;1;3;2;5;1;4;2;3;4];
icol = [int64(1);2;4;2;5;2;5;5;4;5;1;2;3;3;5];
dup = 'S';
zero = 'R';
[nzOut, aOut, irowOut, icolOut, istr, ifail] = ...
f11za(n, nz, a, irow, icol, dup, zero)
```
```

nzOut =

11

aOut =

2
1
1
-1
4
1
1
1
2
-2
3

irowOut =

1
1
2
2
3
3
3
4
4
5
5

icolOut =

1
2
3
4
1
3
5
4
5
2
5

istr =

1
3
5
8
10
12

ifail =

0

```