[Numpy-tickets] [NumPy] #411: indices function too slow

NumPy numpy-tickets at scipy.net
Sat Jan 6 19:08:13 CST 2007


#411: indices function too slow
-------------------------+--------------------------------------------------
 Reporter:  auger        |       Owner:  somebody   
     Type:  enhancement  |      Status:  new        
 Priority:  normal       |   Milestone:  1.0 Release
Component:  numpy.core   |     Version:             
 Severity:  normal       |    Keywords:  indices    
-------------------------+--------------------------------------------------
 The indices function is quite slow (~12s to create indices for a 4000x4000
 array on my machine). Here is a version that is between 4 and 8 times
 faster (depending on the array sizes).


 {{{
 def indices(shape,dtype=int):
         lshape = []
         oshape = [len(shape)]
         for i in shape:
                 lshape.append(i)
                 oshape.append(i)
         out = empty(tuple(oshape))
         dims = len(lshape)
         for i in range(dims):
                 v = lshape.pop(i)
                 lshape.append(1)
                 subshape = tuple(lshape)
                 coords = arange(shape[i])
                 tiled = tile(coords,subshape)
                 ndims = tiled.ndim
                 tindx = arange(ndims)
                 tindx[tindx>i] -= 1
                 tindx[i] = -1
                 lshape.pop()
                 lshape.insert(i,v)
                 out[i] = tiled.transpose(tindx)
         return out.astype(dtype)
 }}}

-- 
Ticket URL: <http://projects.scipy.org/scipy/numpy/ticket/411>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.


More information about the Numpy-tickets mailing list