Reputation: 1073
I am trying to understand Python numpy arctan definition.
What does the
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))
Upvotes: 0
Views: 119
Reputation: 231325
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])
Upvotes: 1
Reputation: 34046
Go through:
Asteriks in Python
to understand how the unpacking of args happen.numpy.Transpose
Upvotes: 1