[Scilab-users] vectorization
    Paul Carrico 
    paul.carrico at free.fr
       
    Tue Nov 13 08:47:49 CET 2012
    
    
  
Dear all,
 
Im trying to optimize a part of an existing (but slow) code including a lot
of loops ; naturally Im thinking in using vectorization  and for that issue
I read the Michaël Baudins document (programming in Scilab V0.9) 
 Ive the feeling the above costly code can be vectorized, but some aspects
are not clear for me at the moment (basically how to transform the double
loop theta/fi in a vector for example ?) 
 
NB:
-          Since the initial tensor is symmetrical, is expressed as a vector
-          To apply transformations, its necessary to rebuild its matrix
form 
 (compared to a direct calculation , the current solution is faster)
-          This kind of code is currently called hundred thousand times 
clearly not acceptable 
-          I understand weve to transform any variable/constant under
vector 
 
Any advice ? where can I find any tutorial or doc that explains and brackets
the use of (efficient) vectorization ?
 
Regards
 
Paul
 
 
 
 
mode(0)
 
// ###############################################################
function E_prime=fct_transformation_M(E, theta, fi)
    // values of theta and fi are in radian
    theta = theta * %pi / 180;
    fi = fi * %pi / 180;
 
    // Calculation of matrix in the studied plane
    E_prime=zeros(3,3);
    M_transform = [
    (cos(theta)* sin(fi)) (sin(theta) * sin(fi)) (cos(fi));
    (-sin(theta)) (cos(theta)) 0. ;
    (-cos(theta) * cos(fi)) (-sin(theta) * cos(fi)) (sin(fi));
    ];
 
    // New tensor calculation
    E_prime = M_transform * E * M_transform';
endfunction
 
function M_new=fct_vector2matrix(M)
    M_new = zeros(3,3);
    M_new(1,1)= M(1,1);
    M_new(2,2)= M(1,2);
    M_new(3,3)= M(1,3);
    M_new(1,2)= M(1,4);
    M_new(2,3)= M(1,5);
    M_new(1,3)= M(1,6);
    M_new(2,1)= M_new(1,2);
    M_new(3,1)= M_new(1,3);
    M_new(3,2)= M_new(2,3);
endfunction
 
function M_vect=fct_matrix2vector(M)
    M_vect = zeros(1,6);
    M_vect(1,1) = M(1,1);
    M_vect(1,2) = M(2,2);
    M_vect(1,3) = M(3,3);
    M_vect(1,4) = M(1,2);
    M_vect(1,5) = M(2,3);
    M_vect(1,6) = M(1,3);
endfunction
// ###############################################################
 
Epsilon = 10^(-8) *[
4.441D-08 0. 0. 0. 0. 0. ;
-1022. -6278. 6914. 210.6 783100. -33910. ;
-2316. -24190. 25930. 894.6 1545000. -67320. ;
-3608. -52660. 55610. 2004. 2284000. -99750. ;
-4895. -90810. 94970. 3508. 3002000. -131300. ;
-6171. -137700. 143100. 5365. 3699000. -161900. ;
-7429. -192400. 199000. 7533. 4373000. -191600. ;
-8675. -254200. 262000. 9997. 5027000. -220400. ;
-8675. -254200. 262000. 9997. 5027000. -220400. 
]
 
[p,c] = size(Epsilon);
// number of time steps
step_ = 2; //
 
nb_loops = 0;
tic()
for i = 1 : p
    for theta = 0 : step_ : 180
        for fi = 0 : step_ : 180
            Epsilon_matrix = fct_vector2matrix(Epsilon(i,:));
            Epsilon_prime_matrix =
fct_transformation_M(Epsilon_matrix,theta,fi);
            Epsilon_prime(i,:) = fct_matrix2vector(Epsilon_prime_matrix);
            nb_loops = nb_loops + 1;
        end
    end
end
t_vect2matr2vect = toc()
nb_loops
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20121113/9bf30560/attachment.htm>
    
    
More information about the users
mailing list