# Matrix exponential via Cayley-Hamilton Theorem

I'm attempting to calculate the exponential of a matrix via Cayley-Hamilton theorem. (Following the "concrete example" from http://en.wikipedia.org/wiki/Cayley%E2%80%93Hamilton_theorem)

I am having trouble manipulating the characteristic polynomial:

A = {{1, 2}, {3, 4}};
cp = CharacteristicPolynomial[A, x]
A = x^2 - cp
A = A /. {x -> A}


This is the form of the example. Now, I can't figure out a way to multiply only the +2 by the identity matrix, while substituting in x->A. The correct result should be

5 A + 2 *IdentityMatrix


which obviously does not match

A = A /. {x -> A}


As the "+2" is applied to all elements of A, not just the diagonals.

To expand on David's answer, you need to replace it with the correct function. It turns out the pattern to do this is a little tricky to get right, either that or I'm out of practice. So, I'll walk you through the process using f as the "correct" function.

Initially, I would propose attempting to match using a default exponent, n_.,

cp /.  x^n_. :>  f[x, n]
(* -2 - 5 f[x, 1] + f[x, 2] *)


which as you see does not match the x^0 term. So, it seems we need to deal with that term directly,

cp /.  {x^(n_.)  :>  f[x, n], c_?NumericQ :> c f[x, 0]}
(* -2 f[x, 0] - 5 f[x, 0] f[x, 1] + f[x, 2] *)


which is over aggressive. To tone it down, we need to add a leading coefficient,

cp /.  {c_. x^(n_.)  :>  c f[x, n], c_?NumericQ :> c f[x, 0]}
(* -2 f[x, 0] - 5 f[x, 1] + f[x, 2] *)


which we also make optional so it will match x^2.

Now, applying this to your exact problem, we substitute f for the correct function: MatrixPower, e.g.

x^2 - cp /. {c_. x^(n_.) :> c MatrixPower[A, n],
c_?NumericQ :> c MatrixPower[A, 0]}
(* {{7, 10}, {15, 22}} *)


MatrixPower[A, 2]

• @gKirkland he's absolutely correct. The distinction is x^2 is interpreted as Power[x, 2], but if x is a matrix, then every element is squared. This helps speed up some types of calculations. – rcollyer May 15 '14 at 1:24