[Numpy-discussion] transform an array of points efficiently?

Keith Goodman kwgoodman at gmail.com
Thu Jul 9 22:22:40 EDT 2009


On Thu, Jul 9, 2009 at 7:08 PM, Chris Colbert<sccolbert at gmail.com> wrote:
> say i have an Nx4 array of points and I want to dot every [n, :] 1x4
> slice with a 4x4 matrix.
>
> Currently I am using apply_along_axis in the following manner:
>
> def func(slice, mat):
>     return np.dot(mat, slice)
>
> np.apply_along_axis(func, arr, 1, mat)
>
> Is there a more efficient way of doing this that doesn't require a
> python function for each slice?

I'm sure I'm missing an important point, but can't you solve the whole
problem with one dot:

>> x = np.random.rand(3,4)
>> y = np.random.rand(4,4)

>> np.dot(x, y)

array([[ 0.86488057,  0.23456114,  0.91592677,  0.89798689],
       [ 1.24197754,  0.39907686,  1.45453141,  1.13645076],
       [ 1.41419289,  0.81818818,  1.09768428,  1.32719635]])

>> np.dot(x[0,:], y)
   array([ 0.86488057,  0.23456114,  0.91592677,  0.89798689])
>> np.dot(x[1,:], y)
   array([ 1.24197754,  0.39907686,  1.45453141,  1.13645076])
>> np.dot(x[2,:], y)
   array([ 1.41419289,  0.81818818,  1.09768428,  1.32719635])



More information about the NumPy-Discussion mailing list