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_matop_real_gen_matrix_fun_num (f01el)

## Purpose

nag_matop_real_gen_matrix_fun_num (f01el) 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.

## Syntax

[a, user, iflag, imnorm, ifail] = f01el(a, f, 'n', n, 'user', user)
[a, user, iflag, imnorm, ifail] = nag_matop_real_gen_matrix_fun_num(a, f, 'n', n, 'user', user)

## Description

$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 function f which evaluates $f\left({z}_{i}\right)$ at a number of points ${z}_{i}$.

## References

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{a}\left(\mathit{lda},:\right)$ – double array
The first dimension of the array a must be at least ${\mathbf{n}}$.
The second dimension of the array a must be at least ${\mathbf{n}}$.
The $n$ by $n$ matrix $A$.
2:     $\mathrm{f}$ – function handle or string containing name of m-file
The function f evaluates $f\left({z}_{i}\right)$ at a number of points ${z}_{i}$.
[iflag, fz, user] = f(iflag, nz, z, user)

Input Parameters

1:     $\mathrm{iflag}$int64int32nag_int scalar
iflag will be zero.
2:     $\mathrm{nz}$int64int32nag_int scalar
${n}_{z}$, the number of function values required.
3:     $\mathrm{z}\left({\mathbf{nz}}\right)$ – complex array
The ${n}_{z}$ points ${z}_{1},{z}_{2},\dots ,{z}_{{n}_{z}}$ at which the function $f$ is to be evaluated.
4:     $\mathrm{user}$ – Any MATLAB object
f is called from nag_matop_real_gen_matrix_fun_num (f01el) with the object supplied to nag_matop_real_gen_matrix_fun_num (f01el).

Output Parameters

1:     $\mathrm{iflag}$int64int32nag_int scalar
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 nag_matop_real_gen_matrix_fun_num (f01el) will terminate the computation, with ${\mathbf{ifail}}={\mathbf{2}}$.
2:     $\mathrm{fz}\left({\mathbf{nz}}\right)$ – complex array
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)$.
3:     $\mathrm{user}$ – Any MATLAB object

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the first dimension of the array a.
$n$, the order of the matrix $A$.
Constraint: ${\mathbf{n}}\ge 0$.
2:     $\mathrm{user}$ – Any MATLAB object
user is not used by nag_matop_real_gen_matrix_fun_num (f01el), but is passed to f. Note that for large objects it may be more efficient to use a global variable which is accessible from the m-files than to use user.

### Output Parameters

1:     $\mathrm{a}\left(\mathit{lda},:\right)$ – double array
The first dimension of the array a will be ${\mathbf{n}}$.
The second dimension of the array a will be ${\mathbf{n}}$.
The $n$ by $n$ matrix, $f\left(A\right)$.
2:     $\mathrm{user}$ – Any MATLAB object
3:     $\mathrm{iflag}$int64int32nag_int scalar
${\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}}$.
4:     $\mathrm{imnorm}$ – double scalar
If $A$ has complex eigenvalues, nag_matop_real_gen_matrix_fun_num (f01el) 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, nag_matop_real_gen_matrix_fun_num (f01el) uses real arithmetic and ${\mathbf{imnorm}}=0$.
5:     $\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$
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 function has been called incorrectly.
${\mathbf{ifail}}=4$
The function was unable to compute the Schur decomposition of $A$.
Note:  this failure should not occur and suggests that the function has been called incorrectly.
${\mathbf{ifail}}=5$
${\mathbf{ifail}}=-1$
Input argument number $_$ is invalid.
${\mathbf{ifail}}=-3$
On entry, argument lda is invalid.
Constraint: $\mathit{lda}\ge {\mathbf{n}}$.
${\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

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.

The integer allocatable memory required is $n$. If $A$ has real eigenvalues then up to $6{n}^{2}$ of double 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 nag_matop_real_gen_matrix_fun_usd (f01em) can be used to evaluate $f\left(A\right)$ more accurately. If $A$ is real symmetric then it is recommended that nag_matop_real_symm_matrix_fun (f01ef) be used as it is more efficient and, in general, more accurate than nag_matop_real_gen_matrix_fun_num (f01el).
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 nag_matop_real_gen_matrix_cond_num (f01jb) should be used.
nag_matop_complex_gen_matrix_fun_num (f01fl) can be used to find the matrix function $f\left(A\right)$ for a complex matrix $A$.

## Example

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

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

a =  [3,  0,  1,  2;
-1,  1,  3,  1;
0,  2,  2,  1;
2,  1, -1,  1];

% Compute cos(2a)
[cos2a, user, iflag, imnorm, ifail] = ...
f01el(a, @f);

disp('f(A) = cos(2A)');
disp(cos2a);

fprintf('Imnorm = %6.2f\n',imnorm);

function [iflag, fz, user] = f(iflag, nz, z, user)
fz = complex(cos(2*z));
```
```f01el example results

f(A) = cos(2A)
-0.1704   -1.1597   -0.1878   -0.7307
-0.3950   -0.4410    0.7606    0.0655
-0.0950   -0.0717    0.0619   -0.4351
-0.1034    0.6424   -1.3964    0.1042

Imnorm =   0.00
```