NumPy angle – Returns the angle of a Complex argument

NumPy Angle

Hey, have you ever thought about how the angle of a complex argument can be calculated? Well, that’s where the Python NumPy library comes into play. With a wide variety of functions available in the NumPy library, there is a function NumPy angle that does the task of calculating the angle.

We will be going through its syntax and different types of examples that will make you familiar with the function.

So, without any further due let’s get started.

About NumPy angle

You all must be familiar with Complex Numbers, right? Let’s quickly revise Complex Numbers.

Complex Numbers are composed of two components first one is the real part and the second one is the imaginary part. When the complex number is plotted on the Complex Plane the real and imaginary parts lie on the axes that are perpendicular to each other (just like the X-axis and Y-axis).

Now, the angle of the Complex Number is the angle between the Complex Number and the Real axis. That’s where the NumPy angle function comes into play.

Note: A complex number z is represented as z = x+yi where x and y are real numbers. The angle of the complex number z is given by tan-1(y/x).

numpy.angle() is one of the mathematical functions of the NumPy library that returns the angle of the Complex Number.

Also read: NumPy Arctan – A Complete Guide

Now, let us understand the syntax of the function.

Syntax of NumPy angle

numpy.angle(z , deg = "")
  • The first argument is the complex number. It can be a single complex number as well as a NumPy array of Complex Numbers.
  • The second argument is the deg value which is of the type bool (true or false). By default, the function returns the angle in radians. To get the angle in degrees the deg value is set to true.

That was about the theory and syntax of the function.

Working with NumPy angle

Let’s write some codes to understand more about the function.

NumPy angle of Single complex number

import numpy as np

print("The angle of the complex number 1+3j is:",np.angle(1+3j),"radians")

print("The angle of the complex number 3j is:",np.angle(3j),"radians")

print("The angle of the complex number 1 is:",np.angle(1),"radians")

print("The angle of the complex number -1-1j is:",np.angle(-1-1j),"radians")

Output

The angle of the complex number 1+3j is: 1.2490457723982544 radians
The angle of the complex number 3j is: 1.5707963267948966 radians
The angle of the complex number 1 is: 0.0 radians
The angle of the complex number -1-1j is: -2.356194490192345 radians

In all the above examples, we have passed a single complex number as an argument to the function np.angle() that calculates the angle of the input complex number. The output angle is in radians.

Let us understand how the output angle is calculated for the complex numbers 3j and 1. Taking the complex number 3j, the real part is 0 and the imaginary part is 3 which implies that in tan-1(y/x), y is 3 and x is 0. This implies tan-1(y/x) equals tan-1(infinity) whose value is 90 degrees or 1.5707 radians.

For complex number 1, the real part is 1 and the imaginary part is 0 which implies that in tan-1(y/x), y is 0 and x is 1. This implies tan-1(y/x) equals tan-1(0) whose value is 0 radians.

Our mathematical calculations and the output of the program exactly matches. How cool is that 🙂

Note: Always plot the complex number on the complex plane to get a clear idea about the angle made by the complex number and the real axis. It will be really helpful to you.

NumPy angle of a NumPy array of complex numbers

import numpy as np

a = np.array((1+3j , -1+0.5j , 4-2j , 0.5+0.5j))

b = np.angle(a)

print("Input Array of Complex Numbers:\n",a)
print("Angles in radians:\n",b)

Output

Input Array of Complex Numbers:
 [ 1. +3.j  -1. +0.5j  4. -2.j   0.5+0.5j]
Angles in radians:
 [ 1.24904577  2.67794504 -0.46364761  0.78539816]

Note: The output array has the same dimensions and shape as the input NumPy array.

In the above examples, a NumPy array of Complex Numbers is passed as an argument to the function. Here also the output angles of all the complex numbers are in radians.

Here also the function works in a similar way. The angle of each of the complex elements of the input NumPy array is calculated and stored in the variable b in the above program. Then, we used two print statements to print the input NumPy array and the NumPy array of angles.

But, can we get the output angle in degrees? Let’s see how can that be done 🙂

NumPy angle with deg attribute

import numpy as np

a = np.array((1+3j, -1j, 0.5+0.5j))

b = np.angle(a , deg="true")

print("Input Array:\n",a)
print("Angle in degrees:\n",b)

Output

Input Array:
 [ 1. +3.j  -0. -1.j   0.5+0.5j]
Angle in degrees:
 [ 71.56505118 -90.          45.        ]

So, we get the output angle in degrees if we set the deg value to true.

Rest all the things are similar to the previous codes. There is one task for you all, you have to calculate the output angle of single complex numbers in degrees.

Summary

In this article, we learned about the NumPy angle function. We practiced the codes for a single complex number as well as a NumPy array of complex numbers and we also used the deg attribute to convert the output angle into degrees. Do write the codes along with going through the articles.

Stay tuned and keep exploring more articles here.

Reference

NumPy Documentation – NumPy angle