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

Kurt Smith kwmsmith@gmail....
Thu Jul 9 22:45:08 CDT 2009

```On Thu, Jul 9, 2009 at 9:36 PM, Chris Colbert<sccolbert@gmail.com> wrote:
> no, because dot(x,y) != dot(y,x)
>

Try this, then:

In [24]: x = np.random.rand(10,4)

In [25]: y = np.random.rand(4,4)

In [26]: result = np.dot(y,x.T).T

In [39]: for i, res in enumerate(result):
....:     assert np.allclose(res, np.dot(y,x[i,:]))
....:
....:

In [40]:

>>>> x = np.random.rand(3,4)
>>>> y = np.random.rand(4,4)
>>>> np.dot(x, y)
> array([[ 1.67624043,  1.66719374,  1.72465017,  1.20372021],
>       [ 0.70046162,  0.60187869,  0.73094349,  0.4604766 ],
>       [ 0.78707401,  1.01959666,  0.61617829,  0.43147398]])
>>>> np.dot(y, x[0,:
> ... ])
> array([ 1.44627767,  1.09332339,  2.66001046,  1.13972652])
>>>> np.dot(y, x[1,:])
> array([ 0.27854715,  0.56261048,  0.7793413 ,  0.44260709])
>>>> np.dot(y, x[2,:])
> array([ 0.70468211,  0.42843143,  1.34022702,  0.53021987])
>>>>
>
>
> hence I need xnew = [Transform]*[xold]
>
> and not [xold]*[Transform]
>
>
>
>
> On Thu, Jul 9, 2009 at 10:22 PM, Keith Goodman<kwgoodman@gmail.com> wrote:
>> On Thu, Jul 9, 2009 at 7:08 PM, Chris Colbert<sccolbert@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])
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
```