Pagemtimes accuracy versus for loop

11 visualizaciones (últimos 30 días)
Morten Nissov
Morten Nissov el 5 de Mayo de 2021
Editada: Matt J el 5 de Mayo de 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.

Respuestas (1)

Matt J
Matt J el 5 de Mayo de 2021
Editada: Matt J el 5 de Mayo de 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 comentarios
Morten Nissov
Morten Nissov el 5 de Mayo de 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 el 5 de Mayo de 2021
Editada: Matt J el 5 de Mayo de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by