Skip to main content

matrix - Why do ReplaceAll and With give different results?


I expected both results to be $0_3$:


P = RandomReal[1, {3, 3}];

A = MatrixFunction[Sin, t*P] /. t -> 0
B = With[{t = 0}, MatrixFunction[Sin, t*P]]

(* {{-0.362821 - 1.25562 I, 0.288053 + 1.1563 I, 0.107869 + 0.0658492 I},
{0.133223 - 1.20752 I, -0.220254 + 1.22158 I, 0.181178 - 0.148004 I},
{-0.0991967 + 0.526917 I, -0.208297 - 0.517217 I, 0.583075 + 0.0340421 }} *)

(* {{0., 0., 0.}, {0., 0., 0.}, {0., 0., 0.}} *)

Following J.M. comments:



P = RandomReal[1, {3, 3}, MachinePrecision -> 20] (* => A == B *)
P = RandomReal[1, {3, 3}, MachinePrecision -> 10] (* => A == B *)
P = RandomReal[1, {3, 3}, MachinePrecision -> $MachinePrecision] (* => A != B *)
a = $WorkingPrecision; P = RandomReal[1, {3, 3}, MachinePrecision -> a] (* => A == B *)

Also, using SetPrecision A is zero:


P = RandomReal[1, {3, 3}]
A = MatrixFunction[Sin, t*SetPrecision[P, $MachinePrecision]] /. t -> 0

So it's not a misunderstanding of mine, but a peculiar behaviour of MMA.




Answer



BUG FIXED IN V11.1.0, CONFIRMED IN EARLIER VERSIONS


This is really nothing to do with With and ReplaceAll. (In the case of With the substitution t=0 happens first, so the apparent bug is not triggered).


It looks as if Mathematica gives incorrect answers for


MatrixFunction[Sin, t * P]

in almost all cases where P is a machine precision square matrix of size 2 or larger, and t is unassigned. This does not appear to be a precision issue.


For example, define notionally equivalent matrices


P = {{1, 2}, {4, 3}}/4;


Aa = MatrixFunction[Sin, t P];
An = MatrixFunction[Sin, t N[P]];

Compare


Aa /. t -> 0
An /. t -> 0
(* {{0, 0}, {0, 0}} *)
(* {{0.421637, -0.210819}, {0.843274, -0.421637}} *)

Further, compare the notionally equivalent



MatrixFunction[Sin, N[P]]
Aa /. t -> 1.0
An /. t -> 1.0
(* {{0.151392, 0.398796}, {0.797592, 0.550188}} *)
(* {{0.151392, 0.398796}, {0.797592, 0.550188}} *)
(* {{1.08194, -0.0664758}, {0.265903, 0.816033 *)}}

This even occurs when P is real, symmetric, positive definite with integer coefficients.


UPDATED


This appears to be the result of Mathematica choosing an incorrect algorithm in the specific (and probably unusual) case where MatrixFunction is applied to the product of an unassigned variable and a machine precision matrix. The results given appear to be incorrect for all values t and occur in cases where the matrix is well behaved (the Schur and Jordan decompositions computed in machine precision agree closely with their exact values). Computing the results with any finite precision (not machine precision) does not suffer from this problem.



Comments