MatrixFunction[f,m]
gives the matrix generated by the scalar function f at the matrix argument m.
MatrixFunction
MatrixFunction[f,m]
gives the matrix generated by the scalar function f at the matrix argument m.
Details and Options
- A matrix function transforms a matrix to another matrix. For convergent power series, MatrixFunction[f,m] effectively evaluates the power series for the function f with ordinary powers replaced by matrix powers. »
- The function f should be a unary differentiable or symbolic function.
- MatrixFunction works only on square matrices.
- MatrixFunction can be used on SparseArray objects and structured arrays.
- The following option can be given:
-
Method Automatic method to use - Possible values for the option Method include:
-
Automatic automatic selection "Jordan" Jordan decomposition "Schur" Schur decomposition with block Parlett recursion "SpectralBasis" spectral method for exact matrices » - The default setting of Method->Automatic uses the Schur decomposition for inexact numerical matrices and, based on matrix contents, selects between the Jordan decomposition and the spectral basis method for exact and symbolic matrices.
- The "Schur" method can be specified with method options mopts by Method->{"Schur",mopts}. The following method options can be given:
-
"Balanced" False whether to balance the input matrix before doing the Schur decomposition "BlockSeparationDelta" Automatic maximum separation between adjacent eigenvalues in a single Parlett block
Examples
open all close allBasic Examples (3)
Compute the square root of a symbolic matrix:
MatrixFunction[Sqrt, {{a, 1}, {0, b}}]Verify that it is, indeed, a square root:
%.%//SimplifyNote that this is different from simply computing the square root of each entry:
Sqrt[{{a, 1}, {0, b}}]Compute the logarithm of a 3×3 matrix m:
m = {{2., 2., 1.}, {4., 6., 4.}, {3., 6., 8.}};
MatrixFunction[Log, m]Verify that the exponential of the result is the original matrix:
MatrixExp[%] == mCompute a matrix polynomial, specifying the polynomial as a pure function:
MatrixFunction[x |-> x^5 + 2x^2 + 1, {{a, 0}, {1, b}}]Scope (11)
Basic Uses (7)
Compute the matrix sine and cosine of a machine-precision matrix:
m = {{1., 1., 0.}, {0., 1., 0.}, {0., 0., 2.}};cosm = MatrixFunction[Cos, m]sinm = MatrixFunction[Sin, m]sinm.sinm + cosm.cosm == IdentityMatrix[3]Compute the matrix sine for a complex matrix:
MatrixFunction[Sin, {{1., 2 + I}, {3 + I, 1}}]//MatrixFormCompute the square root of an exact matrix:
MatrixFunction[Sqrt, {{(5 π/18), (2 π/9)}, {(4 π/9), (π/18)}}]//SimplifyUse 20-digit-precision arithmetic to compute the matrix function of a logarithm of a polynomial:
m = N[{{1, 1 / 2, 1 / 3, 1 / 4}, {0, 1, 1 / 3, 1 / 4}, {0, 0, 1, 1 / 4}, {0, 0, 0, 1}}, 20];MatrixFunction[Log[1 + 2# + 3#^2]&, m]Compute the matrix sine of a symbolic diagonal matrix:
MatrixFunction[Sin, {{a, 0}, {0, b}}]Compute the matrix tangent of a symbolic non-diagonal matrix:
MatrixFunction[Tan, {{a, 0}, {1, b}}]Compute a matrix function with a symbolic scalar function:
MatrixFunction[f, {{1, 2}, {0, 1}}]//SimplifyUse a symbolic matrix with a symbolic scalar function:
MatrixFunction[f, {{0, a}, {b, 0}}]//SimplifyApplying a function to a machine-precision matrix is efficient:
m = RandomReal[{0, 1}, {500, 500}];MatrixFunction[Log, m]; //AbsoluteTimingSpecial Matrices (4)
Computing a matrix function with a sparse matrix generally produces a normal matrix:
SparseArray[{{i_, i_} -> -2., {i_, j_} /; Abs[i - j] == 1 -> 1.}, {6, 6}]MatrixFunction[Exp[-#^2]&, %]//MatrixFormCompute a matrix polynomial of a structured array:
SymmetrizedArray[{{1, 1} -> 3.5, {2, 2} -> 1.2, {3, 1} -> -5.3}, {3, 3}, Symmetric[All]]MatrixFunction[#^3 + 2 #^1 + 3 #&, %]//MatrixFormApplying a matrix function to an identity matrix only changes the value of the diagonal elements:
MatrixFunction[Exp, IdentityMatrix[3]]//MatrixFormMatrixFunction[Log, IdentityMatrix[3]]//MatrixFormMore generally, a function of any diagonal matrix is the function applied to its diagonal elements:
MatrixFunction[Log, DiagonalMatrix[{a, b, c}]]Compute the square of HilbertMatrix to 20-digit precision:
N[MatrixFunction[Sqrt, HilbertMatrix[2]], 20]//MatrixFormOptions (5)
Method (5)
The method "Jordan" can work with exact and inexact matrices:
MatrixFunction[Sin[# π]&, {{1 / 2, 1 / 3}, {1 / 3, 1 / 4}}, Method -> "Jordan"]//SimplifyMatrixFunction[Sin[# π]&, {{1. / 2., 1. / 3.}, {1. / 3., 1. / 4.}}, Method -> "Jordan"]The method "Schur" works only with inexact (machine- and arbitrary-precision) matrices:
MatrixFunction[Sin[# π]&, {{1 / 2, 1 / 3}, {1 / 3, 1 / 4}}, Method -> "Schur"]MatrixFunction[Sin[# π]&, {{1. / 2., 1. / 3.}, {1. / 3., 1. / 4.}}, Method -> "Schur"]If many eigenvalues are very close, they are put in a diagonal-block submatrix, which may become large; computations are then more expensive and convergence may be slow:
m = (| | | |
| ------ | ------ | ------ |
| 0.0003 | 100. | 1. |
| 0 | 0.0002 | 100. |
| 0 | 0 | 0.0001 |);
f = (1 - #) / Sqrt[1 - #^2]&;AbsoluteTiming[m1 = MatrixFunction[f, m]]A smaller value for "BlockSeparationDelta" reduces the diagonal block size and also speeds convergence:
AbsoluteTiming[m2 = MatrixFunction[f, m, Method -> {"Schur", "BlockSeparationDelta" -> 10^-6}]]But the result may be much less accurate:
mfinv = Function[Module[{id = IdentityMatrix[Length[#]]}, Inverse[id + #.#].(id - #.#)]];
{Norm[mfinv[m1] - m], Norm[mfinv[m2] - m]}When an input matrix is poorly balanced (containing terms of very different magnitudes), balancing may improve the result:
m = {{1000., 0, 0, 0, 0.3, 0, -1000}, {0, 0.1, 0, 0.2, 0.4, -1000, 3000}, {0, 0, 2000., 0, 0.3, 0.2, -1000}, {0, 0, 0, 1., 0, -1000, 0}, {0, 1000, 0, 0, 0.3, 0.1, 0}, {0, 0, 0, 0, 0, 0.2, -0.3}, {100, 0, 0, 0, 0, 0, 1.}};
m0 = MatrixFunction[Sqrt, m, Method -> {"Schur", "Balanced" -> True}];
m1 = MatrixFunction[Sqrt, m, Method -> "Schur"];
{Norm[MatrixPower[m0, 2] - m], Norm[MatrixPower[m1, 2] - m]}When it is applicable, "SpectralBasis" can be orders of magnitude faster than "Jordan":
mat = {{0, 0, 0, -2, -2, 0, 2, -1}, {0, -2, 4, 2, -2, 2, 5, 2}, {4, 0, 0, 2, 4, -3, -2, 6}, {-2, -2, 2, -2, -1, -4, 0, 2}, {0, -2, 2, -1, 0, 2, -2, -2}, {0, -4, 1, 2, -2, 2, -4, 0}, {0, 1, 0, 0, 2, 0, -4, 0}, {-1, 0, -2, 0, 0, 0, -4, -2}};
AbsoluteTiming[mexp1 = MatrixFunction[Exp, mat, Method -> "SpectralBasis"];]AbsoluteTiming[mexp2 = MatrixFunction[Exp, mat, Method -> "Jordan"];]It can also produce much simpler results:
AbsoluteTiming[LeafCount[mexp1]]AbsoluteTiming[LeafCount[mexp2]]The two results are equal, however:
AbsoluteTiming[Chop[N[mexp1 - mexp2]]]The method is chosen automatically for matrices of exact numbers, where it excels:
MatrixQ[mat, NumberQ] && Precision[mat] == Infinity && mexp1 === MatrixFunction[Exp, mat]Applications (5)
Find the second inverse matrix power applied to a particular vector:
m = RandomReal[{1, 2}, {10, 10}];
b = ConstantArray[1., 10];MatrixFunction[#^-2&, m].bThis is a more efficient way of computing
:
minv = LinearSolve[m];
Nest[minv, b, 2]Show that a matrix is a root of its characteristic polynomial:
m = {{0, 0, 0, 0, 1}, {0, 0, 0, 1, 0}, {0, 0, 1, 0, 0}, {0, 1, 0, 0, 0}, {1, 0, 0, 0, 0}};p = CharacteristicPolynomial[m, #]MatrixFunction[Evaluate[p]&, m] == ConstantArray[0, Dimensions[m]]The solution of
,
,
for a scalar symbol
is given with:
DSolve[{D[y[t], {t, 2}] + A y[t] == 0, y[0] == y0[0], y'[0] == y0'[0]}, y, t]If
is a matrix, the solution can be computed using matrix functions in the scalar solution:
A = -{{0, 1, 0}, {1 / 3, 1, 0}, {1, 0, 1}}//N;
y0[t_] := {t, Sqrt[1 + t], 1 + t}sqA = MatrixFunction[Sqrt, A];
y[t_ ? NumericQ] := MatrixFunction[Cos, t sqA].y0[0] + Inverse[sqA].MatrixFunction[Sin, t sqA].y0'[0];Plot[y[t], {t, 0, 1}]Find the matrix that satisfies
:
m = RandomReal[{0, 1}, {6, 6}];f[x_ ? MatrixQ] := MatrixFunction[# + Sin[#]&, x];sol = FindRoot[f[x] == m, {x, IdentityMatrix[Length[m]]}];
MatrixForm[x /. sol]f[x /. sol] - m//ChopConfirm the formula
for a Jordan matrix consisting of a single chain for the following matrix
:
MatrixForm[j = λ IdentityMatrix[5] + DiagonalMatrix[{1, 1, 1, 1}, 1, 5]]MatrixFunction[f, j]//MatrixFormProperties & Relations (11)
Using the function 1& returns an identity matrix:
MatrixFunction[1&, {{1, 2}, {3, 4}}]//SimplifyUsing the function 1/#& is the same as using Inverse:
MatrixFunction[1 / #&, {{1, 1}, {1, 2}}] == Inverse[{{1, 1}, {1, 2}}]Using a function that is a power is equivalent to using MatrixPower:
MatrixFunction[#^n&, {{0, 0, 1}, {0, 2, 0}, {-1, 0, 0}}] == MatrixPower[{{0, 0, 1}, {0, 2, 0}, {-1, 0, 0}}, n]MatrixFunction[Exp,m] is essentially equivalent to MatrixExp[m]:
m = RandomReal[1, {3, 3}];
MatrixFunction[Exp, m] == MatrixExp[m]MatrixFunction effectively uses the power series, with Power replaced by MatrixPower:
a = {{1, -(1/6)}, {1, (1/6)}};MatrixFunction[Sqrt, a] == Sum[(Derivative[k][Sqrt][1]/k!) MatrixPower[a - IdentityMatrix[2], k], {k, 0, ∞}]MatrixFunction[Log, a] == Sum[(Derivative[k][Log][1]/k!) MatrixPower[a - IdentityMatrix[2], k], {k, 0, ∞}]Just as
, MatrixExp[MatrixFunction[Log,m]] equals m:
m = {{0, 0, 1}, {0, 2, 0}, {-1, 0, 0}};
m == MatrixExp[MatrixFunction[Log, m]]If m is diagonal, MatrixFunction[f,m] merely applies f to each element of the diagonal of m:
MatrixFunction[Cosh, DiagonalMatrix[{a, b, c}]]//MatrixFormIf m is upper triangular, MatrixFunction[f,m] is also upper triangular:
m = UpperTriangularize[RandomInteger[9, {5, 5}]];
UpperTriangularMatrixQ[MatrixFunction[Tanh, m]]The analogous statement holds for lower-triangular matrices:
m = LowerTriangularize[RandomInteger[9, {5, 5}]];
LowerTriangularMatrixQ[MatrixFunction[Tanh, m]]If
is diagonalizable with
and the eigenvectors are well conditioned, then
:
m = RandomReal[1, {3, 3}];{d, vt} = Eigensystem[m]v = Transpose[vt];
v.DiagonalMatrix[Sin[Cos[d]]].Inverse[v] - MatrixFunction[Sin[Cos[#]]&, m]//Chop
can be computed from the JordanDecomposition
as
:
m = {{2, 0, 0, 0}, {0, 1, 0, 0}, {1, -1, 1, 0}, {1, -1, 1, 1}};
{v, j} = JordanDecomposition[m];
MatrixFunction[Sqrt, m] == v.MatrixFunction[Sqrt, j].Inverse[v]Moreover,
is zero except in upper-triangular blocks delineated by
s in the superdiagonal:
MatrixForm /@ {j, MatrixFunction[Sqrt, j]}A matrix is a root of its characteristic polynomial:
m = RandomInteger[2, {3, 3}]polyf = Function[Evaluate[CharacteristicPolynomial[m, #]]]MatrixFunction[polyf, m]//SimplifyPossible Issues (4)
The scalar function can have symbolic derivatives for exact or symbolic matrices:
F[x_ ? NumericQ] := Sqrt[x];
MatrixFunction[F, {{1, 1, 0}, {0, 1, 1}, {0, 0, 1}}]MatrixFunction[F, {{1, 1, 0}, {0, 1, 1}, {0, 0, 1}}//N]F[x_] := Sqrt[x];
MatrixFunction[F, {{1, 1, 0}, {0, 1, 1}, {0, 0, 1}}]MatrixFunction does not work with non-differentiable functions such as Abs:
MatrixFunction[Abs, {{1., 1., 0.}, {0., 1., 1.}, {0., 0., 1.}}]It will return a matrix function for an exact input matrix, but the result is meaningless because Abs does not have a first or second derivative:
MatrixFunction[Abs, {{1, 1, 0}, {0, 1, 1}, {0, 0, 1}}]MatrixFunction does not return a result when the scalar function or any of its initial derivatives are not defined at matrix eigenvalues:
MatrixFunction[Sqrt[1 - #]&, {{1, 1, 0}, {0, 1, 1}, {0, 0, 1}}]MatrixFunction[Sqrt[1 - #]&, {{1., 1., 0.}, {0., 1., 1.}, {0., 0., 1.}}]The scalar function f has poles (singularities) at 1, 2, and 3:
f = 1 / Apply[Times, (# - Range[3])]&;If a scalar function is not analytic and a matrix eigenvalue is close to a function pole, the resulting matrix is usually incorrect:
m = {{-2.131558166676987, -2.937833681080357, -0.995639204939471}, {4.991469442950794, 5.743899123162778, 1.4428901292047926}, {1.621158931412957, 1.1503162105039664, 2.387659043514208}};
Eigenvalues[m]mf = MatrixFunction[f, m]Neat Examples (1)
See Also
MatrixExp MatrixLog MatrixPower Inverse JordanDecomposition Eigensystem SchurDecomposition
Function Repository: MatrixPolynomial NEigenvalueSumGradient
Tech Notes
Related Guides
Text
Wolfram Research (2012), MatrixFunction, Wolfram Language function, https://reference.wolfram.com/language/ref/MatrixFunction.html (updated 2026).
CMS
Wolfram Language. 2012. "MatrixFunction." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2026. https://reference.wolfram.com/language/ref/MatrixFunction.html.
APA
Wolfram Language. (2012). MatrixFunction. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/MatrixFunction.html
BibTeX
@misc{reference.wolfram_2026_matrixfunction, author="Wolfram Research", title="{MatrixFunction}", year="2026", howpublished="\url{https://reference.wolfram.com/language/ref/MatrixFunction.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_matrixfunction, organization={Wolfram Research}, title={MatrixFunction}, year={2026}, url={https://reference.wolfram.com/language/ref/MatrixFunction.html}, note=[Accessed: 13-June-2026]}