Which one is the right answer, sum(w(:)) or sum(sum(w))?
Show older comments
Hi, all
Just as title. When process certain matrix, like the one in attachment, I found different method returns different result, which one should be used?
>> sum(w(:)) - sum(sum(w))
ans =
single
-3.8147e-06
Since I am quite new to MATLAB, can anyone tell me, is there any other function or Best Practical method to solve such problem?
I know, the real cause of this problem is due to the limitation of bit length on hardware, but if the result is highly depend on the order of matrix elements, I have to wondering neither one of the methods can get the right answer.
This issue is related to sum(), but also influence related functions like mean(), etc. Since all these are daily used element blocks, I thinks, it worth to be clearified in more detailed.
After all, accuracy is critical to mathmatic, and the truth is only one. The aim of MATLAB is to make user focus on the beauty of math but not the detail of hardware, isn't it?
Waiting for any comments!
Regards,
Liu Lan
Accepted Answer
More Answers (3)
sum(sum(w)) will generally be slower, because it requires 2 function calls. Also, it only works for 2D arrays whereas sum(w(:)) or sum(w,'all') work for w of any dimension.
There is no way you can optimize the accumulation of floating point errors through any one of the 3 calling strategies. The error is influenced by the way the summation is split into parallel jobs and distributed to your multicore processor. This is done in ways that are hidden from you as the user, and which will be hardware and OS dependent.
1 Comment
Walter Roberson
on 22 Oct 2022
w(:) turns out to be a function call for matlab purposes. If w is marked as complex but all of the complex components are zero or negative zero, then w(:) will drop the complex component. The data pointer for (:) will be different than the original one for complex values.
reshape(w, [], 1) on the other hand is treated specially and will retain any all-zero complex component. The data pointer will not be changed: reshape creates a new dimension descriptor pointing to the same data.
Bruno Luong
on 22 Oct 2022
Edited: Bruno Luong
on 22 Oct 2022
"Since I am quite new to MATLAB, can anyone tell me, is there any other function or Best Practical method to solve such problem?"
The best pratice IMO is using
sum(w,'all')
if your MATLAB can support it. I believe.
It seems the result is the same than sum(w(:)) but avoid the tiny slowness of (:) for complex data. NOTE that Walter warns about that in his comment, however I'm not 100% sure the Excution engine is smart enough to bypass explicit call of (:) in the statement sum(w(:)), but let us assume it is not.
The order has been studied in depth by TMW and they come up with relative robust but fast way to implement sum for generic data.
The most accurate otder depends on your data (oscillante data like Bessel function in long rage is horrible to compute caccuretely), and the good order is very difficult to know and very expansive to estimate. I don't believe sum is designed for that purpose, and there is few implementations on FEX when I look a while ago.
But your ^rincipal duty is design the overall numerical method that should be less sensitive to truncation error of sum, mean etc... generally anything related to limited floating point precision. If not your algorithm is a bad one.
3 Comments
Lan
on 24 Oct 2022
Bruno Luong
on 24 Oct 2022
Edited: Bruno Luong
on 24 Oct 2022
As it is not my code I cannot post it here.
Lan
on 24 Oct 2022
Lan
on 24 Oct 2022
0 votes
4 Comments
Walter Roberson
on 24 Oct 2022
Is there any method to make sum() stick to the Right Answer of a 'single' type matrix?
We already told you NO.
@Jan has a File Exchange contribution that implements several different extended precision summation methods; see https://www.mathworks.com/matlabcentral/fileexchange/26800-xsum
Any of the methods in his description that talk about being "equivalent" to a certain number of decimal places, will not give results "rounded from Exactly Arithmatic summation"
Several years ago, I read a paper in which some numeric analysis people demonstrated a family of functions such that for any given binary floating point precision (e.g., could be 10000 binary digits), that a function can be constructed such that the evaluation of the function will, in an infinite number of places, round incorrectly compared to mathematical analysis.
Is it theoretically possible to get correct summation if you stick to the input values being IEEE 754 Double Precision ? Maybe; it might require aligning everything in a 2098 (or possibly more) bit number. (abs(log2(eps(realmin))) + log2(realmax) = 2098 - you might be adding the largest possible number and the smallest possible number, and you cannot lose any bits because the expression might be
SUM([realmax(), eps(realmin), -realmax())
and the result needs to be eps(realmin) exactly.
One thing you can do is use
sum(sym(YourDoubleMatrix, 'f'))
as the sym() part will convert each value to its internal rational value, such as 884279719003555/281474976710656 being the internal representation for the closest representable value to π . The symbolic toolbox can then add the rationals, and you can do appropriate rounding afterwards.
Bruno Luong
on 24 Oct 2022
Edited: Bruno Luong
on 24 Oct 2022
@Lan "The definition of Right Answer here should be the unique 'single' value, rounded from Exactly Arithmatic summation of all members in specified matrix.
Anyway, from the discussion, I think I might get the answer: there is no such build-in function in MATLAB can achieve that purpose, at least on 'single' type, isn't it?"
There is NOT such floating point algorithm based on native sum of 2 floating numbers, MATLAB or not.
Walter Roberson
on 24 Oct 2022
https://dl.acm.org/doi/10.1145/3544488 seems to imply that there are known algorithms
Lan
on 24 Oct 2022
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!