[Scilab-users] funny result for tensor norm calculation
Paul Carrico
paul.carrico at free.fr
Wed Sep 26 22:54:50 CEST 2012
Hi Adrien,
I forgot this basic
thanks
Ive another naïve question concerning eigen values calculation :
D2 is a symmetrical 3x3 matrix while D1 not ; as expected, D1 gives 3 eigen
values with (by convention) eigen_D1(1) > eigen_D1(2) > eigen_D1(3)
but
this is not the case with the symmetrical matrix (under scilab5.3.3) -> the
max value is on eigen_D2(3)
any reason ?
D1 = [2 2 1 ; 1 2 1 ; 0 2 2]
D2 = [ 2 2 1 ; 2 2 1; 1 1 2 ]
eigen_D1 = spec(D1)
eigen_D2 = spec(D2)
Paul
De : users-bounces at lists.scilab.org [mailto:users-bounces at lists.scilab.org]
De la part de Adrien Vogt-Schilb
Envoyé : mercredi 26 septembre 2012 22:32
À : International users mailing list for Scilab.
Objet : Re: [Scilab-users] funny result for tensor norm calculation
On 26/09/2012 19:42, Paul Carrico wrote:
Dear All,
A funny result for calculating the norm of a tensor
for ounce a
traditional method (that probably uses vectorization in back stage) is
faster than the norm function
Paul
mode(0);
A = [ 1 2 3 ; 4 5 6; 7 8 9]
n = 1000
for i = 1 : n
for j = 1 : n
B(i,j) = i*j;
end
end
// Scilab function
tic();
norm(B)
t1 = toc()
// "traditional" method
// norm = sqrt(trace(A_transp * A))
tic
norm_ = (trace(B'*B))**0.5
t2 = toc()
Hi
Yep, same result for me, the handmade computation is 4 times faster. Maybe
norm i written assuming B is a complex hypermatrix or something.
But the real improvement would be to use a "vectorized" code to generate B,
try this:
tic
n = 1000
for i = 1 : n
for j = 1 : n
B(i,j) = i*j;
end
end
toc
tic
BB=(1:n)'*(1:n);
toc
the second method is several hundred times faster than the first.
Note that the first method uses loops when matrix computations are able to
do the job, and, worst, B is not initialized.
This means that every while, scilab will notice that B is larger than
expected, and will ask for a new, larger place in memory to store B, will
copy the existing B from the old place to the new one, only to find out
later that B is actually even bigger.
I do not know how exactly this is implemented, but this can happen basically
for each new value of i, so 1000 time.
The take away message is: when having to building a matrix with a loop,
"initailize" your matrix:
tic
n = 1000
for i = 1 : n
for j = 1 : n
Bad(i,j) = i*j;
end
end
bad=toc()
tic
Good = zeros(n,n);
n = 1000
for i = 1 : n
for j = 1 : n
Good(i,j) = i*j;
end
end
good=toc()
bad/good
//(in my PC this is already 3x faster)
tic
best=(1:n)'*(1:n);
best=toc()
bad/best
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20120926/84b86ead/attachment.htm>
More information about the users
mailing list