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_direct_real_gen_refine (f11mh)

## Purpose

nag_sparse_direct_real_gen_refine (f11mh) returns error bounds for the solution of a real sparse system of linear equations with multiple right-hand sides, AX = B$AX=B$ or ATX = B${A}^{\mathrm{T}}X=B$. It improves the solution by iterative refinement in standard precision, in order to reduce the backward error as much as possible.

## Syntax

[x, ferr, berr, ifail] = f11mh(trans, icolzp, irowix, a, iprm, il, lval, iu, uval, b, x, 'n', n, 'nrhs_p', nrhs_p)
[x, ferr, berr, ifail] = nag_sparse_direct_real_gen_refine(trans, icolzp, irowix, a, iprm, il, lval, iu, uval, b, x, 'n', n, 'nrhs_p', nrhs_p)

## Description

nag_sparse_direct_real_gen_refine (f11mh) returns the backward errors and estimated bounds on the forward errors for the solution of a real system of linear equations with multiple right-hand sides AX = B$AX=B$ or ATX = B${A}^{\mathrm{T}}X=B$. The function handles each right-hand side vector (stored as a column of the matrix B$B$) independently, so we describe the function of nag_sparse_direct_real_gen_refine (f11mh) in terms of a single right-hand side b$b$ and solution x$x$.
Given a computed solution x$x$, the function computes the component-wise backward error β$\beta$. This is the size of the smallest relative perturbation in each element of A$A$ and b$b$ such that if x$x$ is the exact solution of a perturbed system:
 (A + δA) x = b + δ b then   |δaij| ≤ β |aij|   and   |δbi| ≤ β |bi| .
$(A+δA) x = b + δ b then |δaij| ≤ β |aij| and |δbi| ≤ β |bi| .$
Then the function estimates a bound for the component-wise forward error in the computed solution, defined by:
 maxi |xi − x̂i| / maxi |xi| $maxi | xi - x^i | / maxi |xi|$
where $\stackrel{^}{x}$ is the true solution.
The function uses the LU $LU$ factorization Pr A Pc = LU ${P}_{r}A{P}_{c}=LU$ computed by nag_sparse_direct_real_gen_lu (f11me) and the solution computed by nag_sparse_direct_real_gen_solve (f11mf).

## References

Golub G H and Van Loan C F (1996) Matrix Computations (3rd Edition) Johns Hopkins University Press, Baltimore

## Parameters

### Compulsory Input Parameters

