Pagemtimes accuracy versus for loop

조회 수: 11 (최근 30일)
Morten Nissov
Morten Nissov 2021년 5월 5일
편집: Matt J 2021년 5월 5일
I was using pagemtimes for some calculations an I get results which are not quite what I expect, for example
N = 1e3;
a = randn(3, 3, N);
b = randn(2, 3);
c1 = zeros(2, 2, N);
for i=1:N
c1(:,:,i) = b * a(:,:,i) * b';
end
c2 = pagemtimes(pagemtimes(b, a), b');
c3 = pagemtimes(b, pagemtimes(a, b'));
isequal(c1,c2)
isequal(c1,c3)
returns a logical no for both isequal queries.
I assume this is due to numerical deviations? Or is there something non-equivalent between the different fomulations of the "c" matrix.

답변 (1개)

Matt J
Matt J 2021년 5월 5일
편집: Matt J 2021년 5월 5일
I assume this is due to numerical deviations?
Yes, there is no expectation that both approaches will produce the same floating point noise. Clearly the percent errors are very small, though:
N = 1e3;
a = randn(3, 3, N);
b = randn(2, 3);
c1 = zeros(2, 2, N);
for i=1:N
c1(:,:,i) = b * a(:,:,i) * b';
end
c2 = pagemtimes(pagemtimes(b, a), b');
c3 = pagemtimes(b, pagemtimes(a, b'));
%Percent errors
relError=@(a,b)norm(a(:)-b(:),inf)/norm(b(:),inf)*100;
relError(c1,c2)
ans = 2.1378e-14
relError(c1,c3)
ans = 2.1378e-14
  댓글 수: 2
Morten Nissov
Morten Nissov 2021년 5월 5일
Okay sounds good, is there any metric for how small these deviations should be? Like for example differences in 1e-10 indicate different matrices but 1e-12 or less is likely numerical noise.
Matt J
Matt J 2021년 5월 5일
편집: Matt J 2021년 5월 5일
If the difference were greater than, say,
1000*eps(class(c1))
ans = 2.2204e-13
I might start to wonder what was going on.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by