# NAG Library Routine Document

## 1Purpose

f01elf computes the matrix function, $f\left(A\right)$, of a real $n$ by $n$ matrix $A$. Numerical differentiation is used to evaluate the derivatives of $f$ when they are required.

## 2Specification

Fortran Interface
 Subroutine f01elf ( n, a, lda, f,
 Integer, Intent (In) :: n, lda Integer, Intent (Inout) :: iuser(*), ifail Integer, Intent (Out) :: iflag Real (Kind=nag_wp), Intent (Inout) :: a(lda,*), ruser(*) Real (Kind=nag_wp), Intent (Out) :: imnorm External :: f
C Header Interface
#include <nagmk26.h>
 void f01elf_ (const Integer *n, double a[], const Integer *lda, void (NAG_CALL *f)(Integer *iflag, const Integer *nz, const Complex z[], Complex fz[], Integer iuser[], double ruser[]),Integer iuser[], double ruser[], Integer *iflag, double *imnorm, Integer *ifail)

## 3Description

$f\left(A\right)$ is computed using the Schur–Parlett algorithm described in Higham (2008) and Davies and Higham (2003). The coefficients of the Taylor series used in the algorithm are evaluated using the numerical differentiation algorithm of Lyness and Moler (1967).
The scalar function $f$ is supplied via subroutine f which evaluates $f\left({z}_{i}\right)$ at a number of points ${z}_{i}$.

## 4References

Davies P I and Higham N J (2003) A Schur–Parlett algorithm for computing matrix functions. SIAM J. Matrix Anal. Appl. 25(2) 464–485
Higham N J (2008) Functions of Matrices: Theory and Computation SIAM, Philadelphia, PA, USA
Lyness J N and Moler C B (1967) Numerical differentiation of analytic functions SIAM J. Numer. Anal. 4(2) 202–210

## 5Arguments

1:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the order of the matrix $A$.
Constraint: ${\mathbf{n}}\ge 0$.
2:     $\mathbf{a}\left({\mathbf{lda}},*\right)$ – Real (Kind=nag_wp) arrayInput/Output
Note: the second dimension of the array a must be at least ${\mathbf{n}}$.
On entry: the $n$ by $n$ matrix $A$.
On exit: the $n$ by $n$ matrix, $f\left(A\right)$.
3:     $\mathbf{lda}$ – IntegerInput
On entry: the first dimension of the array a as declared in the (sub)program from which f01elf is called.
Constraint: ${\mathbf{lda}}\ge {\mathbf{n}}$.
4:     $\mathbf{f}$ – Subroutine, supplied by the user.External Procedure
The subroutine f evaluates $f\left({z}_{i}\right)$ at a number of points ${z}_{i}$.
The specification of f is:
Fortran Interface
 Subroutine f ( nz, z, fz,
 Integer, Intent (In) :: nz Integer, Intent (Inout) :: iflag, iuser(*) Real (Kind=nag_wp), Intent (Inout) :: ruser(*) Complex (Kind=nag_wp), Intent (In) :: z(nz) Complex (Kind=nag_wp), Intent (Out) :: fz(nz)
C Header Interface
#include <nagmk26.h>
 void f (Integer *iflag, const Integer *nz, const Complex z[], Complex fz[], Integer iuser[], double ruser[])
1:     $\mathbf{iflag}$ – IntegerInput/Output
On entry: iflag will be zero.
On exit: iflag should either be unchanged from its entry value of zero, or may be set nonzero to indicate that there is a problem in evaluating the function $f\left({z}_{i}\right)$; for instance $f\left({z}_{i}\right)$ may not be defined. If iflag is returned as nonzero then f01elf will terminate the computation, with ${\mathbf{ifail}}={\mathbf{2}}$.
2:     $\mathbf{nz}$ – IntegerInput
On entry: ${n}_{z}$, the number of function values required.
3:     $\mathbf{z}\left({\mathbf{nz}}\right)$ – Complex (Kind=nag_wp) arrayInput
On entry: the ${n}_{z}$ points ${z}_{1},{z}_{2},\dots ,{z}_{{n}_{z}}$ at which the function $f$ is to be evaluated.
4:     $\mathbf{fz}\left({\mathbf{nz}}\right)$ – Complex (Kind=nag_wp) arrayOutput
On exit: the ${n}_{z}$ function values. ${\mathbf{fz}}\left(\mathit{i}\right)$ should return the value $f\left({z}_{\mathit{i}}\right)$, for $\mathit{i}=1,2,\dots ,{n}_{z}$. If ${z}_{i}$ lies on the real line, then so must $f\left({z}_{i}\right)$.
5:     $\mathbf{iuser}\left(*\right)$ – Integer arrayUser Workspace
6:     $\mathbf{ruser}\left(*\right)$ – Real (Kind=nag_wp) arrayUser Workspace
f is called with the arguments iuser and ruser as supplied to f01elf. You should use the arrays iuser and ruser to supply information to f.
f must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which f01elf is called. Arguments denoted as Input must not be changed by this procedure.
Note: f should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by f01elf. If your code inadvertently does return any NaNs or infinities, f01elf is likely to produce unexpected results.
5:     $\mathbf{iuser}\left(*\right)$ – Integer arrayUser Workspace
6:     $\mathbf{ruser}\left(*\right)$ – Real (Kind=nag_wp) arrayUser Workspace
iuser and ruser are not used by f01elf, but are passed directly to f and may be used to pass information to this routine.
7:     $\mathbf{iflag}$ – IntegerOutput
On exit: ${\mathbf{iflag}}=0$, unless iflag has been set nonzero inside f, in which case iflag will be the value set and ifail will be set to ${\mathbf{ifail}}={\mathbf{2}}$.
8:     $\mathbf{imnorm}$ – Real (Kind=nag_wp)Output
On exit: if $A$ has complex eigenvalues, f01elf will use complex arithmetic to compute $f\left(A\right)$. The imaginary part is discarded at the end of the computation, because it will theoretically vanish. imnorm contains the $1$-norm of the imaginary part, which should be used to check that the routine has given a reliable answer.
If a has real eigenvalues, f01elf uses real arithmetic and ${\mathbf{imnorm}}=0$.
9:     $\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$
A Taylor series failed to converge after $40$ terms. Further Taylor series coefficients can no longer reliably be obtained by numerical differentiation.
${\mathbf{ifail}}=2$
iflag has been set nonzero by the user.
${\mathbf{ifail}}=3$
There was an error whilst reordering the Schur form of $A$.
Note:  this failure should not occur and suggests that the routine has been called incorrectly.
${\mathbf{ifail}}=4$
The routine was unable to compute the Schur decomposition of $A$.
Note:  this failure should not occur and suggests that the routine has been called incorrectly.
${\mathbf{ifail}}=5$
An unexpected internal error occurred. Please contact NAG.
${\mathbf{ifail}}=-1$
Input argument number $〈\mathit{\text{value}}〉$ is invalid.
${\mathbf{ifail}}=-3$
On entry, argument lda is invalid.
Constraint: ${\mathbf{lda}}\ge {\mathbf{n}}$.
${\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

For a normal matrix $A$ (for which ${A}^{\mathrm{T}}A=A{A}^{\mathrm{T}}$) the Schur decomposition is diagonal and the algorithm reduces to evaluating $f$ at the eigenvalues of $A$ and then constructing $f\left(A\right)$ using the Schur vectors. See Section 9.4 of Higham (2008) for further discussion of the Schur–Parlett algorithm, and Lyness and Moler (1967) for a discussion of numerical differentiation.

## 8Parallelism and Performance

f01elf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library. In these implementations, this routine may make calls to the user-supplied functions from within an OpenMP parallel region. Thus OpenMP directives within the user functions can only be used if you are compiling the user-supplied function and linking the executable in accordance with the instructions in the Users' Note for your implementation. The user workspace arrays iuser and ruser are classified as OpenMP shared memory and use of iuser and ruser has to take account of this in order to preserve thread safety whenever information is written back to either of these arrays. If at all possible, it is recommended that these arrays are only used to supply read-only data to the user functions when a multithreaded implementation is being used.
f01elf 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.

## 9Further Comments

The integer allocatable memory required is $n$. If $A$ has real eigenvalues then up to $6{n}^{2}$ of real allocatable memory may be required. If $A$ has complex eigenvalues then up to $6{n}^{2}$ of complex allocatable memory may be required.
The cost of the Schur–Parlett algorithm depends on the spectrum of $A$, but is roughly between $28{n}^{3}$ and ${n}^{4}/3$ floating-point operations. There is an additional cost in numerically differentiating $f$, in order to obtain the Taylor series coefficients. If the derivatives of $f$ are known analytically, then f01emf can be used to evaluate $f\left(A\right)$ more accurately. If $A$ is real symmetric then it is recommended that f01eff be used as it is more efficient and, in general, more accurate than f01elf.
For any $z$ on the real line, $f\left(z\right)$ must be real. $f$ must also be complex analytic on the spectrum of $A$. These conditions ensure that $f\left(A\right)$ is real for real $A$.
For further information on matrix functions, see Higham (2008).
If estimates of the condition number of the matrix function are required then f01jbf should be used.
f01flf can be used to find the matrix function $f\left(A\right)$ for a complex matrix $A$.

## 10Example

This example finds $\mathrm{cos}2A$ where
 $A= 3 0 1 2 -1 1 3 1 0 2 2 1 2 1 -1 1 .$

### 10.1Program Text

Program Text (f01elfe.f90)

### 10.2Program Data

Program Data (f01elfe.d)

### 10.3Program Results

Program Results (f01elfe.r)