# Errors relating to MatrixFunction

I ran this and got some errors. Does anyone know how to solve this? First I want to caluclate A^2(A+I) with A = {{0, 1, 0}, {0, 0, 0}, {0, 0, -1}}.

MatrixFunction[#^2.(# + IdentityMatrix) &, {{0, 1, 0}, {0, 0,
0}, {0, 0, -1}}]


StringForm::sfr: Item 1 requested in "Cannot compute the matrix function for the function 1." out of range; 0 items available. MatrixFunction::nosol: Cannot compute the matrix function for the function 1.

Second, I want to calculate A2^k:

A2= {{-0.5, 3}, {0, -0.8}};
MatrixFunction[#^k &, A2] //
FullSimplify[#, k > 0 && k \[Element] Integers] & // MatrixForm


MatrixFunction::fnnum: Unable to compute the matrix function because the function #1^k& at a numeric value is not numeric.

• I'm not sure what you're trying to do.
– chuy
May 11, 2021 at 19:35
• You can't have a matrix in the pure function in the first example. Use MatrixPower instead for the second example. May 11, 2021 at 20:37
• @chuy I updated the post May 12, 2021 at 5:28
• @CarlWoll I want to make a pure function #^2(#+I) where I is the identiy matrix. So how can I make that? May 12, 2021 at 5:29

A = RandomReal[{0, 1}, {3, 3}];
MatrixFunction[#^2 (# + 1) &, A] == A . A . (A + IdentityMatrix)
(*    True    *)


You need to use the number 1, not the unit matrix, in the definition of the function for MatrixFunction. As the documentation states,

For convergent power series, MatrixFunction[f,m] effectively evaluates the power series for the function f with ordinary powers replaced by matrix powers.

For the second case, as @CarlWoll says, use MatrixPower:

A2 = {{-0.5, 3}, {0, -0.8}};
MatrixPower[A2, k]
(*    {{0. + 1. (-0.5)^k, -10. (-0.8)^k + 10. (-0.5)^k},
{0.,               0. + 1. (-0.8)^k}}                *)


For some reason, MatrixFunction[#^k &, A2] does not seem to work if k is unspecified, even though the exact calculation works without a problem:

A2 = {{-1/2, 3}, {0, -4/5}};
MatrixFunction[#^k &, A2]
(*    {{(-1/2)^k, 5 (-1)^k 2^(1 - k) + (-1)^(1 + k) 2^(1 + 2 k) 5^(1 - k)},
{0,        (-4/5)^k}}                                                   *)

• This would be complete if why  MatrixFunction[#^k &, A2] does not work is answered. May 14, 2021 at 4:30
• @anhnha see update May 14, 2021 at 7:30
• could you add the why the exact numbers make it work? May 14, 2021 at 7:41
• If I knew, I would have. May 14, 2021 at 8:14