Issue
Suppose to have two complex numbers using Python and NumPy (1.20.1):
a = 5 + 1j*3
a0 = 4 + 1j*2
And I want to calculate the phase shift, aka the distance between the two angles. I am getting two slightly different results:
>>> np.angle(a*np.conjugate(a0))
0.07677189126977804
>>> np.angle(a) - np.angle(a0)
0.07677189126977807
I guess the most correct way should be the first. In some cases the difference is bigger, in others there is none.
Does anyone know the origin of this difference?
Cheers.
EDIT I've found a more relevant example:
>>> a = 41.887609743111966+3.868827773225067j
>>> a0 = -65.06495257694792-0.19335140606773393j
>>> np.angle(a) - np.angle(a0)
3.2307217955357035
>>> np.angle(a*np.conjugate(a0))
-3.0524635116438827
Solution
The first example is just due to numerical imprecision inherent in doing floating point calculations; performing these operations in different order leads to different round offs that result in them being represented by (very slightly) different floating point values. The difference in value between the two is negligible for most applications.
However, as your second example shows, these two expressions are not equivalent. np.angle
returns a value from -pi to pi, which is important when the difference in angle is larger than than that. When you take a difference between two angles, you can get a value outside this range, which is what happens in the first snippet. The second snippet where the result comes directly from np.angle
has to be in the range -pi to pi. The difference between these your two results is simply 2pi.
So if you wanted to determine the absolute angle between two points, you would use your first formula. If you just wanted to determine the relative phase between -pi and pi, you would use the second.
Answered By - Tyberius
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.