Since Bob's answer has already been accepted, I will just leave a more extensive note on how to check for consistency. This is mostly a rehash of my other answers, so please refer to them for further details.
The starting point, as always, is to recall the (Cauchy integral-like) definition
$$f(\mathbf A) = \frac{1}{2\pi i} \oint_\gamma f(z)\, (z \mathbf I- \mathbf A)^{-1}\,\mathrm dz$$
where $\gamma$ is a closed contour enclosing the eigenvalues of $\mathbf A$, and where $f(z)$ is analytic within the contour.
First, this definition can be used to prove Daniel's identity in the comments, by letting $f(z)=\exp(i z)=\cos z+i\sin z$ and then decomposing accordingly.
Thus,
mat = {{1., 1., 3.}, {0., 1., 0.}, {0., 1.0 I, 2.}};
MatrixFunction[Cos, mat] // Chop
{{0.5403023058681398, -0.8414709848078964 - 0.34493447282215695 I, -2.869347427245846},
{0, 0.5403023058681398, 0},
{0, 0. - 0.9564491424152821 I, -0.41614683654714246}}
(MatrixExp[I mat] + MatrixExp[-I mat])/2 // Chop
{{0.5403023058681397, -0.8414709848078965 - 0.34493447282215695 I, -2.869347427245847},
{0, 0.5403023058681397, 0},
{0, 0. - 0.9564491424152821 I, -0.41614683654714235}}
Of course, the contour integral formula itself can be used for computation. I will temporarily consider the exact version of the OP's matrix for this demonstration:
mex = {{1, 1, 3}, {0, 1, 0}, {0, I, 2}};
eig = Eigenvalues[mex]
{2, 1, 1}
At this juncture, we note that all the eigenvalues are real.
We can then employ the residue theorem and the Cauchy integral theorem to convert the evaluation to a sum of residues of the integrand over the eigenvalues of the given matrix:
Sum[Map[Residue[#, {z, λ}] &,
Cos[z] Inverse[z IdentityMatrix[Length[mex]] - mex], {2}],
{λ, Union[eig]}]
{{Cos[1], 3 I Cos[2] - I (3 Cos[1] - (3 + I) Sin[1]), -3 Cos[1] + 3 Cos[2]},
{0, Cos[1], 0}, {0, -I Cos[1] + I Cos[2], Cos[2]}}
N[%]
{{0.5403023058681398, -0.8414709848078965 - 0.3449344728221573 I, -2.8693474272458466},
{0., 0.5403023058681398, 0.},
{0., 0. - 0.9564491424152821 I, -0.4161468365471424}}
Compare this with the more popular evaluation method that uses the Jordan decomposition:
{sm, jm} = JordanDecomposition[mex]
{{{1, 0, 3}, {0, 1/10 + 3 I/10, 0}, {0, 3/10 - I/10, 1}},
{{1, 1, 0}, {0, 1, 0}, {0, 0, 2}}}
sm.{{Cos[1], Cos'[1], 0}, {0, Cos[1], 0}, {0, 0, Cos[2]}}.Inverse[sm]
{{Cos[1], -3 I Cos[1] + 3 I Cos[2] - (1 - 3 I) Sin[1], -3 Cos[1] + 3 Cos[2]},
{0, Cos[1], 0}, {0, -I Cos[1] + I Cos[2], Cos[2]}}
N[%]
{{0.5403023058681398, -0.8414709848078965 - 0.3449344728221573 I, -2.8693474272458466},
{0., 0.5403023058681398, 0.},
{0., 0. - 0.9564491424152821 I, -0.4161468365471424}}
The contour integral formula also readily lends itself to numerical evaluation. Earlier, we noted that the eigenvalues of mat
are real, so a convenient choice for the contour $\gamma$ is an axis-aligned rectangle enclosing the eigenvalues:
With[{ε = 1/20},
contour = (Tuples[{MinMax[Eigenvalues[mat]] + {-ε, ε},
{-ε, ε}}].{1, I})[[{1, 3, 4, 2, 1}]]];
NIntegrate[]
can then be used for the evaluation:
NIntegrate[Cos[z] Inverse[z IdentityMatrix[3] - mat],
{z, Sequence @@ contour} // Evaluate]/(2 π I) // Chop
{{0.5403023058681499, -0.8414709848079495 - 0.3449344728223653 I, -2.8693474272458817},
{0, 0.5403023058681499, 0},
{0, 0. - 0.9564491424152939*I, -0.41614683654714557}}
This evaluation will throw a few NIntegrate::izero
, because some of the matrix elements are zero. Nevertheless, the result is consistent with all the other methods previously presented.
m
, i.e.,m={{1,1,3},...}
, or usem=Rationalize@m
Or seeChop
$\endgroup$Chop
command does not help at all. BTW, this is a slightly modified example from Mathematica documentation and I follow that documentation. $\endgroup$cosm = MatrixFunction[Cos, m] // Chop
orm = Rationalize@{{1., 1., 3.}, {0., 1., 0.}, {0., 1.0*I, 2.}}; cosm = MatrixFunction[Cos, m] // N
work $\endgroup$MatrixFunction[Cos, m]+I*MatrixFunction[Sin, m]
agrees withMatrixExp[I*m]
so there is some consistency. AndMatrixExp
is numerically a fairly reliable function, to the extent that it can be at least. $\endgroup$