[Numpy-discussion] general version of repmat?
wbaxter at gmail.com
Sat Sep 23 11:03:14 CDT 2006
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):
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):
c = c.reshape(-1,n).repeat(nrep,0)
dim_in = shape[i]
dim_out = dim_in*nrep
shape[i] = dim_out
n /= dim_in
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.
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 5486 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20060924/e786574a/attachment-0001.py
More information about the Numpy-discussion