weird matrix multiplication with or without bracket

2 vues (au cours des 30 derniers jours)
Ruiting
Ruiting le 4 Août 2016
Commenté : Guillaume le 5 Août 2016
I got a weird result of simple matrix multiplication. Theoretically, adding a bracket will not change result and R1=R2, but I have found R1 is not same as R2 by Matlab, especially when some columns of V are similar to each other. Do you know why they are different? and which one is closer to the true value?
Thanks
% V is K by M complex matrix, R is K by K complex matrix
R1=(V'*V)^(-0.5)*V'*R*V*(V'*V)^(-0.5)
R2=(V'*V)^(-0.5)*(V'*R*V)*(V'*V)^(-0.5)

Réponses (2)

Walter Roberson
Walter Roberson le 4 Août 2016

Stephen23
Stephen23 le 4 Août 2016
Modifié(e) : Stephen23 le 4 Août 2016
"Theoretically, adding a bracket will not change result"
Actually the two calculations are not equivalent, there is no reason why their outputs should be the same, and there is no "weird matrix multiplication" going on. These are perfectly normal floating point calculations.
The difference lies between these:
A*(B*C)
A*B*C
Are they the same? No.
MATLAB's operator precedence rules clearly state that brackets have the highest precedence of all, whereas operators of equal precedence are evaluated from left to right. So the above two calculations are actually evaluated as:
A*(B*C)
(A*B)*C
For floating point calculations these can give different results: this is the normal and expected behavior. Neither can be said to be "closer to the true value", because they are both floating point calculations and their errors may depend on many factors such as the magnitudes of the values, their differences, the specific operations, etc. As such, without an in-depth study of the that exact operation and the possible floating point error, it is not possible to say which might give a result closest to the related (but not the same) analytic calculation.
Lets try some values:
A = 0.9;
B = 0.1;
C = 1e20;
fprintf('%.0f\n',A*(B*C))
fprintf('%.0f\n',(A*B)*C)
fprintf('%.0f\n',A*B*C)
Prints this:
9000000000000000000
9000000000000001024
9000000000000001024
Note how the operations that I said are equivalent give the same result.
  2 commentaires
Ruiting
Ruiting le 5 Août 2016
Stephen,
Thank you very much for your detailed explanations. Do you think the floating point calculations can make R1 and R2 very different or some other factors also involved?
If I set up R= I (eye), so
R1=(V'*V)^(-0.5)*V'*V*(V'*V)^(-0.5);
R2=(V'*V)^(-0.5)*(V'*V)*(V'*V)^(-0.5);
and analytically, both R1 and R2 should be I. They are near to I and the difference between R1 and R2 is very small, when V'*V is low dimensional. But when V'*V becomes some higher dimensional, and its condition number(max eigenvalue /min eigenvalue) becomes large and R1 and R2 are very different.
Guillaume
Guillaume le 5 Août 2016
Read the links from Walter's answer, and particularly read up about catastrophic cancellation.
Yes, the order of operations can result in vastly different results:
>> x = pi;
>> y = 1e20;
>> x + y - y %same as (x + y) - y
ans =
0
>> x + (y - y)
ans =
3.1416

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by