简体   繁体   中英

Python Numpy arctan2 asterisk and .T meaning

I am trying to understand Python numpy arctan definition.

What does the

  1. asterisk after arctan2 represent?
  2. and what does.T mean?

Below we are finding the angles between a set of contour points and a center.

Just trying to understand these syntax.

https://numpy.org/doc/stable/reference/generated/numpy.arctan2.html

angles = np.rad2deg(np.arctan2(*(np.tile(center, (len(pts), 1)) - pts).T))

Go through:

  1. Asteriks in Python to understand how the unpacking of args happen.
  2. numpy.Transpose

Taking a guess as to what pts and center might look like:

In [322]: pts = np.array([[0,0],[1,1],[1,2],[2,1]])
In [323]: center = np.array([.5,.5])

The inner part of the expression is then:

In [324]: np.tile(center, (len(pts), 1)) - pts
Out[324]: 
array([[ 0.5,  0.5],
       [-0.5, -0.5],
       [-0.5, -1.5],
       [-1.5, -0.5]])

Taking the transpose, and unpacking:

In [325]: x, y = (np.tile(center, (len(pts), 1)) - pts).T
In [326]: x
Out[326]: array([ 0.5, -0.5, -0.5, -1.5])
In [327]: y
Out[327]: array([ 0.5, -0.5, -1.5, -0.5])

So the full expression is:

In [328]: np.arctan2(*(np.tile(center, (len(pts), 1)) - pts).T)
Out[328]: array([ 0.78539816, -2.35619449, -2.8198421 , -1.89254688])

and the same as:

In [329]: np.arctan2(x,y)
Out[329]: array([ 0.78539816, -2.35619449, -2.8198421 , -1.89254688])

But we don't need tile to subtract the pts from center :

In [333]: center-pts
Out[333]: 
array([[ 0.5,  0.5],
       [-0.5, -0.5],
       [-0.5, -1.5],
       [-1.5, -0.5]])

center is (2,) shape, pts is (n,2); by broadcasting they subtract.

So perhaps a clearer way of writing this is:

In [341]: temp = center-pts
In [343]: np.arctan2(temp[:,0],temp[:,1])
Out[343]: array([ 0.78539816, -2.35619449, -2.8198421 , -1.89254688])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM