Effacer les filtres
Effacer les filtres

Pagemtimes accuracy versus for loop

13 vues (au cours des 30 derniers jours)
Morten Nissov
Morten Nissov le 5 Mai 2021
Modifié(e) : Matt J le 5 Mai 2021
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.

Réponses (1)

Matt J
Matt J le 5 Mai 2021
Modifié(e) : Matt J le 5 Mai 2021
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 commentaires
Morten Nissov
Morten Nissov le 5 Mai 2021
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 le 5 Mai 2021
Modifié(e) : Matt J le 5 Mai 2021
If the difference were greater than, say,
1000*eps(class(c1))
ans = 2.2204e-13
I might start to wonder what was going on.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by