[Scilab-users] Vectorization : how to proceed
Paul Carrico
paul.carrico at free.fr
Tue Nov 20 19:05:16 CET 2012
Deer Serge,
Effectively its cleaner (and faster) to use directly vectors as arguments
see above
B3 = list in arguments
B4 = vector in argument
Thanks for the recipe
I need to modify my code
LJ
Paul
/// classical function
function vect_fct2=fct_angles2(i, j)
vect_fct2 = [ (cos(i) * sin(j) ) (-sin(j)) i j];
endfunction
// vectorized function
function vect_fct3=fct_angles3(i, j)
vect_fct3 = [ (cos(i). * sin(j) ) (-sin(j)) i j];
endfunction
function vect_fc4t=fct_angles4(i, j)
i=i(:);j=j(:); //transform i and j into column vectors
vect_fct4 = [ (cos(i). *.sin(j) ), (-ones(i). *.sin(j)), i. *.ones(j),
ones(i). *. j];
endfunction
// traditional with loops
B2 = zeros(130321,4); n = 1
tic()
for i = 0 : 360
for j = 0 : 360
B2(n,:) = fct_angles2(i,j);
n = n +1;
end
end
time_with_loops = toc()
// vectorized 1
B3 = zeros(130321,4);
theta_vect = (0:360)'.*.ones(361,1);
fi_vect = (ones(361,1)'.*.[0:360])';
tic()
B3 = fct_angles3(theta_vect([1:130321],1),fi_vect([1:130321],1));
time_vectorization = toc()
max(abs(B2 - B3))
// vectorized 2
tic()
B4 = zeros(130321,4);
B4 = fct_angles3(theta_vect,fi_vect);
time2_vectorization = toc()
max(abs(B4 - B3))
De : users-bounces at lists.scilab.org [mailto:users-bounces at lists.scilab.org]
De la part de Serge Steer
Envoyé : mardi 20 novembre 2012 18:37
À : International users mailing list for Scilab.
Objet : Re: [Scilab-users] Vectorization : how to proceed
On 20/11/2012 17:28, Paul Carrico wrote:
Finally I think Ive found how to proceed .. at the same time I think I
understood the use of the dot . (see after the cos)
You are right, but take care that your code will work if and only if at
least one of i or j are scalars.
If you want your code being able to work when i and j are vectors you can
use the kronecker product .*.
function vect_fct=fct_angles(i, j)
i=i(:);j=j(:); //transform i and j into column vectors
vect_fct3 = [ (cos(i). *.sin(j) ), (-ones(i). *.sin(j)), i. *.ones(j),
ones(i). *. j];
endfunction
Serge Steer
Paul
###################################################
function vect_fct=fct_angles(i, j)
vect_fct3 = [ (cos(i). * sin(j) ) (-sin(j)) i j];
endfunction
B = zeros(130321,4);
theta_vect = (0:360)'.*.ones(361,1);
fi_vect = (ones(361,1)'.*.[0:360])';
B = fct_angles3(theta_vect([1:130321],1),fi_vect([1:130321],1));
De : users-bounces at lists.scilab.org [mailto:users-bounces at lists.scilab.org]
De la part de Paul Carrico
Envoyé : mardi 20 novembre 2012 14:34
À : 'International users mailing list for Scilab.'
Objet : [Scilab-users] Vectorization : how to proceed
Dear All
In the above example, how can I proceed to perform the product (see function
fct_angles2) ?
Indeed, theta are 2 vectors, so no problem for the fct_angles
but in case
of product, its naturally not work !!! what is the correct syntax in order
to use vectorization ?
Thanks
Paul
######################################################
mode(0)
function vect_fct=fct_angles(i, j)
vect_fct = [cos(i)+sin(j) -sin(j) i j];
endfunction
function vect_fct2=fct_angles2(i, j)
vect_fct = [cos(i)*sin(j) -sin(j) i j];
endfunction
B1 = zeros(130321,4);
theta_vect = (0:360)'.*.ones(361,1);
fi_vect = (ones(361,1)'.*.[0:360])';
B1 = fct_angles(theta_vect([1:130321],1),fi_vect([1:130321],1));
B2 = fct_angles2(theta_vect([1:130321],1),fi_vect([1:130321],1));
_______________________________________________
users mailing list
users at lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20121120/f731e5f6/attachment.htm>
More information about the users
mailing list