Reputation: 1
In Linear Algebra by David Lay, he asks which of the two matrix computation way is faster:
A*(A*x)
OR(A*A)*x
Where A
is an n
xn
matrix and x
is n
x1
vector (matrix product is well defined.)
I made a matlab program and saw it for myself for higher dimensions and I am convinced. But i also tried it for case n=1 and in this case, strangely option 1 is still faster then option 2 but it shouldn't be the case.
Does any one know why case n=1 is showing up faster? it is just marginally faster though
Upvotes: 0
Views: 120
Reputation: 1
Ok. figured it out. on average the calculation time is same. i had a logical error in my code. now, if faster shows less time in one run (which itself is an ensemble average), then in next run it is slower then the other counterpart. here is the matlab code:
close all
clc
clear all
n=1
for j=1:100
tic
for i=1:100
a=round(10*rand(n,n));
x=round(10*rand(n,1));
(a*a)*x;
end
d=toc;
f(j)=d*1e3;
end
for j=1:100
tic
for i=1:100
a=round(10*rand(n,n));
x=round(10*rand(n,1));
(a)*(a*x);
end
e=toc;
t(j)=e*1e3;
end
slower=mean(f)
faster=mean(t)
plot(1:100,f)
hold on
plot(1:100,t,'r')
legend('slower','faster')
Upvotes: 0
Reputation: 883
For the n=1
case both A
and x
are scalars, so both operations consist of two scalar multiplications. The operations should take microseconds (or less) and are possibly faster than the resolution of your timer. The question is more concerned with larger (more realistic) values of n
.
Upvotes: 1