# NAG Library Routine Document

## 1Purpose

f04aef calculates the accurate solution of a set of real linear equations with multiple right-hand sides using an $LU$ factorization with partial pivoting, and iterative refinement.

## 2Specification

Fortran Interface
 Subroutine f04aef ( a, lda, b, ldb, n, m, c, ldc, aa, ldaa, bb, ldbb,
 Integer, Intent (In) :: lda, ldb, n, m, ldc, ldaa, ldbb Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: a(lda,*), b(ldb,*) Real (Kind=nag_wp), Intent (Inout) :: c(ldc,*), aa(ldaa,*), bb(ldbb,*) Real (Kind=nag_wp), Intent (Out) :: wkspce(max(1,n))
#include <nagmk26.h>
 void f04aef_ (const double a[], const Integer *lda, const double b[], const Integer *ldb, const Integer *n, const Integer *m, double c[], const Integer *ldc, double wkspce[], double aa[], const Integer *ldaa, double bb[], const Integer *ldbb, Integer *ifail)

## 3Description

Given a set of real linear equations $AX=B$, the routine first computes an $LU$ factorization of $A$ with partial pivoting, $PA=LU$, where $P$ is a permutation matrix, $L$ is lower triangular and $U$ is unit upper triangular. An approximation to $X$ is found by forward and backward substitution. The residual matrix $R=B-AX$ is then calculated using additional precision, and a correction $D$ to $X$ is found by solving $LUD=PR$. $X$ is replaced by $X+D$ and this iterative refinement of the solution is repeated until full machine accuracy has been obtained.

## 4References

Wilkinson J H and Reinsch C (1971) Handbook for Automatic Computation II, Linear Algebra Springer–Verlag

## 5Arguments

1:     $\mathbf{a}\left({\mathbf{lda}},*\right)$ – Real (Kind=nag_wp) arrayInput
Note: the second dimension of the array a must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
On entry: the $n$ by $n$ matrix $A$.
2:     $\mathbf{lda}$ – IntegerInput
On entry: the first dimension of the array a as declared in the (sub)program from which f04aef is called.
Constraint: ${\mathbf{lda}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
3:     $\mathbf{b}\left({\mathbf{ldb}},*\right)$ – Real (Kind=nag_wp) arrayInput
Note: the second dimension of the array b must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}\right)$.
On entry: the $n$ by $m$ right-hand side matrix $B$.
4:     $\mathbf{ldb}$ – IntegerInput
On entry: the first dimension of the array b as declared in the (sub)program from which f04aef is called.
Constraint: ${\mathbf{ldb}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
5:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the order of the matrix $A$.
Constraint: ${\mathbf{n}}\ge 0$.
6:     $\mathbf{m}$ – IntegerInput
On entry: $m$, the number of right-hand sides.
Constraint: ${\mathbf{m}}\ge 0$.
7:     $\mathbf{c}\left({\mathbf{ldc}},*\right)$ – Real (Kind=nag_wp) arrayOutput
Note: the second dimension of the array c must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}\right)$.
On exit: the $n$ by $m$ solution matrix $X$.
8:     $\mathbf{ldc}$ – IntegerInput
On entry: the first dimension of the array c as declared in the (sub)program from which f04aef is called.
Constraint: ${\mathbf{ldc}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
9:     $\mathbf{wkspce}\left(\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)\right)$ – Real (Kind=nag_wp) arrayWorkspace
10:   $\mathbf{aa}\left({\mathbf{ldaa}},*\right)$ – Real (Kind=nag_wp) arrayOutput
Note: the second dimension of the array aa must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
On exit: the triangular factors $L$ and $U$, except that the unit diagonal elements of $U$ are not stored.
11:   $\mathbf{ldaa}$ – IntegerInput
On entry: the first dimension of the array aa as declared in the (sub)program from which f04aef is called.
Constraint: ${\mathbf{ldaa}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
12:   $\mathbf{bb}\left({\mathbf{ldbb}},*\right)$ – Real (Kind=nag_wp) arrayOutput
Note: the second dimension of the array bb must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}\right)$.
On exit: the final $n$ by $m$ residual matrix $R=B-AX$.
13:   $\mathbf{ldbb}$ – IntegerInput
On entry: the first dimension of the array bb as declared in the (sub)program from which f04aef is called.
Constraint: ${\mathbf{ldbb}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
14:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value  is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value  is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
Matrix $A$ is approximately singular.
${\mathbf{ifail}}=2$
The matrix $A$ is too ill-conditioned.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{ldaa}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldaa}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
On entry, ${\mathbf{lda}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lda}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
On entry, ${\mathbf{ldb}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldb}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
On entry, ${\mathbf{ldbb}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldbb}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
On entry, ${\mathbf{ldc}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldc}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 0$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

The computed solutions should be correct to full machine accuracy. For a detailed error analysis see page 107 of Wilkinson and Reinsch (1971).

## 8Parallelism and Performance

f04aef is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
f04aef makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

The time taken by f04aef is approximately proportional to ${n}^{3}$.
If there is only one right-hand side, it is simpler to use f04atf.

## 10Example

This example solves the set of linear equations $AX=B$ where
 $A= 33 16 72 -24 -10 -57 -8 -4 -17 and B= -359 281 85 .$

### 10.1Program Text

Program Text (f04aefe.f90)

### 10.2Program Data

Program Data (f04aefe.d)

### 10.3Program Results

Program Results (f04aefe.r)