Numpy example for arctan2 perhaps wrong/misleading/confusing?
Hi all, the page http://www.scipy.org/Numpy_Example_List_With_Doc#head-ad8dc60988f26f59ebe565... describes the numpy.arctan2 function and gives an example:
arctan2(array([0, 1]), array([1, 0]))
However in the example there are used two arrays as arguments to arctan2, where it should probably be the x and y component of a single 2D vector. What arctan2(x,y) returns is the angle between the 2D vector (x,y) and the x axis. The example made me assume that it can compute the angle between two vectors, which it does obviously not. Wikipedia has a nice article on atan2. I think arctan2 works the same, or? http://en.wikipedia.org/wiki/Atan2 What could be the reason of using arrays in arctan2? Could someone update the documentation? Also help(numpy.arctan2) is not very helpful, to be honest. cheers, Samuel
Hi Samuel 2008/6/9 Samuel John <scipy@samueljohn.de>:
Hi all,
the page
http://www.scipy.org/Numpy_Example_List_With_Doc#head-ad8dc60988f26f59ebe565...
describes the numpy.arctan2 function and gives an example:
arctan2(array([0, 1]), array([1, 0]))
However in the example there are used two arrays as arguments to arctan2, where it should probably be the x and y component of a single 2D vector. What arctan2(x,y) returns is the angle between the 2D vector (x,y) and the x axis. The example made me assume that it can compute the angle between two vectors, which it does obviously not.
Wikipedia has a nice article on atan2. I think arctan2 works the same, or? http://en.wikipedia.org/wiki/Atan2
What could be the reason of using arrays in arctan2? Could someone update the documentation? Also help(numpy.arctan2) is not very helpful, to be honest.
You're right, it is not. Would you like to improve it? Please register an account for yourself on our documentation wiki: http://sd-2116.dedibox.fr/pydocweb/doc/numpy.core.umath.arctan2/ I'll then give you permission to edit docstrings. Regards Stéfan
Hi Samuel, One thing that you should know is that all "ufunc" (universal functions) are defined as functions operating on scalars. Numpy then wraps these functions such that they can process ndarrays of any shape element by element. This is what is explained in the help(arctan2) documentation. As for the example, maybe a way to avoid confusion would be to give an example with scalar arguments first to show the basic behavior, and then an example using ndarray broadcasting. HTH, David 2008/6/9 Samuel John <scipy@samueljohn.de>:
Hi all,
the page
http://www.scipy.org/Numpy_Example_List_With_Doc#head-ad8dc60988f26f59ebe565...
describes the numpy.arctan2 function and gives an example:
arctan2(array([0, 1]), array([1, 0]))
However in the example there are used two arrays as arguments to arctan2, where it should probably be the x and y component of a single 2D vector. What arctan2(x,y) returns is the angle between the 2D vector (x,y) and the x axis. The example made me assume that it can compute the angle between two vectors, which it does obviously not.
Wikipedia has a nice article on atan2. I think arctan2 works the same, or? http://en.wikipedia.org/wiki/Atan2
What could be the reason of using arrays in arctan2? Could someone update the documentation? Also help(numpy.arctan2) is not very helpful, to be honest.
cheers, Samuel _______________________________________________ Scipy-dev mailing list Scipy-dev@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-dev
Hi David! On Mon, Jun 9, 2008 at 3:56 PM, David Huard <david.huard@gmail.com> wrote:
One thing that you should know is that all "ufunc" (universal functions) are defined as functions operating on scalars. Numpy then wraps these functions such that they can process ndarrays of any shape element by element. This is what is explained in the help(arctan2) documentation.
I see the point for arctan and other functions with a single argument, but not for arctan2, especially. It needs two arguments and if I want to compute the result of the vectors a=array([a1,a2]) and b=array([b1,b2]) like arctan2(a,b) what I get is are the two results as if I would have written: array([ artcan2(a1,b1), arctan2(a2,b2) ]) #Note, this is NOT (a1,a2) or (b1,b2) !! If you know this and distribute your vector entries over two arrays, the broadcasting may be very useful. (As it is for many other functions!) It would be perhaps nice to call arctan2 with just ONE array argument and have a broadcasting like this: arctan2( array([[a1,a2],[b1,b2],[c1,c2]]) ) --> array([arctan2(a1,a2), arctan2(b1,b2), arctan2(c1,c2) ] ) and arctan2( array([x,y]) ) --> arctan2(x,y)
As for the example, maybe a way to avoid confusion would be to give an example with scalar arguments first to show the basic behavior, and then an example using ndarray broadcasting.
I would argue to show just the scalar version in the example and leave out the broadcasting version, because it makes less sense here, doesn't it? Is there any fancy matrix-or-whatever notation, that really makes up a good example for broadcasting with arctan2? cheers, Samuel PS: I am not against the current broadcasting scheme. It is perfectly ok.
On Mon, Jun 9, 2008 at 09:56, Samuel John <scipy@samueljohn.de> wrote:
Hi David!
On Mon, Jun 9, 2008 at 3:56 PM, David Huard <david.huard@gmail.com> wrote:
One thing that you should know is that all "ufunc" (universal functions) are defined as functions operating on scalars. Numpy then wraps these functions such that they can process ndarrays of any shape element by element. This is what is explained in the help(arctan2) documentation.
I see the point for arctan and other functions with a single argument, but not for arctan2, especially. It needs two arguments and if I want to compute the result of the vectors a=array([a1,a2]) and b=array([b1,b2]) like
arctan2(a,b)
what I get is are the two results as if I would have written:
array([ artcan2(a1,b1), arctan2(a2,b2) ]) #Note, this is NOT (a1,a2) or (b1,b2) !!
If you know this and distribute your vector entries over two arrays, the broadcasting may be very useful. (As it is for many other functions!)
This is just something you have to know about ufuncs. Ufuncs simply don't do (a1,a2). Note also, that this is not actually broadcasting (which describes what happens when you have arrays that don't have the same dimensions).
It would be perhaps nice to call arctan2 with just ONE array argument and have a broadcasting like this: arctan2( array([[a1,a2],[b1,b2],[c1,c2]]) ) --> array([arctan2(a1,a2), arctan2(b1,b2), arctan2(c1,c2) ] )
and arctan2( array([x,y]) ) --> arctan2(x,y)
No, sorry, that's not going to happen.
As for the example, maybe a way to avoid confusion would be to give an example with scalar arguments first to show the basic behavior, and then an example using ndarray broadcasting.
I would argue to show just the scalar version in the example and leave out the broadcasting version, because it makes less sense here, doesn't it?
There is no difference between this ufunc and any other.
Is there any fancy matrix-or-whatever notation, that really makes up a good example for broadcasting with arctan2?
I think an array example showing the results for each of the four quadrants would be ideal in order to show why you would use this over arctan(). -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
Hi Robert! On Mon, Jun 9, 2008 at 5:27 PM, Robert Kern <robert.kern@gmail.com> wrote:
This is just something you have to know about ufuncs. Ufuncs simply don't do (a1,a2).
I agree, ufuncs are fine the way they are right now. Its just the example for arctan2 that confused me.
It would be perhaps nice to call arctan2 with just ONE array argument and have a broadcasting like this: arctan2( array([[a1,a2],[b1,b2],[c1,c2]]) ) --> array([arctan2(a1,a2), arctan2(b1,b2), arctan2(c1,c2) ] )
and arctan2( array([x,y]) ) --> arctan2(x,y)
No, sorry, that's not going to happen.
I know :-)
There is no difference between this ufunc and any other.
Hopefully not!
I think an array example showing the results for each of the four quadrants would be ideal in order to show why you would use this over arctan().
Yes, this would be best! But I can't think of a vectorized version with arrays. greetings, Samuel
and arctan2( array([x,y]) ) --> arctan2(x,y)
No, sorry, that's not going to happen.
Another way to put that is that we are not going to do anything special to remove the need for a '*' arctan2(*array([x,y])) is already equivalent to arctan2(x,y) -Travis
Hi Travis!
Another way to put that is that we are not going to do anything special to remove the need for a '*' arctan2(*array([x,y])) is already equivalent to arctan2(x,y)
I was not aware of the '*' operator in front of the array, since I am new to python/scipy/numpy. Indeed, this is a good thing to have! David, On Mon, Jun 9, 2008 at 8:08 PM, David Huard <david.huard@gmail.com> wrote:
What version of Numpy are you using ?
I am using the lates svn version from 2 weeks ago or so. I installed it with the ScipySuperPack installer on a Mac. Best Regards, Samuel
Samuel, What you want is this :
[4]: def arctan2(vectors): ...: """Return the angle between the x axis and each vector in vectors.""" ...: v = np.atleast_2d(vectors) ...: return np.arctan2(v[:,0], v[:,1]) ...:
[5]: arctan2(([0,0], [1,0])) < [5]: array([ 0. , 1.57079633])
You could also use the numpy.angle function which returns the angle of a complex argument.
[15]: np.angle([0, 1j]) <[15]: array([ 0. , 1.57079633])
David 2008/6/9 Samuel John <scipy@samueljohn.de>:
Hi David!
On Mon, Jun 9, 2008 at 3:56 PM, David Huard <david.huard@gmail.com> wrote:
One thing that you should know is that all "ufunc" (universal functions) are defined as functions operating on scalars. Numpy then wraps these functions such that they can process ndarrays of any shape element by element. This is what is explained in the help(arctan2) documentation.
I see the point for arctan and other functions with a single argument, but not for arctan2, especially. It needs two arguments and if I want to compute the result of the vectors a=array([a1,a2]) and b=array([b1,b2]) like
arctan2(a,b)
what I get is are the two results as if I would have written:
array([ artcan2(a1,b1), arctan2(a2,b2) ]) #Note, this is NOT (a1,a2) or (b1,b2) !!
If you know this and distribute your vector entries over two arrays, the broadcasting may be very useful. (As it is for many other functions!)
It would be perhaps nice to call arctan2 with just ONE array argument and have a broadcasting like this: arctan2( array([[a1,a2],[b1,b2],[c1,c2]]) ) --> array([arctan2(a1,a2), arctan2(b1,b2), arctan2(c1,c2) ] )
and arctan2( array([x,y]) ) --> arctan2(x,y)
As for the example, maybe a way to avoid confusion would be to give an example with scalar arguments first to show the basic behavior, and then an example using ndarray broadcasting.
I would argue to show just the scalar version in the example and leave out the broadcasting version, because it makes less sense here, doesn't it? Is there any fancy matrix-or-whatever notation, that really makes up a good example for broadcasting with arctan2?
cheers, Samuel
PS: I am not against the current broadcasting scheme. It is perfectly ok. _______________________________________________ Scipy-dev mailing list Scipy-dev@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-dev
Hi!
What you want is this :
[4]: def arctan2(vectors): ...: """Return the angle between the x axis and each vector in vectors.""" ...: v = np.atleast_2d(vectors) ...: return np.arctan2(v[:,0], v[:,1]) ...:
[5]: arctan2(([0,0], [1,0])) < [5]: array([ 0. , 1.57079633])
Indeed, very nice.
You could also use the numpy.angle function which returns the angle of a complex argument.
[15]: np.angle([0, 1j]) <[15]: array([ 0. , 1.57079633])
Cool, I was not aware of this function. Perfect. Unfortunately there is no docstring *g*... -- Samuel
2008/6/9 Samuel John <scipy@samueljohn.de>:
Hi!
What you want is this :
[4]: def arctan2(vectors): ...: """Return the angle between the x axis and each vector in vectors.""" ...: v = np.atleast_2d(vectors) ...: return np.arctan2(v[:,0], v[:,1]) ...:
[5]: arctan2(([0,0], [1,0])) < [5]: array([ 0. , 1.57079633])
Indeed, very nice.
You could also use the numpy.angle function which returns the angle of a complex argument.
[15]: np.angle([0, 1j]) <[15]: array([ 0. , 1.57079633])
Cool, I was not aware of this function. Perfect. Unfortunately there is no docstring *g*...
What version of Numpy are you using ? Here is what I have: Return the angle of the complex argument z. Examples -------- >>> numpy.angle(1+1j) # in radians 0.78539816339744828 >>> numpy.angle(1+1j,deg=True) # in degrees 45.0
-- Samuel _______________________________________________ Scipy-dev mailing list Scipy-dev@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-dev
2008/6/9 David Huard <david.huard@gmail.com>:
Samuel,
What you want is this :
[4]: def arctan2(vectors): ...: """Return the angle between the x axis and each vector in vectors.""" ...: v = np.atleast_2d(vectors) ...: return np.arctan2(v[:,0], v[:,1]) ...:
[5]: arctan2(([0,0], [1,0])) < [5]: array([ 0. , 1.57079633])
You could also use the numpy.angle function which returns the angle of a complex argument.
[15]: np.angle([0, 1j]) <[15]: array([ 0. , 1.57079633])
Just a detail, but it would probably be better to write np.arctan2(v[...,0], v[...,1]) (and then drop the atleast_2d). This then works for arbitrarily-dimensioned arrays. Anne
2008/6/10 Anne Archibald <peridot.faceted@gmail.com>:
2008/6/9 David Huard <david.huard@gmail.com>:
Samuel,
What you want is this :
[4]: def arctan2(vectors): ...: """Return the angle between the x axis and each vector in vectors.""" ...: v = np.atleast_2d(vectors) ...: return np.arctan2(v[:,0], v[:,1]) ...:
[5]: arctan2(([0,0], [1,0])) < [5]: array([ 0. , 1.57079633])
You could also use the numpy.angle function which returns the angle of a complex argument.
[15]: np.angle([0, 1j]) <[15]: array([ 0. , 1.57079633])
Just a detail, but it would probably be better to write np.arctan2(v[...,0], v[...,1]) (and then drop the atleast_2d). This then works for arbitrarily-dimensioned arrays.
Nice. Thanks for the pointer, David
Anne _______________________________________________ Scipy-dev mailing list Scipy-dev@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-dev
On Jun 9, 2008, at 10:56 AM, Samuel John wrote:
I would argue to show just the scalar version in the example and leave out the broadcasting version, because it makes less sense here, doesn't it?
No. Perhaps, though, the example should not use two-element arrays, to avoid any ambiguity in interpretation.
Is there any fancy matrix-or-whatever notation, that really makes up a good example for broadcasting with arctan2?
We use this all the time to calculate fields of angles from fields of vector components. It's not a matrix thing. It's a field thing. It makes just as much (or as little) sense as sin(array([a1, a2, a3, ...])). It works like everything else. Please leave it alone.
Hi there! On Mon, Jun 9, 2008 at 5:47 PM, Jonathan Guyer <guyer@nist.gov> wrote:
On Jun 9, 2008, at 10:56 AM, Samuel John wrote:
I would argue to show just the scalar version in the example and leave out the broadcasting version, because it makes less sense here, doesn't it?
No. Perhaps, though, the example should not use two-element arrays, to avoid any ambiguity in interpretation.
However, I would prefer an example with scalars (first) but only for arctan2... not the other examples.
Is there any fancy matrix-or-whatever notation, that really makes up a good example for broadcasting with arctan2?
We use this all the time to calculate fields of angles from fields of vector components. It's not a matrix thing. It's a field thing. It makes just as much (or as little) sense as sin(array([a1, a2, a3, ...])).
ok, fine ...
It works like everything else. Please leave it alone.
No one want to change anything here :-) Perhaps just one example :-P cheers, Samuel
participants (7)
-
Anne Archibald -
David Huard -
Jonathan Guyer -
Robert Kern -
Samuel John -
Stéfan van der Walt -
Travis E. Oliphant