1:     trans – string (length ≥ 1)
Specifies whether AX = B$AX=B$ or ATX = B${A}^{\mathrm{T}}X=B$ is solved.
trans = 'N'${\mathbf{trans}}=\text{'N'}$
AX = B$AX=B$ is solved.
trans = 'T'${\mathbf{trans}}=\text{'T'}$
ATX = B${A}^{\mathrm{T}}X=B$ is solved.
Constraint: trans = 'N'${\mathbf{trans}}=\text{'N'}$ or 'T'$\text{'T'}$.
2:     icolzp( : $:$) – int64int32nag_int array
Note: the dimension of the array icolzp must be at least n + 1${\mathbf{n}}+1$.
icolzp(i)${\mathbf{icolzp}}\left(i\right)$ contains the index in A$A$ of the start of a new column. See Section [Compressed column storage (CCS) format] in the F11 Chapter Introduction.
3:     irowix( : $:$) – int64int32nag_int array
Note: the dimension of the array irowix must be at least icolzp(n + 1)1${\mathbf{icolzp}}\left({\mathbf{n}}+1\right)-1$, the number of nonzeros of the sparse matrix A$A$.
The row index array of sparse matrix A$A$.
4:     a( : $:$) – double array
Note: the dimension of the array a must be at least icolzp(n + 1)1${\mathbf{icolzp}}\left({\mathbf{n}}+1\right)-1$, the number of nonzeros of the sparse matrix A$A$.
The array of nonzero values in the sparse matrix A$A$.
5:     iprm(7 × n$7×{\mathbf{n}}$) – int64int32nag_int array
The column permutation which defines Pc${P}_{c}$, the row permutation which defines Pr${P}_{r}$, plus associated data structures as computed by nag_sparse_direct_real_gen_lu (f11me).
6:     il( : $:$) – int64int32nag_int array
Note: the dimension of the array il must be at least as large as the dimension of the array of the same name in nag_sparse_direct_real_gen_lu (f11me).
Records the sparsity pattern of matrix L$L$ as computed by nag_sparse_direct_real_gen_lu (f11me).
7:     lval( : $:$) – double array
Note: the dimension of the array lval must be at least as large as the dimension of the array of the same name in nag_sparse_direct_real_gen_lu (f11me).
Records the nonzero values of matrix L$L$ and some nonzero values of matrix U$U$ as computed by nag_sparse_direct_real_gen_lu (f11me).
8:     iu( : $:$) – int64int32nag_int array
Note: the dimension of the array iu must be at least as large as the dimension of the array of the same name in nag_sparse_direct_real_gen_lu (f11me).
Records the sparsity pattern of matrix U$U$ as computed by nag_sparse_direct_real_gen_lu (f11me).
9:     uval( : $:$) – double array
Note: the dimension of the array uval must be at least as large as the dimension of the array of the same name in nag_sparse_direct_real_gen_lu (f11me).
Records some nonzero values of matrix U$U$ as computed by nag_sparse_direct_real_gen_lu (f11me).
10:   b(ldb, : $:$) – double array
The first dimension of the array b must be at least max (1,n)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$
The second dimension of the array must be at least max (1,nrhs)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nrhs}}\right)$
The n$n$ by nrhs$\mathit{nrhs}$ right-hand side matrix B$B$.
11:   x(ldx, : $:$) – double array
The first dimension of the array x must be at least max (1,n)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$
The second dimension of the array must be at least max (1,nrhs)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nrhs}}\right)$
The n$n$ by nrhs$\mathit{nrhs}$ solution matrix X$X$, as returned by nag_sparse_direct_real_gen_solve (f11mf).

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The first dimension of the arrays b, x. (An error is raised if these dimensions are not equal.)
n$n$, the order of the matrix A$A$.
Constraint: n0${\mathbf{n}}\ge 0$.
2:     nrhs_p – int64int32nag_int scalar
Default: The second dimension of the arrays b, x.
nrhs$\mathit{nrhs}$, the number of right-hand sides in B$B$.
Constraint: nrhs0${\mathbf{nrhs}}\ge 0$.

ldb ldx

### Output Parameters

1:     x(ldx, : $:$) – double array
The first dimension of the array x will be max (1,n)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$
The second dimension of the array will be max (1,nrhs)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nrhs}}\right)$
ldxmax (1,n)$\mathit{ldx}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
The n$n$ by nrhs$\mathit{nrhs}$ improved solution matrix X$X$.
2:     ferr(nrhs_p) – double array
ferr(j)${\mathbf{ferr}}\left(\mathit{j}\right)$ contains an estimated error bound for the j$\mathit{j}$th solution vector, that is, the j$\mathit{j}$th column of X$X$, for j = 1,2,,nrhs$\mathit{j}=1,2,\dots ,\mathit{nrhs}$.
3:     berr(nrhs_p) – double array
berr(j)${\mathbf{berr}}\left(\mathit{j}\right)$ contains the component-wise backward error bound β$\beta$ for the j$\mathit{j}$th solution vector, that is, the j$\mathit{j}$th column of X$X$, for j = 1,2,,nrhs$\mathit{j}=1,2,\dots ,\mathit{nrhs}$.
4:     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, trans ≠ 'N'${\mathbf{trans}}\ne \text{'N'}$ or 'T'$\text{'T'}$, or n < 0${\mathbf{n}}<0$, or nrhs < 0${\mathbf{nrhs}}<0$, or ldb < max (1,n)$\mathit{ldb}<\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$, or ldx < max (1,n)$\mathit{ldx}<\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
ifail = 2${\mathbf{ifail}}=2$
Ill-defined row permutation in array iprm${\mathbf{iprm}}$. Internal checks have revealed that the iprm${\mathbf{iprm}}$ array is corrupted.
ifail = 3${\mathbf{ifail}}=3$
Ill-defined column permutations in array iprm${\mathbf{iprm}}$. Internal checks have revealed that the iprm${\mathbf{iprm}}$ array is corrupted.
ifail = 301${\mathbf{ifail}}=301$
Unable to allocate required internal workspace.

## Accuracy

