0
$\begingroup$

I am multiplying three matrices that should give me a diagonal matrix but instead of zeros for the other entries I get very small numbers. Can someone explain what is going on here and how I can fix it? This is what what I've been doing:

P = {{.7, .15, .15}, {.2, .8, .15}, {.1, .05, .7}}
(* {{0.7, 0.15, 0.15}, {0.2, 0.8, 0.15}, {0.1, 0.05, 0.7}} *)

S = {{7, 0, -2}, {10, -1, 1}, {4, 1, 1}}
(* {{7, 0, -2}, {10, -1, 1}, {4, 1, 1}} *)

Inverse[S].P.S
(* {{1., -1.38778*10^-17, 0.}, {-6.66134*10^-16, 
  0.65, -5.55112*10^-17}, {1.66533*10^-16, -1.38778*10^-17, 0.55}} *)
$\endgroup$
  • 2
    $\begingroup$ Chop[%] gives {{1., 0, 0}, {0, 0.65, 0}, {0, 0, 0.55}} floating point calculation noise. $\endgroup$ – Nasser Mar 23 '14 at 6:10
4
$\begingroup$

For exact results, use exact input:

p = {{7/10, 15/100, 15/100}, {2/10, 8/10, 15/100}, {1/10, 5/100, 7/10}}
s = {{7, 0, -2}, {10, -1, 1}, {4, 1, 1}}

then

(Inverse[s].p.s) // MatrixForm

Mathematica graphics

When using floating points, there is always chance of noise generated. Chop[] can be used to clean the final output

p = {{.7, .15, .15}, {.2, .8, .15}, {.1, .05, .7}}
(res = Inverse[s].p.s) // MatrixForm

Mathematica graphics

Chop[res] // MatrixForm

Mathematica graphics

You can always convert final result to floating point (which is more accurate than using floating point during the computation, but sometimes (many times?) of course we can not avoid doing the intermediate steps using floating point), but at least in Mathematica you have the choice.

p = {{7/10, 15/100, 15/100}, {2/10, 8/10, 15/100}, {1/10, 5/100, 7/10}};
(N[Inverse[s].p.s, $MachinePrecision]) // MatrixForm

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.