Re: ML matrices multiplication accuracy



Roger Stafford wrote:


In article <ef5b370.-1@xxxxxxxxxxxxxxxxxxxxxxx>,
"Tarek
Eldeeb"
<tarekeldeeb@xxxxxxx> wrote:

Hello all,

I wrote a simple matlab function to do the matrices
multiplication:

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
operator
(a*b).

When I generate rand matrices [values range from 0~1],
it was
found
that the error from the built-in matlab's matrix
multiplication
is 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


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
.



Relevant Pages

  • Re: ML matrices multiplication accuracy
    ... I wrote a simple matlab function to do the matrices ... I found that the result is not equal to the built-in matlab ... give the same answer as would be expected, but this differs by one bit ... "These instructions combine multiply ...
    (comp.soft-sys.matlab)
  • Re: Serial Communication with "DirectedPerception" pan-/tilt head
    ... through serial connection. ... instructions through hyperterminal everything works. ... When I try to operate the apparatus through MatLab, however, it fails to ... I tried every possible configuraton with the 'Test & Measurement ...
    (comp.soft-sys.matlab)
  • Re: Copy Excel-cells
    ... instructions how to use the commands in the recorded macro from ... MatLab? ... >> I want to copy cells in excel to new cells in the same or in a ... > Now see the instructions in the VB. ...
    (comp.soft-sys.matlab)
  • Re: Transparency/print problem
    ... I just tried Matlab R2006b ... There are instructions at the bottom of the stack trace (which are also ... Steve Lord ...
    (comp.soft-sys.matlab)
  • 3D nonlinear least squares fit in Matlab
    ... I am new to Matlab, and trying to solve the following ... I have some line intersection constraints as described below). ... 'rod' and the simple 3D structure to be the 'nest'. ... directly feed to the Matlab function chosen in 1. ...
    (comp.soft-sys.matlab)

Loading