Skip to content

funm

linear-algebra · scripts/linear-algebra/funm.m

: F = funm (A, fun)
: F = funm (A, fun, options)
: F = funm (A, fun, options, p1, …)
: [F, exitflag] = funm (…)
: [F, exitflag, output] = funm (…)

Apply a scalar function to a square matrix.

The matrix function is defined through the Taylor series of fun, which must converge everywhere, and is computed with a Schur decomposition: the eigenvalues are grouped into clusters, the function is evaluated on each diagonal block of the reordered Schur form by a Taylor expansion, and the off-diagonal blocks are recovered from Sylvester equations.

fun may be a function handle or a function name. For a general function it is called as fun (x, k) and must return the k-th derivative of the function at every element of x. The handles @exp, @log, @sin, @cos, @sinh and @cosh are recognised and need no derivative argument, so funm (A, @cos) works as is.

sqrtm is the right tool for matrix square roots; for the matrix exponential expm is usually the better choice, although funm (A, @exp) is available.

options is a structure that controls the amount of diagnostic output and the tuning of the algorithm. Field names are matched without regard to case. The recognised fields are:

Display

How much to report while working. Either one of the strings "off" (the default), "on" or "verbose", or a number from 0 (silent) to 5 (everything). At "verbose", or a number of 3 or more, the eigenvalues and their clusters are also plotted.

TolBlk

Positive tolerance that decides when two eigenvalues belong to the same cluster. Default 0.1.

TolTay

Positive tolerance for stopping the Taylor expansion of a block. Default eps.

MaxTerms

Largest number of Taylor terms to use for one block. Default 250.

MaxSqrt

For the logarithm only: the largest number of square roots taken while reducing a block before its series is summed. Default 100.

Ord

A vector with one entry per row of A that fixes the clustering by hand instead of letting TolBlk decide.

Any further arguments p1, … are appended to every call of fun.

The second output exitflag is 0 when every Taylor expansion converged and 1 when at least one did not, in which case F may still be usable.

The third output output is a structure with these fields (MATLAB R2026a):

terms

The number of Taylor terms used for each block, or for the logarithm the number of square roots taken for each block of order greater than 2.

ind

Cell array of index vectors; block (i, j) of the reordered Schur factor is T(output.ind{i}, output.ind{j}).

When the Schur factor is already diagonal no reordering takes place: terms is all ones and ind lists each index on its own.

Example: the sine of a symmetric matrix agrees with its spectral decomposition.

A = [2, 1; 1, 2];             # eigenvalues 1 and 3
[V, D] = eig (A);
norm (funm (A, @sin) - V * diag (sin (diag (D))) / V)
  ⇒ ans is of the order of eps

Because exp (i*X) = cos (X) + i sin (X) holds for matrices too, real (expm (i*X)) and imag (expm (i*X)) reproduce funm (X, @cos) and funm (X, @sin) up to rounding.

The algorithm follows P. I. Davies and N. J. Higham, "A Schur-Parlett algorithm for computing matrix functions", SIAM J. Matrix Anal. Appl. 25(2), 2003, and chapter 9 of N. J. Higham, Functions of Matrices, SIAM 2008.