Re: ML matrices multiplication accuracy
- From: "Yi Cao" <y.cao@xxxxxxxxxxxxxxx>
- Date: Sun, 24 Jun 2007 05:05:14 -0400
Roger Stafford wrote:
"Tarek
In article <ef5b370.-1@xxxxxxxxxxxxxxxxxxxxxxx>,
matlabEldeeb"
<tarekeldeeb@xxxxxxx> wrote:
Hello all,multiplication:
I wrote a simple matlab function to do the matrices
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
it wasoperator
(a*b).
When I generate rand matrices [values range from 0~1],
multiplymultiplicationfound
that the error from the built-in matlab's matrix
is in
the order of 10^-12
Does matlab uses some sort of inaccurate algorithm to
precision ?matrices ? or it uses single instead of double
---------------------
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
Which matlab version did you use? On my machine (matlab r2007a, Intel
Core Duo Processor T2500), I got all 9 cases exactily the same:
format long eng
a = hex2num('bfd729edb7ae53dc');
b = hex2num('3fef2524f4fe4a4a');
c = hex2num('bf88abe7503157c0');
d = hex2num('bfddeefa0ebbddf4');
x=[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]]
x =
-450.855637235782e-003
-450.855637235782e-003
-450.855637235782e-003
-450.855637235782e-003
-450.855637235782e-003
-450.855637235782e-003
-450.855637235782e-003
-450.855637235782e-003
-450.855637235782e-003
norm(diff(x))
ans =
0.00000000000000e-003
hth
Yi
.
- Follow-Ups:
- Re: ML matrices multiplication accuracy
- From: Roger Stafford
- 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
- Re: ML matrices multiplication accuracy
- From: Roger Stafford
- ML matrices multiplication accuracy
- Prev by Date: Re: reading text file
- Next by Date: Question about function arguments
- Previous by thread: Re: ML matrices multiplication accuracy
- Next by thread: Re: ML matrices multiplication accuracy
- Index(es):
Relevant Pages
|
Loading