Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Precision question

Subject: Precision question

From: mailcwc@gmail.com

Date: 21 Oct, 2011 03:28:54

Message: 1 of 2

I have three algorithms to calculate the inner product of vector
A(i,j) and vector V for M x N times.
With the line A = round(A), B1 = B2 = B3.
Without the line A = round(A);, B1 != B2, B2 = B3.
Is this a precision error?
Which one is correct?


clc;100
clear all;
M = 1000;
N = 1000;
P = 10;

A = 10*rand(M,N,P);
A = round(A);

% A = zeros(M,N,P);
% for i = 1:P
% A(:,:,i) = i;
% end

V = 1:P;

% Algorithm 1
A = shiftdim(A,2);
tic;
B1 = zeros(M,N);
for i = 1:M
    for j = 1:N
        B1(i,j) = V*A(:,i,j);
    end
end
t1 = toc;
A = shiftdim(A,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Algorithm 2
tic;
temp = zeros(M,N,P);
for i = 1:P
    temp(:,:,i) = A(:,:,i)*V(i);
end

B2 = sum(temp,3);
clear temp;
t2 = toc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Algorithm 3
tic
v = repmat(reshape(V, [1 1 P]), [M N]);
temp = bsxfun(@times, A, v);
B3 = sum(temp,3);
clear temp;
t3 = toc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


isequal(B1,B2,B3)
isequal(B1,B2)
isequal(B2,B3)
isequal(B1,B3)
t1
t2
t3

Subject: Precision question

From: Roger Stafford

Date: 21 Oct, 2011 19:45:26

Message: 2 of 2

"mailcwc@gmail.com" <mailcwc@gmail.com> wrote in message <89936606-0159-4134-80f0-4248e2cba78b@u13g2000prm.googlegroups.com>...
> I have three algorithms to calculate the inner product of vector
> A(i,j) and vector V for M x N times.
> With the line A = round(A), B1 = B2 = B3.
> Without the line A = round(A);, B1 != B2, B2 = B3.
> Is this a precision error?
> Which one is correct?
> ........
- - - - - - - - -
  When you take the sum of terms (as in an inner product,) if the quantities involved have one-bits in their binary representations occurring at or near their least significant bit positions, the results will generally be subject to round off errors. In this case the order in which the additions are performed may affect the result, though of course only in these least bits.

  In other words, with such quantities, the associative law of addition is not strictly adhered to, so that (a+b)+c does not always give exactly the same result as a+(b+c). Try this on your computer:

 a = 3/14; b = 3/14; c = 15/14;
 (a+b)+c == a+(b+c)

  This would presumably account for your results when the 'rand' function is used without doing a 'round' on A. The matrix multiply operation in algorithm 1 apparently performs its additions in a different order than is used in the element-by-element multiplication followed by the 'sum' function of algorithms 2 and 3.

  The same comment applies to a series of successive multiplications. The associative law of multiplication does not always hold so that the ordering of multiplications would affect the result.

  You must always be prepared for differing results in the least few bits of an answer when different, though supposedly mathematically equivalent, series of arithmetic operations are performed, due to differences in rounding errors.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us