Pagemtimes accuracy versus for loop

11 次查看(过去 30 天)
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