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

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_sparse_real_gen_matvec (f11xa)

## Purpose

nag_sparse_real_gen_matvec (f11xa) computes a matrix-vector or transposed matrix-vector product involving a real sparse nonsymmetric matrix stored in coordinate storage format.

## Syntax

[y, ifail] = f11xa(trans, a, irow, icol, check, x, 'n', n, 'nz', nz)
[y, ifail] = nag_sparse_real_gen_matvec(trans, a, irow, icol, check, x, 'n', n, 'nz', nz)

## Description

nag_sparse_real_gen_matvec (f11xa) computes either the matrix-vector product $y=Ax$, or the transposed matrix-vector product $y={A}^{\mathrm{T}}x$, according to the value of the argument trans, where $A$ is an $n$ by $n$ sparse nonsymmetric matrix, of arbitrary sparsity pattern. The matrix $A$ is stored in coordinate storage (CS) format (see Coordinate storage (CS) format in the F11 Chapter Introduction). The array a stores all nonzero elements of $A$, while arrays irow and icol store the corresponding row and column indices respectively.
It is envisaged that a common use of nag_sparse_real_gen_matvec (f11xa) will be to compute the matrix-vector product required in the application of nag_sparse_real_gen_basic_solver (f11be) to sparse linear systems. An illustration of this usage appears in Example in nag_sparse_real_gen_precon_ssor_solve (f11dd).

None.

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{trans}$ – string (length ≥ 1)
Specifies whether or not the matrix $A$ is transposed.
${\mathbf{trans}}=\text{'N'}$
$y=Ax$ is computed.
${\mathbf{trans}}=\text{'T'}$
$y={A}^{\mathrm{T}}x$ is computed.
Constraint: ${\mathbf{trans}}=\text{'N'}$ or $\text{'T'}$.
2:     $\mathrm{a}\left({\mathbf{nz}}\right)$ – double array
The nonzero elements in the matrix $A$, ordered by increasing row index, and by increasing column index within each row. Multiple entries for the same row and column indices are not permitted. The function nag_sparse_real_gen_sort (f11za) may be used to order the elements in this way.
3:     $\mathrm{irow}\left({\mathbf{nz}}\right)$int64int32nag_int array
4:     $\mathrm{icol}\left({\mathbf{nz}}\right)$int64int32nag_int array
The row and column indices of the nonzero elements supplied in array a.
Constraints:
irow and icol must satisfy the following constraints (which may be imposed by a call to nag_sparse_real_gen_sort (f11za)):
• $1\le {\mathbf{irow}}\left(\mathit{i}\right)\le {\mathbf{n}}$ and $1\le {\mathbf{icol}}\left(\mathit{i}\right)\le {\mathbf{n}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{nz}}$;
• ${\mathbf{irow}}\left(\mathit{i}-1\right)<{\mathbf{irow}}\left(\mathit{i}\right)$ or ${\mathbf{irow}}\left(\mathit{i}-1\right)={\mathbf{irow}}\left(\mathit{i}\right)$ and ${\mathbf{icol}}\left(\mathit{i}-1\right)<{\mathbf{icol}}\left(\mathit{i}\right)$, for $\mathit{i}=2,3,\dots ,{\mathbf{nz}}$.
5:     $\mathrm{check}$ – string (length ≥ 1)
Specifies whether or not the CS representation of the matrix $A$, values of n, nz, irow and icol should be checked.
${\mathbf{check}}=\text{'C'}$
Checks are carried on the values of n, nz, irow and icol.
${\mathbf{check}}=\text{'N'}$
None of these checks are carried out.
Constraint: ${\mathbf{check}}=\text{'C'}$ or $\text{'N'}$.
6:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
The vector $x$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array x.
$n$, the order of the matrix $A$.
Constraint: ${\mathbf{n}}\ge 1$.
2:     $\mathrm{nz}$int64int32nag_int scalar
Default: the dimension of the arrays a, irow, icol. (An error is raised if these dimensions are not equal.)
The number of nonzero elements in the matrix $A$.
Constraint: $1\le {\mathbf{nz}}\le {{\mathbf{n}}}^{2}$.

### Output Parameters

