Re: ML matrices multiplication accuracy
- From: ellieandrogerxyzzy@xxxxxxxxxxxxxxxxxxxxxx (Roger Stafford)
- Date: Sun, 24 Jun 2007 00:27:07 GMT
---------------------In article <ef5b370.-1@xxxxxxxxxxxxxxxxxxxxxxx>, "TarekEldeeb"
<tarekeldeeb@xxxxxxx> wrote:
Hello all,multiplication:
I wrote a simple matlab function to do the matrices
operator
function product = mult(a,b)
[ra,ca] = size(a);
[rb,cb] = size(b);
product = zeros(ra,cb);
for c=1:ra
for cc=1:cb
for ccc=1:ca
product(c,cc) = product(c,cc) + a(c,ccc)*b(ccc,cc);
end
end
end
I found that the result is not equal to the built-in matlab
(a*b).found
When I generate rand matrices [values range from 0~1], it was
that the error from the built-in matlab's matrix multiplicationis in
the order of 10^-12
Does matlab uses some sort of inaccurate algorithm to multiply
matrices ? or it uses single instead of double precision ?
Does anyone have a clue ?
Thanks in advance,
Yours,
Tarek
Tarek, your original question got me to experimenting with matlab's
matrix multiplication on my machine (4a), and the explanation I gave you
earlier for the differences between the answers with your for-loop and
with direct matrix multiplication do not account for what I have finally
observed. If you recall, I stated earlier that different sequencing of
operations, like (x + y) + z as opposed to x + (y + z), could yield
slightly different answers, which in fact is often true. However, I've
managed to boil things down to just four numbers (they weren't hard to
find), a, b, c, and d, defined below. When you write a*c+b*d, or any of
the other seven ways of rearranging them such as d*b+a*c etc., they all
give the same answer as would be expected, but this differs by one bit
from the matrix product, [a,b]*[c;d], and this is NOT what one would
expect.
Nothing in IEEE specifications would predict such a difference in terms
of the round off errors one could anticipate. I can think of only one
possibility to account for it. There is a group of floating point
multiply-add instructions present on many computer chips which perform
multiplication and addition in a single operation. My Macintosh computer
chip (PowerPC 604e) manual states, "These instructions combine multiply
and add operations without an intermediate rounding operation. The
fractional part of the intermediate product is 106 bits wide, and all 106
bits take part in the add/subtract portion of the instruction." It seems
possible to me that MathWorks' matlab compiler has been purposefully
written to take advantage of such instructions so as to tend to decrease
the amount of over-all round off error accumulation for large matrices.
The MathWorks' people can tell us whether or not this is true.
Here are the four quantities, a, b, c, and d I mentioned:
format hex
a = hex2num('bfd729edb7ae53dc');
b = hex2num('3fef2524f4fe4a4a');
c = hex2num('bf88abe7503157c0');
d = hex2num('bfddeefa0ebbddf4');
[a*c+b*d;
c*a+b*d;
a*c+d*b;
c*a+d*b;
b*d+a*c;
b*d+c*a;
d*b+a*c;
d*b+c*a;
[a,b]*[c;d]]
The first eight results are all the same but the matrix product at the
ninth position differs by one bit (at least on my macine.)
Roger Stafford
.
- Follow-Ups:
- Re: ML matrices multiplication accuracy
- From: Yi Cao
- Re: ML matrices multiplication accuracy
- From: Tim Davis
- Re: ML matrices multiplication accuracy
- References:
- ML matrices multiplication accuracy
- From: Tarek Eldeeb
- Re: ML matrices multiplication accuracy
- From: Roger Stafford
- Re: ML matrices multiplication accuracy
- From: Tarek Eldeeb
- ML matrices multiplication accuracy
- Prev by Date: Bartlett's test for dimensionality
- Next by Date: Re: What is max usable memory (largest matrix) in Matlab on MacBook Pro (Santa Rosa) 4GB RAM?
- Previous by thread: Re: ML matrices multiplication accuracy
- Next by thread: Re: ML matrices multiplication accuracy
- Index(es):
Relevant Pages
|
Loading