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_basic_solver (f11be)

## Purpose

nag_sparse_real_gen_basic_solver (f11be) is an iterative solver for a real general (nonsymmetric) system of simultaneous linear equations; nag_sparse_real_gen_basic_solver (f11be) is the second in a suite of three functions, where the first function, nag_sparse_real_gen_basic_setup (f11bd), must be called prior to nag_sparse_real_gen_basic_solver (f11be) to set up the suite, and the third function in the suite, nag_sparse_real_gen_basic_diag (f11bf), can be used to return additional information about the computation.
These three functions are suitable for the solution of large sparse general (nonsymmetric) systems of equations.

## Syntax

[irevcm, u, v, work, ifail] = f11be(irevcm, u, v, wgt, work, 'lwork', lwork)
[irevcm, u, v, work, ifail] = nag_sparse_real_gen_basic_solver(irevcm, u, v, wgt, work, 'lwork', lwork)

## Description

nag_sparse_real_gen_basic_solver (f11be) solves the general (nonsymmetric) system of linear simultaneous equations $Ax=b$ of order $\mathit{n}$, where $\mathit{n}$ is large and the coefficient matrix $A$ is sparse, using one of four available methods: RGMRES, the preconditioned restarted generalized minimum residual method (see Saad and Schultz (1986)); CGS, the preconditioned conjugate gradient squared method (see Sonneveld (1989)); Bi-CGSTAB($\ell$), the bi-conjugate gradient stabilized method of order $\ell$ (see Van der Vorst (1989) and Sleijpen and Fokkema (1993)); or TFQMR, the transpose-free quasi-minimal residual method (see Freund and Nachtigal (1991) and Freund (1993)).
For a general description of the methods employed you are referred to Description in nag_sparse_real_gen_basic_setup (f11bd).
nag_sparse_real_gen_basic_solver (f11be) can solve the system after the first function in the suite, nag_sparse_real_gen_basic_setup (f11bd), has been called to initialize the computation and specify the method of solution. The third function in the suite, nag_sparse_real_gen_basic_diag (f11bf), can be used to return additional information generated by the computation, during monitoring steps and after nag_sparse_real_gen_basic_solver (f11be) has completed its tasks.
nag_sparse_real_gen_basic_solver (f11be) uses reverse communication, i.e., it returns repeatedly to the calling program with the argument irevcm (see Arguments) set to specified values which require the calling program to carry out one of the following tasks:
 – compute the matrix-vector product $v=Au$ or $v={A}^{\mathrm{T}}u$ (the four methods require the matrix transpose-vector product only if ${‖A‖}_{1}$ or ${‖A‖}_{\infty }$ is estimated internally by Higham's method (see Higham (1988))); – solve the preconditioning equation $Mv=u$; – notify the completion of the computation; – allow the calling program to monitor the solution.
Through the argument irevcm the calling program can cause immediate or tidy termination of the execution. On final exit, the last iterates of the solution and of the residual vectors of the original system of equations are returned.
Reverse communication has the following advantages.
 1 Maximum flexibility in the representation and storage of sparse matrices: all matrix operations are performed outside the solver function, thereby avoiding the need for a complicated interface with enough flexibility to cope with all types of storage schemes and sparsity patterns. This applies also to preconditioners. 2 Enhanced user interaction: you can closely monitor the progress of the solution and tidy or immediate termination can be requested. This is useful, for example, when alternative termination criteria are to be employed or in case of failure of the external functions used to perform matrix operations.

## References

Freund R W (1993) A transpose-free quasi-minimal residual algorithm for non-Hermitian linear systems SIAM J. Sci. Comput. 14 470–482
Freund R W and Nachtigal N (1991) QMR: a Quasi-Minimal Residual Method for Non-Hermitian Linear Systems Numer. Math. 60 315–339
Higham N J (1988) FORTRAN codes for estimating the one-norm of a real or complex matrix, with applications to condition estimation ACM Trans. Math. Software 14 381–396
Saad Y and Schultz M (1986) GMRES: a generalized minimal residual algorithm for solving nonsymmetric linear systems SIAM J. Sci. Statist. Comput. 7 856–869
Sleijpen G L G and Fokkema D R (1993) BiCGSTAB$\left(\ell \right)$ for linear equations involving matrices with complex spectrum ETNA 1 11–32
Sonneveld P (1989) CGS, a fast Lanczos-type solver for nonsymmetric linear systems SIAM J. Sci. Statist. Comput. 10 36–52
Van der Vorst H (1989) Bi-CGSTAB, a fast and smoothly converging variant of Bi-CG for the solution of nonsymmetric linear systems SIAM J. Sci. Statist. Comput. 13 631–644

## Parameters

Note: this function uses reverse communication. Its use involves an initial entry, intermediate exits and re-entries, and a final exit, as indicated by the argument irevcm. Between intermediate exits and re-entries, all arguments other than irevcm and v must remain unchanged.

### Compulsory Input Parameters

1:     $\mathrm{irevcm}$int64int32nag_int scalar
On initial entry: ${\mathbf{irevcm}}=0$, otherwise an error condition will be raised.
On intermediate re-entry: must either be unchanged from its previous exit value, or can have one of the following values.
${\mathbf{irevcm}}=5$
Tidy termination: the computation will terminate at the end of the current iteration. Further reverse communication exits may occur depending on when the termination request is issued. nag_sparse_real_gen_basic_solver (f11be) will then return with the termination code ${\mathbf{irevcm}}=4$. Note that before calling nag_sparse_real_gen_basic_solver (f11be) with ${\mathbf{irevcm}}=5$ the calling program must have performed the tasks required by the value of irevcm returned by the previous call to nag_sparse_real_gen_basic_solver (f11be), otherwise subsequently returned values may be invalid.
${\mathbf{irevcm}}=6$
Immediate termination: nag_sparse_real_gen_basic_solver (f11be) will return immediately with termination code ${\mathbf{irevcm}}=4$ and with any useful information available. This includes the last iterate of the solution. The residual vector is generally not available.
Immediate termination may be useful, for example, when errors are detected during matrix-vector multiplication or during the solution of the preconditioning equation.
Changing irevcm to any other value between calls will result in an error.
Constraint: on initial entry, ${\mathbf{irevcm}}=0$; on re-entry, either irevcm must remain unchanged or be reset to $5$ or $6$.
2:     $\mathrm{u}\left(:\right)$ – double array
The dimension of the array u must be at least $\mathit{n}$
On initial entry: an initial estimate, ${x}_{0}$, of the solution of the system of equations $Ax=b$.
On intermediate re-entry: must remain unchanged.
3:     $\mathrm{v}\left(:\right)$ – double array
The dimension of the array v must be at least $\mathit{n}$
On initial entry: the right-hand side $b$ of the system of equations $Ax=b$.
On intermediate re-entry: the returned value of irevcm determines the contents of v in the following way:
• if ${\mathbf{irevcm}}=-1$, $1$ or $2$, v must store the vector $v$, the result of the operation specified by the value of irevcm returned by the previous call to nag_sparse_real_gen_basic_solver (f11be);
• if ${\mathbf{irevcm}}=3$, v must remain unchanged.
4:     $\mathrm{wgt}\left(:\right)$ – double array
The dimension of the array wgt must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,\mathit{n}\right)$
The user-supplied weights, if these are to be used in the computation of the vector norms in the termination criterion (see Description and Arguments in nag_sparse_real_gen_basic_setup (f11bd)).
Constraint: if weights are to be used, at least one element of wgt must be nonzero.
5:     $\mathrm{work}\left({\mathbf{lwork}}\right)$ – double array
On initial entry: the array work as returned by nag_sparse_real_gen_basic_setup (f11bd) (see also Arguments in nag_sparse_real_gen_basic_setup (f11bd)).
On intermediate re-entry: must remain unchanged.