The bounds returned in ferr are not rigorous, because they are estimated, not computed exactly; but in practice they almost always overestimate the actual error.

At most five steps of iterative refinement are performed, but usually only one or two steps are required.
Estimating the forward error involves solving a number of systems of linear equations of the form Ax = b$Ax=b$ or ATx = b${A}^{\mathrm{T}}x=b$;

## Example

```function nag_sparse_direct_real_gen_refine_example
n = int64(5);
nz = int64(11);
icolzp = [int64(1); 3; 5; 7; 9; 12];
irowix = [int64(1); 3; 1; 5; 2; 3; 2; 4; 3; 4; 5];
a = [2; 4; 1; -2; 1; 1; -1; 1; 1; 2; 3];
b = [  1.56,  3.12;
-0.25, -0.50;
3.60,  7.20;
1.33,  2.66;
0.52,  1.04];
x = b;
iprm = zeros(1, 7*n, 'int64');
spec = 'M';
thresh = 1;
nzlmx = int64(8*nz);
nzlumx = int64(8*nz);
nzumx = int64(8*nz);
trans = 'N';

% Calculate COLAMD permutation
[iprm, ifail] = nag_sparse_direct_real_gen_setup(spec, n, icolzp, irowix, iprm);

% Factorise
[iprm, nzlumx, il, lval, iu, uval, nnzl, nnzu, flop, ifail] = ...
nag_sparse_direct_real_gen_lu(n, irowix, a, iprm, thresh, nzlmx, nzlumx, nzumx);

% Compute solution in x
[x, ifail] = nag_sparse_direct_real_gen_solve(trans, iprm, il, lval, iu, uval, x);

% Improve solution, and compute backward errors and estimated
% bounds on the forward errors
[x, ferr, berr, ifail] = ...
nag_sparse_direct_real_gen_refine(trans, icolzp, irowix, a, iprm, il, lval, iu, uval, b, x);

fprintf('\nSolutions:\n');
disp(x);
fprintf('Estmated Forward Error:\n');
fprintf('%8.1e\n', ferr);
fprintf('\nEstmated Backward Error:\n');
fprintf('%8.1e\n', berr);
```
```

Solutions:
0.7000    1.4000
0.1600    0.3200
0.5200    1.0400
0.7700    1.5400
0.2800    0.5600

Estmated Forward Error:
5.0e-15
5.0e-15

Estmated Backward Error:
3.6e-17
3.6e-17

```
```function f11mh_example
n = int64(5);
nz = int64(11);
icolzp = [int64(1); 3; 5; 7; 9; 12];
irowix = [int64(1); 3; 1; 5; 2; 3; 2; 4; 3; 4; 5];
a = [2; 4; 1; -2; 1; 1; -1; 1; 1; 2; 3];
b = [  1.56,  3.12;
-0.25, -0.50;
3.60,  7.20;
1.33,  2.66;
0.52,  1.04];
x = b;
iprm = zeros(1, 7*n, 'int64');
spec = 'M';
thresh = 1;
nzlmx = int64(8*nz);
nzlumx = int64(8*nz);
nzumx = int64(8*nz);
trans = 'N';

% Calculate COLAMD permutation
[iprm, ifail] = f11md(spec, n, icolzp, irowix, iprm);

% Factorise
[iprm, nzlumx, il, lval, iu, uval, nnzl, nnzu, flop, ifail] = ...
f11me(n, irowix, a, iprm, thresh, nzlmx, nzlumx, nzumx);

% Compute solution in x
[x, ifail] = f11mf(trans, iprm, il, lval, iu, uval, x);

% Improve solution, and compute backward errors and estimated
% bounds on the forward errors
[x, ferr, berr, ifail] = ...
f11mh(trans, icolzp, irowix, a, iprm, il, lval, iu, uval, b, x);

fprintf('\nSolutions:\n');
disp(x);
fprintf('Estmated Forward Error:\n');
fprintf('%8.1e\n', ferr);
fprintf('\nEstmated Backward Error:\n');
fprintf('%8.1e\n', berr);
```
```

Solutions:
0.7000    1.4000
0.1600    0.3200
0.5200    1.0400
0.7700    1.5400
0.2800    0.5600

Estmated Forward Error:
5.0e-15
5.0e-15

Estmated Backward Error:
3.6e-17
3.6e-17

```