In this article, we are going to see how to return the angle of the complex argument in radians in Python Using NumPy.
The NumPy library provides the numpy.angle() method to return the angle of the complex argument in radians in python. Let’s look at the syntax of the method.
Syntax: numpy.angle(z, deg=False)
parameters:
- z: array like object. input. A complex number or a series of complex numbers.
- deg:default value, boolean. If True, return the angle in degrees; if False, return the angle in radians by default.
Return:
- angle: The complex plane’s counterclockwise angle from the positive real axis in the range (-pi, pi), with dtype numpy.float64.
Example 1:
In this example, we created an array of complex numbers using the np.array() method. The shape of the array is defined by the .shape attribute and the dimension of the array is defined by .ndim , the datatype of the array is returned by .dtype attribute. The angle of the complex numbers is returned in radians using the np.angle() method.
Python3
# import package import numpy as np # Creating an array of complex numbers array = np.array([ 10.0 , 20 + 1.0j , 30 + 40j ]) print (array) # shape of the array is print ( "Shape of the array is : " , array.shape) # dimension of the array print ( "The dimension of the array is : " , array.ndim) # Datatype of the array print ( "Datatype of our Array is : " , array.dtype) # angle of the complex argument in radians print ( "Angle in radians : " , np.angle(array)) |
Output:
[10. +0.j 20. +1.j 30.+40.j] Shape of the array is : (3,) The dimension of the array is : 1 Datatype of our Array is : complex128 Angle in radians : [0. 0.0499584 0.92729522]
Example 2:
In case we want the angle to be returned in degrees, deg parameter should be set to True.
Python3
# import package import numpy as np # Creating an array of complex numbers array = np.array([ 10.0 , 20 + 1.0j , 30 + 40j ]) print (array) # shape of the array is print ( "Shape of the array is : " , array.shape) # dimension of the array print ( "The dimension of the array is : " , array.ndim) # Datatype of the array print ( "Datatype of our Array is : " , array.dtype) # angle of the complex argument in radians print ( "Angle in radians : " , np.angle(array)) |
Output:
[10. +0.j 20. +1.j 30.+40.j] Shape of the array is : (3,) The dimension of the array is : 1 Datatype of our Array is : complex128 Angle in degrees : [ 0. 2.86240523 53.13010235]