[Numpy-discussion] general version of repmat?
Bill Baxter
wbaxter at gmail.com
Sat Sep 23 12:03:14 EDT 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):
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: play.py
Type: text/x-python
Size: 5486 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20060924/e786574a/attachment-0001.py>
More information about the NumPy-Discussion
mailing list