matrix transpose multiplication precision

2 次查看(过去 30 天)
Hi everyone,
Forgive me if this is a stupid question. Is there a loss of precision when Matlab calculates the multiplication of the transpose of a matrix and itself?
for example, set variable q to a 2d matrix of size 6x6 with value 0.9 for each element. The result of q'*q is different than (q+0)'*q.
if true
% code
q = .9*ones(6,6);
norm((q+0)'*q - q'*q)
end
returns:
ans =
5.3291e-015
However a 5x5 matrix is alright:
if true
% code
q = .9*ones(5,5);
norm((q+0)'*q - q'*q)
end
A "whos" of q reveals:
whos qq Name Size Bytes Class Attributes
qq 6x6 288 double
Thanks, Afro

采纳的回答

James Tursa
James Tursa 2013-9-5
编辑:James Tursa 2013-9-5
MATLAB uses BLAS routines to do matrix multiplication. There are separate routines available for generic matrix multiplication and symmetric matrix multiplication. In your case, the MATLAB parser recognizes q'*q as a symmetric matrix multiply and will call the symmetric matrix multiply routine (only calculates about 1/2 the answer and then fills in the rest with copies, which is faster). But for the (q+0)'*q case the parser does not recognize the symmetry of the multiply so it calls the generic matrix multiply routine instead. Slightly different code can produce slightly different results, which is to be expected for floating point arithmetic. Both are "correct".

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by