On Sun, May 4, 2014 at 9:34 PM, srean <srean.l...@gmail.com> wrote:

> Hi all,
>
>   is there an efficient way to do the following without allocating A where
>
>  A = np.repeat(x, [4, 2, 1, 3], axis=0)
>  c = A.dot(b)    # b.shape
>

If x is a 2D array you can call repeat **after** dot, not before, which
will save you some memory and a few operations:

>>> a = np.random.rand(4, 5)
>>> b = np.random.rand(5, 6)
>>> np.allclose(np.repeat(a, [4, 2, 1, 3], axis=0).dot(b),
...             np.repeat(a.dot(b), [4, 2, 1, 3], axis=0))
True

Similarly, if x is a 1D array, you can sum the corresponding items of b
before calling dot:

>>> a = np.random.rand(4)
>>> b = np.random.rand(10)
>>> idx = np.concatenate(([0], np.cumsum([4,2,1,3])[:-1]))
>>> np.allclose(np.dot(np.repeat(a, [4,2,1,3] ,axis=0), b),
...             np.dot(a, np.add.reduceat(b, idx)))
... )
True

Jaime

-- 
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes
de dominación mundial.
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to