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

fred fredmfp at gmail.com
Sun Apr 22 12:31:51 EDT 2007


Hi,

I try to fix the following problem, using scipy arrays method.
The fact is that I don't know which methods I can use to fix it.

Thus, I set this array, with loops, as this :

            cells_array = array([[VTK_HEXAHEDRON_NB_POINTS, \
                                  k*(nx*ny) + j*nx + i, \
                                  k*(nx*ny) + j*nx + i+1, \
                                  k*(nx*ny) + (j+1)*nx + i+1, \
                                  k*(nx*ny) + (j+1)*nx + i, \
                                  (k+1)*(nx*ny) + j*nx + i, \
                                  (k+1)*(nx*ny) + j*nx + i+1, \
                                  (k+1)*(nx*ny) + (j+1)*nx + i+1, \
                                  (k+1)*(nx*ny) + (j+1)*nx + i] \
                                 for k in range(nz-1) for j in 
range(ny-1) for i in range(nx-1)], dtype='i').ravel()

Works fine, but a bit too slow for nx,ny,nz \approx 10^2 (endless).

As an example, let's say nx, ny, nz = 2, 3, 4 ; cells_array looks like this:

[[ 8  0  1  3  2  6  7  9  8]
 [ 8  2  3  5  4  8  9 11 10]
 [ 8  6  7  9  8 12 13 15 14]
 [ 8  8  9 11 10 14 15 17 16]
 [ 8 12 13 15 14 18 19 21 20]
 [ 8 14 15 17 16 20 21 23 22]]

My first idea is to work on the transpose of the array:

[[ 8  8  8  8  8  8]
 [ 0  2  6  8 12 14]
 [ 1  3  7  9 13 15]
 [ 3  5  9 11 15 17]
 [ 2  4  8 10 14 16]
 [ 6  8 12 14 18 20]
 [ 7  9 13 15 19 21]
 [ 9 11 15 17 21 23]
 [ 8 10 14 16 20 22]]

My second idea is to proceed step-by-step.

1) Filling the first row is trivial.

2) Second row

I'm thinking of setting
a = arange(0, ny, 2) (set [0, 2])

then "replicate" a to the next items, adding (ny-1)*(nz-1) each time.
-> a = [ 0  2  6  8 12 14]

3) 3rd, 4th & 5th rows

a+1
a+3
a+2

4) 6th to 9th
repeat previous steps.

I think I have the idea (at least, one idea ;-)
but don't know how to do this with scipy.

Any suggestion are welcome.

Thanks in advance.

Cheers,

-- 
http://scipy.org/FredericPetit




More information about the SciPy-User mailing list