[SciPy-user] filling array without loop...

fred fredmfp@gmail....
Sun Apr 22 13:47:33 CDT 2007


Anne Archibald a écrit :
> ca1 = array([VTK_HEXAHEDRON_NB_POINTS*ones((nz-1,ny-1,nx-1),dtype=int),
>                       k[:-1,:,:]*(nx*ny)+j[:,:-1,:]*nx+i[:,:,:-1],
>                       k[:-1,:,:]*(nx*ny)+j[:,:-1,:]*nx+i[:,:,1:],
>                       k[:-1,:,:]*(nx*ny)+j[:,1:,:]*nx+i[:,:,1:],
>                       k[:-1,:,:]*(nx*ny)+j[:,1:,:]*nx+i[:,:,:-1],
>                       k[1:,:,:]*(nx*ny)+j[:,:-1,:]*nx+i[:,:,:-1],
>                       k[1:,:,:]*(nx*ny)+j[:,:-1,:]*nx+i[:,:,1:],
>                       k[1:,:,:]*(nx*ny)+j[:,1:,:]*nx+i[:,:,1:],
>                       k[1:,:,:]*(nx*ny)+j[:,1:,:]*nx+i[:,:,:-1]], dtype=int)
>   
Whaouh !

Many thanks, Anne !

I absolutely understand nothing, but I'll work hard on it ;-)

I think you are ready to answer to a more complex (at least, for me, of 
course) issue, Anne :-))))

Each array cell is a convolution that I wrote as a scalar product 
between KW, a weights filter vector, and input_data,
the "raw" data (3D); output_data is the filtered response given by the 
filter.

    def calc_output(self):
        from scipy import copy, inner, zeros
        nx, ny, nz = self.nx, self.ny, self.nz
        nvx, nvy, nvz = self.nvx, self.nvy, self.nvz
        nx_nvx, ny_nvy, nz_nvz = nx-nvx, ny-nvy, nz-nvz
        nvx1, nvy1, nvz1 = nvx+1, nvy+1, nvz+1
        KWsize = self.KW.size
        KW = self.KW
        data_input = self.input
        data_output = self.output = copy(data_input)
         for k1 in range(nvz, nz_nvz):
             for j1 in range(nvy, ny_nvy):
                 for i1 in range(nvx, nx_nvx):
                     data_output[i1,j1,k1] = inner(KW, \
                                                   
data_input[i1-nvx:i1+nvx1,j1-nvy:j1+nvy1,k1-nvz:k1+nvz1].reshape(KWsize))

These triple loops are _very_ CPU time consuming.

Is it possible to write this function without loop ?

If yes, you are my God, Anne ! :-))))


Cheers,

-- 

http://scipy.org/FredericPetit



More information about the SciPy-user mailing list