_______________________________________________
NumPy-Discussion mailing list
NumPy-Di...@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
How about using list comprehension? And setting dot = numpy.dot. Would
initializing the the c list first help?
Doesn't buy much:
>> def forloop(a, b):
.....: c = []
.....: for x, y in izip(a, b):
.....: c.append(np.dot(x, y))
.....: return c
.....:
>> a = [np.random.rand(4,4) for i in range(10000)]
>> b = [np.random.rand(4,4) for i in range(10000)]
>>
>> timeit forloop(a, b)
10 loops, best of 3: 21.2 ms per loop
>>
>> dot = np.dot
>> timeit [dot(x, y) for x, y in izip(a, b)]
100 loops, best of 3: 19.2 ms per loop
>Could you place all Rot's into the same array and all the Trans's into the same array?Well I guess since they're all the same size. I would just have to do array(a). But the result of the dot product of two 3d arrays is most unexpected:
>>> a = numpy.ones((4,5,6))
>>> a = numpy.ones((10,4,4))
>>> b = numpy.ones((10,4,4))
>>> c = numpy.dot(a,b)
>>> c.shape
(10, 4, 10, 4) #Hmm, not what a newbie expects D:Hmm, I'm not sure I understand what is being done there.
>Yes, there is a trick for this using a multiply with properly placed newaxis followed by a sum. It uses more memory but for stacks of small arrays that shouldn't matter. See the post here.
_______________________________________________
NumPy-Discussion mailing list
NumPy-Di...@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Ok I get it. Thanks!
Numpy syntax that works for me:
numpy.sum(a[:,:,:,numpy.newaxis]*b[:,numpy.newaxis,:,:],axis=-2)
_______________________________________________
NumPy-Discussion mailing list
NumPy-Di...@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
> Hello,
>
> I have a list of 4x4 transformation matrices, that I want to "dot with" another list of the same size (elementwise).
> Making a for loop that calculates the dot product of each is extremely slow,
> I thought that maybe it's due to the fact that I have thousands of matrices and it's a python for loop and there's a high Python overhead.
>
> I do something like this:
> >> for a,b in izip(Rot,Trans):
> >> c.append(numpy.dot(a,b))
If you need/want more speed than the solution Chuck proposed, you should check out Cython and Tokyo. Cython lets you write loops that execute at C speed, whereas Tokyo provides a Cython level wrapper for BLAS (no need to go through Python code to call NumPy). Tokyo was designed for exactly your use case: lots of matrix multiplies with relatively small matrices, where you start noticing the Python overhead.
David
> If you need/want more speed than the solution Chuck proposed, you should check out Cython and Tokyo. Cython lets you write loops that execute at C speed, whereas Tokyo provides a Cython level wrapper for BLAS (no need to go through Python code to call NumPy). Tokyo was designed for exactly your use case: lots of matrix multiplies with relatively small matrices, where you start noticing the Python overhead.
It occurred to me I neglected to provide a link (cursed iPhone):
http://www.vetta.org/2009/09/tokyo-a-cython-blas-wrapper-for-fast-matrix-math/
On 2010-07-15, at 4:31 PM, David Warde-Farley wrote:
> If you need/want more speed than the solution Chuck proposed, you should check out Cython and Tokyo. Cython lets you write loops that execute at C speed, whereas Tokyo provides a Cython level wrapper for BLAS (no need to go through Python code to call NumPy). Tokyo was designed for exactly your use case: lots of matrix multiplies with relatively small matrices, where you start noticing the Python overhead.
It occurred to me I neglected to provide a link (cursed iPhone):
http://www.vetta.org/2009/09/tokyo-a-cython-blas-wrapper-for-fast-matrix-math/