High dimension matrix summation: a + b != b + a
    4 ビュー (過去 30 日間)
  
       古いコメントを表示
    
I have two matrices X1 and X2. Both are 10x15x10x10 dimensions. There are three scalars:  ,
, and
and  .
. 
 ,
, and
and  .
. Let i={i1,i2,i3,i4} denote the index of an element. For elements with the same index in X1 and X2, say X1(i) and X2(i), I want to compute a simple weighted sum:
 +
 +  *X1(i) +
*X1(i) +   *X2(i)
*X2(i)Since the γ's are all scalars, I think I can do this multiplication and summation at the matrix level and get the result as a 10x15x10x10 matrix. However, when I change the order of summation, the results are also different. That is:
A =  +
 +  *X1 +
*X1 +   *X2;
*X2;
 +
 +  *X1 +
*X1 +   *X2;
*X2;B =  +
 +  *X2 +
*X2 +  *X1;
*X1;
 +
 +  *X2 +
*X2 +  *X1;
*X1;The differences in all entries of A and B sum up to 2.3856e-12. However, since this is just element-wise summation, A and B should be exactly the same. 
Can someone explain what is going wrong?
I do notice that if I take out  , I get the same answer... Why does adding a constant affect the final result?
, I get the same answer... Why does adding a constant affect the final result?
 , I get the same answer... Why does adding a constant affect the final result?
, I get the same answer... Why does adding a constant affect the final result?Please see attached for the sample data and codes.
Thank you for your help!!
0 件のコメント
採用された回答
  Wan Ji
      
 2021 年 8 月 23 日
        
      編集済み: Wan Ji
      
 2021 年 8 月 23 日
  
      It is a problem of machine error. If you have learned numerical analysis lessons, you will notice that big number will 'eat' small number when they are added.
a = 1;
b = eps;
for i = 1:10000
a = a+b;
end
c = a+10000*eps
c-a
Well the result is
ans =
     2.220446049250313e-12
When you follow this code, error will not occur!
A =  gamma0 + (gamma1*X1 + gamma2*X2); 
B =  gamma0 + (gamma2*X2 + gamma1*X1); 
A = A(:);
B = B(:);
sum(sum(B-A))
ans = 
0
This is beacuse the add operator order now is now the same for two expressions
3 件のコメント
  Wan Ji
      
 2021 年 8 月 23 日
				So, when you do numerical analysis, avoid adding a large number with a small number or dividing a large number by small number. The error will explode if you do so.  Also, you cannot minus a number far too close to the minuend.
An example will also show why:
calculate  where x=1000001, y = 1000000; (use single precision)
 where x=1000001, y = 1000000; (use single precision)
 where x=1000001, y = 1000000; (use single precision)
 where x=1000001, y = 1000000; (use single precision)We use
a = sqrt(single(1000001)) - sqrt(single(1000000))
The answer is
  single
   4.8828125e-04
While with the following transformation

a = single(1)/(sqrt(single(1000001)) + sqrt(single(1000000)))
The answer is
a = single(1)/(sqrt(single(1000001)) + sqrt(single(1000000)))
a =
  single
   4.9999985e-04
The answer with double precision is
a =
     4.999998750000625e-04
You can see what I mean.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

