Luca
Luca

Reputation: 10996

Python: matrix-vector multiplication with broadcasting

I have a numpy 2x2 matrix defined as follows:

a = np.pi/2
g = np.asarray([[-np.sin(a), -np.cos(a)],
                [ np.cos(a), -np.sin(a)]])

Now, I have numpy array of 2D points that I would like to transform using this matrix. So we can simulate a bunch (25) of 2D points as follows:

p = np.random.rand(25, 2)

How can I do this matrix-vector multiplication for all these 25 points with broadcasting rather than do a for loop?

At the moment, I can do something like:

for i in range(25):
    print np.dot(g, p[i])

This should give me another 2D array with the shape (25, 2).

Is there a more elegant way to do this without the for loop?

Upvotes: 2

Views: 1344

Answers (2)

Anand S Kumar
Anand S Kumar

Reputation: 90899

I think what you want is -

np.dot(p,g.T)

.T is to transpose an array


Example/Demo -

In [1]: import numpy as np

In [2]: a = np.pi/2

In [3]: g = np.asarray([[-np.sin(a), -np.cos(a)],
   ...:                 [ np.cos(a), -np.sin(a)]])

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

In [8]: for i in range(25):
   ...:     print(np.dot(g, p[i]))
   ...:
[-0.56997282 -0.70151323]
[-0.65807814 -0.21773391]
[-0.533987   -0.53936287]
[-0.91982277 -0.01423868]
[-0.96648577 -0.42122831]
[-0.67169383 -0.94959473]
[-0.09013282 -0.57637376]
[-0.03937037 -0.94635173]
[ -2.59523258e-01  -4.04297667e-05]
[-0.77029438 -0.67325988]
[-0.24862373 -0.89806226]
[-0.91866799 -0.07927881]
[-0.83540497 -0.33473515]
[-0.38738641 -0.75406194]
[-0.07569734 -0.66859275]
[-0.72707983 -0.21314985]
[-0.67738699 -0.90763549]
[-0.96172981 -0.68684667]
[-0.40152064 -0.14629421]
[-0.46495457 -0.37456133]
[-0.97915149 -0.0470546 ]
[-0.76488223 -0.70756525]
[-0.21534494 -0.91354898]
[-0.25035908 -0.37841355]
[-0.17990176 -0.18436497]

In [10]: np.dot(p,g.T)
Out[10]:
array([[ -5.69972820e-01,  -7.01513225e-01],
       [ -6.58078138e-01,  -2.17733909e-01],
       [ -5.33987004e-01,  -5.39362872e-01],
       [ -9.19822767e-01,  -1.42386768e-02],
       [ -9.66485769e-01,  -4.21228314e-01],
       [ -6.71693832e-01,  -9.49594730e-01],
       [ -9.01328234e-02,  -5.76373760e-01],
       [ -3.93703749e-02,  -9.46351732e-01],
       [ -2.59523258e-01,  -4.04297667e-05],
       [ -7.70294378e-01,  -6.73259882e-01],
       [ -2.48623728e-01,  -8.98062260e-01],
       [ -9.18667987e-01,  -7.92788080e-02],
       [ -8.35404971e-01,  -3.34735152e-01],
       [ -3.87386412e-01,  -7.54061939e-01],
       [ -7.56973425e-02,  -6.68592746e-01],
       [ -7.27079833e-01,  -2.13149846e-01],
       [ -6.77386988e-01,  -9.07635490e-01],
       [ -9.61729810e-01,  -6.86846673e-01],
       [ -4.01520636e-01,  -1.46294211e-01],
       [ -4.64954574e-01,  -3.74561327e-01],
       [ -9.79151491e-01,  -4.70545953e-02],
       [ -7.64882230e-01,  -7.07565246e-01],
       [ -2.15344940e-01,  -9.13548984e-01],
       [ -2.50359076e-01,  -3.78413552e-01],
       [ -1.79901758e-01,  -1.84364974e-01]])

Upvotes: 4

michaelrccurtis
michaelrccurtis

Reputation: 1172

Try:

np.dot(p, g.T)

which multiplies the points by the transpose of the rotation matrix.

Upvotes: 3

Related Questions