### Optional Input Parameters

1:     $\mathrm{lwork}$int64int32nag_int scalar
Default: the dimension of the array work.
On initial entry: the dimension of the array work (see also Description and Arguments in nag_sparse_real_gen_basic_setup (f11bd)). the required amount of workspace is as follows:
 Method Requirements RGMRES ${\mathbf{lwork}}=100+\mathit{n}\left(m+3\right)+m\left(m+5\right)+1$, where $m$ is the dimension of the basis. CGS ${\mathbf{lwork}}=100+7\mathit{n}$. Bi-CGSTAB($\ell$) ${\mathbf{lwork}}=100+\left(2\mathit{n}+\ell \right)\left(\ell +2\right)+p$, where $\ell$ is the order of the method. TFQMR ${\mathbf{lwork}}=100+10\mathit{n}$,
where
 $p=2\mathit{n}$ if $\ell >1$ and ${\mathbf{iterm}}=2$ was supplied. $p=\mathit{n}$ if $\ell >1$ and a preconditioner is used or ${\mathbf{iterm}}=2$ was supplied. $p=0$ otherwise.
Constraint: ${\mathbf{lwork}}\ge {\mathbf{lwreq}}$, where lwreq is returned by nag_sparse_real_gen_basic_setup (f11bd).

### Output Parameters

