From: Bill B. <wb...@gm...> - 2006-09-23 16:03:14
|
Here's a new version of 'reparray'. I tested the previous version against 'repmat' for the 2d case and found it to be as much as 3x slower in some instances. Ick. So I redid it using the approach repmat uses -- reshape() and repeat() rather than concatenate(). Now it's very nearly as fast as repmat for the 2-d case. def reparray(A, tup): if numpy.isscalar(tup): tup = (tup,) d = len(tup) c = numpy.array(A,copy=False,subok=True,ndmin=d) shape = list(c.shape) n = c.size for i, nrep in enumerate(tup): if nrep!=1: c = c.reshape(-1,n).repeat(nrep,0) dim_in = shape[i] dim_out = dim_in*nrep shape[i] = dim_out n /= dim_in return c.reshape(shape) The full file with tests and timing code is attached. One thing I noticed while doing this was that repmat doesn't preserve subclasses. Which is funny considering 'mat' is part of the name and it only works on 2-d arrays. Using asanyarray() instead of asarray() in the first line should fix that. --Bill |