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

fred fredmfp@gmail....
Wed Apr 25 04:00:08 CDT 2007


Anne Archibald a écrit :
> Well, I see your problem in two parts: (1) build an array of shape
> (7,nx,ny,nz), then (2) flatten it enough to make VTK happy. Step (1)
> is most easily accomplished by making a list of 7 arrays of shape
> (nx,ny,nz) then putting them together along a new axis. Making the
> arrays is no problem, you just use broadcasting. Step (2). putting a
> list of arrays together into one array along a new axis you can do
> with array(), or you could use one of the stacking functions. You only
> have to do that once, so I'm not clear on why you use so many
> hstack()s. I'd take a look at the Numpy Example List; it looks like
> none of the available functions do quite what you want, although a
> combination of [...,newaxis] and concatenate might be all right.
Hi Anne,

This is what I got from the python french newsgroup.
Beautiful and fast ;-)

            csize = (nx-1)*(ny-1)*(nz-1)
            a = array([0, 1, nx+1, nx], dtype=int32)
            a = hstack((a, nx*ny + a))
            i = arange(nx-1, dtype=int32)[newaxis,newaxis,:]
            j = arange(ny-1, dtype=int32)[newaxis,:,newaxis]
            k = arange(nz-1, dtype=int32)[:,newaxis,newaxis]
            b = (k*(nx*ny) + j*nx + i).flatten()
            cells_array = 
zeros(((nx-1)*(ny-1)*(nz-1),VTK_HEXAHEDRON_NB_POINTS+1), dtype=int32)
            cells_array[:,0] = VTK_HEXAHEDRON_NB_POINTS
            cells_array[:,1:] = b[:,newaxis] + a
            cells_id = array([VTK_HEXAHEDRON]*csize, dtype=uint8)
 

Cheers,

-- 
http://scipy.org/FredericPetit



More information about the SciPy-user mailing list