1:     $\mathrm{y}\left({\mathbf{n}}\right)$ – double array
The vector $y$.
2:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{trans}}\ne \text{'N'}$ or $\text{'T'}$, or ${\mathbf{check}}\ne \text{'C'}$ or $\text{'N'}$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{n}}<1$, or ${\mathbf{nz}}<1$, or ${\mathbf{nz}}>{{\mathbf{n}}}^{2}$.
${\mathbf{ifail}}=3$
On entry, the arrays irow and icol fail to satisfy the following constraints:
• $1\le {\mathbf{irow}}\left(i\right)\le {\mathbf{n}}$ and $1\le {\mathbf{icol}}\left(i\right)\le {\mathbf{n}}$, for $i=1,2,\dots ,{\mathbf{nz}}$;
• ${\mathbf{irow}}\left(i-1\right)<{\mathbf{irow}}\left(i\right)$, or ${\mathbf{irow}}\left(i-1\right)={\mathbf{irow}}\left(i\right)$ and ${\mathbf{icol}}\left(i-1\right)<{\mathbf{icol}}\left(i\right)$, for $i=2,3,\dots ,{\mathbf{nz}}$.
Therefore a nonzero element has been supplied which does not lie within the matrix $A$, is out of order, or has duplicate row and column indices. Call nag_sparse_real_gen_sort (f11za) to reorder and sum or remove duplicates.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

The computed vector $y$ satisfies the error bound:
• ${‖y-Ax‖}_{\infty }\le c\left(n\right)\epsilon {‖A‖}_{\infty }{‖x‖}_{\infty }$, if ${\mathbf{trans}}=\text{'N'}$, or
• ${‖y-{A}^{\mathrm{T}}x‖}_{\infty }\le c\left(n\right)\epsilon {‖{A}^{\mathrm{T}}‖}_{\infty }{‖x‖}_{\infty }$, if ${\mathbf{trans}}=\text{'T'}$,
where $c\left(n\right)$ is a modest linear function of $n$, and $\epsilon$ is the machine precision

### Timing

The time taken for a call to nag_sparse_real_gen_matvec (f11xa) is proportional to nz.

### Use of check

It is expected that a common use of nag_sparse_real_gen_matvec (f11xa) will be to compute the matrix-vector product required in the application of nag_sparse_real_gen_basic_solver (f11be) to sparse linear systems. In this situation nag_sparse_real_gen_matvec (f11xa) is likely to be called many times with the same matrix $A$. In the interests of both reliability and efficiency you are recommended to set ${\mathbf{check}}=\text{'C'}$ for the first of such calls, and to set ${\mathbf{check}}=\text{'N'}$ for all subsequent calls.

## Example

This example reads in a sparse matrix $A$ and a vector $x$. It then calls nag_sparse_real_gen_matvec (f11xa) to compute the matrix-vector product $y=Ax$ and the transposed matrix-vector product $y={A}^{\mathrm{T}}x$.
```function f11xa_example

fprintf('f11xa example results\n\n');

% Sparse Matrix vector Products y=Ax, y = A^Tx

% 5x5 sparse matrix A and vector x
a    =         [2; 1; 1;-1; 4; 1; 1; 1; 2;-2; 3];
irow = int64([1; 1; 2; 2; 3; 3; 3; 4; 4; 5; 5]);
icol = int64([1; 2; 3; 4; 1; 3; 5; 4; 5; 2; 5]);

x    = [0.7; 0.16; 0.52; 0.77; 0.28];

% Calculate matrix-vector product
trans = 'N';
check = 'C';
[y, ifail] = f11xa( ...
trans, a, irow, icol, check, x);
fprintf('Matrix-vector product:\n');
disp(y);

% Calculate transposed matrix-vector product
trans = 'T';
[y, ifail] = f11xa( ...
trans, a, irow, icol, check, x);
fprintf('Transposed matrix-vector product:\n');
disp(y);

```
```f11xa example results

Matrix-vector product:
1.5600
-0.2500
3.6000
1.3300
0.5200

Transposed matrix-vector product:
3.4800
0.1400
0.6800
0.6100
2.9000

```

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015