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 Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by