1:     $\mathrm{irevcm}$int64int32nag_int scalar
On intermediate exit: has the following meanings.
${\mathbf{irevcm}}=-1$
The calling program must compute the matrix-vector product $v={A}^{\mathrm{T}}u$, where $u$ and $v$ are stored in u and v, respectively; RGMRES, CGS and Bi-CGSTAB($\ell$) methods return ${\mathbf{irevcm}}=-1$ only if the matrix norm ${‖A‖}_{1}$ or ${‖A‖}_{\infty }$ is estimated internally using Higham's method. This can only happen if ${\mathbf{iterm}}=1$ in nag_sparse_real_gen_basic_setup (f11bd).
${\mathbf{irevcm}}=1$
The calling program must compute the matrix-vector product $v=Au$, where $u$ and $v$ are stored in u and v, respectively.
${\mathbf{irevcm}}=2$
The calling program must solve the preconditioning equation $Mv=u$, where $u$ and $v$ are stored in u and v, respectively.
${\mathbf{irevcm}}=3$
Monitoring step: the solution and residual at the current iteration are returned in the arrays u and v, respectively. No action by the calling program is required. nag_sparse_real_gen_basic_diag (f11bf) can be called at this step to return additional information.
On final exit: ${\mathbf{irevcm}}=4$: nag_sparse_real_gen_basic_solver (f11be) has completed its tasks. The value of ifail determines whether the iteration has been successfully completed, errors have been detected or the calling program has requested termination.
2:     $\mathrm{u}\left(:\right)$ – double array
The dimension of the array u will be $\mathit{n}$
On intermediate exit: the returned value of irevcm determines the contents of u in the following way:
• if ${\mathbf{irevcm}}=-1$, $1$ or $2$, u holds the vector $u$ on which the operation specified by irevcm is to be carried out;
• if ${\mathbf{irevcm}}=3$, u holds the current iterate of the solution vector.
On final exit: if ${\mathbf{ifail}}={\mathbf{3}}$ or ${\mathbf{ifail}}<{\mathbf{0}}$, the array u is unchanged from the initial entry to nag_sparse_real_gen_basic_solver (f11be).
If ${\mathbf{ifail}}={\mathbf{1}}$, the array u is unchanged from the last entry to nag_sparse_real_gen_basic_solver (f11be).
Otherwise, u holds the last available iterate of the solution of the system of equations, for all returned values of ifail.
3:     $\mathrm{v}\left(:\right)$ – double array
The dimension of the array v will be $\mathit{n}$
On intermediate exit: if ${\mathbf{irevcm}}=3$, v holds the current iterate of the residual vector. Note that this is an approximation to the true residual vector. Otherwise, it does not contain any useful information.
On final exit: if ${\mathbf{ifail}}={\mathbf{3}}$ or ${\mathbf{ifail}}<{\mathbf{0}}$, the array v is unchanged from the initial entry to nag_sparse_real_gen_basic_solver (f11be).
If ${\mathbf{ifail}}={\mathbf{1}}$, the array v is unchanged from the last entry to nag_sparse_real_gen_basic_solver (f11be).
If ${\mathbf{ifail}}={\mathbf{0}}$ or ${\mathbf{2}}$, the array v contains the true residual vector of the system of equations (see also Error Indicators and Warnings).
Otherwise, v stores the last available iterate of the residual vector unless ${\mathbf{ifail}}={\mathbf{8}}$ is returned on last entry, in which case v is set to $0.0$.
4:     $\mathrm{work}\left({\mathbf{lwork}}\right)$ – double array
5:     $\mathrm{ifail}$int64int32nag_int scalar
On final exit: ${\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:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

${\mathbf{ifail}}=-i$
If ${\mathbf{ifail}}=-i$, parameter $i$ had an illegal value on entry. The parameters are numbered as follows:
1: irevcm, 2: u, 3: v, 4: wgt, 5: work, 6: lwork, 7: ifail.
W  ${\mathbf{ifail}}=1$
nag_sparse_real_gen_basic_solver (f11be) has been called again after returning the termination code ${\mathbf{irevcm}}=4$. No further computation has been carried out and all input data and data stored for access by nag_sparse_real_gen_basic_diag (f11bf) have remained unchanged.
W  ${\mathbf{ifail}}=2$
The required accuracy could not be obtained. However, nag_sparse_real_gen_basic_solver (f11be) has terminated with reasonable accuracy: the last iterate of the residual satisfied the termination criterion but the exact residual $r=b-Ax$, did not. After the first occurrence of this situation, the iteration was restarted once, but nag_sparse_real_gen_basic_solver (f11be) could not improve on the accuracy. This error code usually implies that your problem has been fully and satisfactorily solved to within or close to the accuracy available on your system. Further iterations are unlikely to improve on this situation. You should call nag_sparse_real_gen_basic_diag (f11bf) to check the values of the left- and right-hand sides of the termination condition.
${\mathbf{ifail}}=3$
nag_sparse_real_gen_basic_setup (f11bd) was either not called before calling nag_sparse_real_gen_basic_solver (f11be) or it returned an error. The arguments u and v remain unchanged.
W  ${\mathbf{ifail}}=4$
The calling program requested a tidy termination before the solution had converged. The arrays u and v return the last iterates available of the solution and of the residual vector, respectively.
W  ${\mathbf{ifail}}=5$
The solution did not converge within the maximum number of iterations allowed. The arrays u and v return the last iterates available of the solution and of the residual vector, respectively.
${\mathbf{ifail}}=6$
Algorithmic breakdown. The last available iterates of the solution and residuals are returned, although it is possible that they are completely inaccurate.
W  ${\mathbf{ifail}}=8$
The calling program requested an immediate termination. However, the array u returns the last iterate of the solution, the array v returns the last iterate of the residual vector, for the CGS and TFQMR methods only.
${\mathbf{ifail}}=10$
User-supplied weights are to be used, but all elements of the array wgt are zero.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

On completion, i.e., ${\mathbf{irevcm}}=4$ on exit, the arrays u and v will return the solution and residual vectors, ${x}_{k}$ and ${r}_{k}=b-A{x}_{k}$, respectively, at the $k$th iteration, the last iteration performed, unless an immediate termination was requested.
On successful completion, the termination criterion is satisfied to within the user-specified tolerance, as described in Description in nag_sparse_real_gen_basic_setup (f11bd). The computed values of the left- and right-hand sides of the termination criterion selected can be obtained by a call to nag_sparse_real_gen_basic_diag (f11bf).

The number of operations carried out by nag_sparse_real_gen_basic_solver (f11be) for each iteration is likely to be principally determined by the computation of the matrix-vector products $v=Au$ and by the solution of the preconditioning equation $Mv=u$ in the calling program. Each of these operations is carried out once every iteration.
The number of the remaining operations in nag_sparse_real_gen_basic_solver (f11be) for each iteration is approximately proportional to $\mathit{n}$.
The number of iterations required to achieve a prescribed accuracy cannot be easily determined at the onset, as it can depend dramatically on the conditioning and spectrum of the preconditioned matrix of the coefficients $\stackrel{-}{A}={M}^{-1}A$ (RGMRES, CGS and TFQMR methods) or $\stackrel{-}{A}=A{M}^{-1}$ (Bi-CGSTAB($\ell$) method).
Additional matrix-vector products are required for the computation of ${‖A‖}_{1}$ or ${‖A‖}_{\infty }$, when this has not been supplied to nag_sparse_real_gen_basic_setup (f11bd) and is required by the termination criterion employed.
If the termination criterion ${‖{r}_{k}‖}_{p}\le \tau \left({‖b‖}_{p}+{‖A‖}_{p}×{‖{x}_{k}‖}_{p}\right)$ is used (see Description in nag_sparse_real_gen_basic_setup (f11bd)) and $‖{x}_{0}‖\gg ‖{x}_{k}‖$, then the required accuracy cannot be obtained due to loss of significant digits. The iteration is restarted automatically at some suitable point: nag_sparse_real_gen_basic_solver (f11be) sets ${x}_{0}={x}_{k}$ and the computation begins again. For particularly badly scaled problems, more than one restart may be necessary. This does not apply to the RGMRES method which, by its own nature, self-restarts every super-iteration. Naturally, restarting adds to computational costs: it is recommended that the iteration should start from a value ${x}_{0}$ which is as close to the true solution $\stackrel{~}{x}$ as can be estimated. Otherwise, the iteration should start from ${x}_{0}=0$.

## Example

See Example in nag_sparse_real_gen_basic_setup (f11bd).
```function f11be_example

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

% Solve sparse system Ax = b iteratively using ILU preconditioner

% Define sparse matrix A and RHS B
n    = int64(8);
nz   = int64(24);
a    = zeros(3*nz, 1);
irow = zeros(3*nz, 1, 'int64');
icol = irow;
irow(1:nz) = int64(...
[1; 1; 1; 2; 2; 2; 3; 3; 4; 4; 4; 4;
5; 5; 5; 6; 6; 6; 7; 7; 7; 8; 8; 8]);
icol(1:nz) = int64(...
[1; 4; 8; 1; 2; 5; 3; 6; 1; 3; 4; 7;
2; 5; 7; 1; 3; 6; 3; 5; 7; 2; 6; 8]);
a(1:nz)    = [2;-1; 1; 4;-3; 2;-7; 2; 3;-4; 5; 5;
-1; 8;-3;-6; 5; 2;-5;-1; 6;-1; 2; 3];
b          = [6; 8;-9;46;17;21;22;34];

% ILU preconditioner
%     Input parameters
lfill = int64(0);
dtol  = 0;
milu  = 'No modification';
ipivp = zeros(n, 1, 'int64');
ipivq = zeros(n, 1, 'int64');
% Compute preconditioner
[a, irow, icol, ipivp, ipivq, istr, idiag, nnzc, npivm, ifail] = ...
f11da(...
nz, a, irow, icol, lfill, dtol, milu, ipivp, ipivq);

% Iterative Solution Setup
%      Input parameters
method = 'BICGSTAB';
precon = 'Preconditioned';
lpoly  = int64(1);
tol    = sqrt(x02aj);
maxitn = int64(20);
anorm  = 0;
sigmax = 0;
monit  = int64(1);
lwork  = int64(6000);
% Setup iterative method
[lwreq, work, ifail] = ...
f11bd(...
method, precon, n, lpoly, tol, maxitn, anorm, sigmax, ...
monit, lwork, 'norm_p', '1');

% Reverse communication loop calling iterative solver
irevcm = int64(0);
u      = zeros(8, 1);
v      = b;
wgt    = zeros(8, 1);

while (irevcm ~= 4)

[irevcm, u, v, work, ifail] = f11be(...
irevcm, u, v, wgt, work);

if (irevcm == -1)
% v = A^Tu
[v, ifail] = f11xa(...
'T', a(1:nz), irow(1:nz), icol(1:nz), 'N', u);
elseif (irevcm == 1)
% v = Au
[v, ifail] = f11xa(...
'N', a(1:nz), irow(1:nz), icol(1:nz), 'N', u);
elseif (irevcm == 2)
% Solve ILU v = u
[v, ifail] = f11db('N', a, irow, icol, ipivp, ipivq, istr, idiag, 'N', u);
if (ifail ~= 0)
irevcm = 6;
end
elseif (irevcm == 3)
% Monitoring stage
[itn, stplhs, stprhs, anorm, sigmax, ifail] = ...
f11bf(work);
fprintf('\nMonitoring at iteration number %2d\n',itn);
fprintf('residual norm:              %14.4e\n', stplhs);
fprintf('\n   Solution Vector  Residual Vector\n');
for i = 1:n
fprintf('%16.4f %16.2e\n', u(i), v(i));
end
end
end

% Get information about the computation
[itn, stplhs, stprhs, anorm, sigmax, ifail] = ...
f11bf(work);

fprintf('\nNumber of iterations for convergence:     %4d\n', itn);
fprintf('Residual norm:                           %14.4e\n', stplhs);
fprintf('Right-hand side of termination criteria: %14.4e\n', stprhs);
fprintf('i-norm of matrix a:                      %14.4e\n', anorm);
fprintf('\n   Solution Vector  Residual Vector\n');
for i = 1:n
fprintf('%16.4f %16.2e\n', u(i), v(i));
end

```
```f11be example results

Monitoring at iteration number  1
residual norm:                  1.4059e+02

Solution Vector  Residual Vector
-4.5858         1.53e+01
1.0154         2.66e+01
-2.2234        -8.75e+00
6.0097         1.86e+01
1.3827         8.28e+00
-7.9070         2.04e+01
0.4427         9.61e+00
5.9248         3.31e+01

Monitoring at iteration number  2
residual norm:                  3.2742e+01

Solution Vector  Residual Vector
4.1642        -2.96e+00
4.9370        -5.55e+00
4.8101         8.21e-01
5.4324        -1.68e+01
5.8531         5.60e-01
11.9250        -1.91e+00
8.4826         1.01e+00
6.0625        -3.10e+00

Number of iterations for convergence:        3
Residual norm:                               1.0373e-08
Right-hand side of termination criteria:     5.8900e-06
i-norm of matrix a:                          1.1000e+01

Solution Vector  Residual Vector
1.0000        -1.36e-09
2.0000        -2.61e-09
3.0000         2.25e-10
4.0000        -3.22e-09
5.0000         6.30e-10
6.0000        -5.24e-10
7.0000         9.58e-10
8.0000        -8.49e-10
```