NumPy degrees(), deg2rad(), rad2deg() and radians() Functions

NumPy Mathematical Functions

Hello Readers! Welcome to another tutorial on NumPy Functions. In this tutorial, we will understand three important functions i.e. numpy.degrees(), numpy.deg2rad(), numpy.rad2deg() and numpy.radians() in detail.

So, let’s get started.

1. numpy.degrees()

numpy.degrees() is a function of the NumPy Library that converts the angle from radians to degrees.

Syntax: numpy.degrees(input) where input can be a single angle in radians or a NumPy array of angles in radians.

Working with numpy.degrees()

Let’s try out some examples for better understanding.

Using numpy.degrees() with a single angle as an input

import numpy as np

# Converting pi/6 radians to equivalent angle in degrees
print("PI/6 radians is ",np.degrees(np.pi/6),"degrees \n")

# Converting pi/4 radians to equivalent angle in degrees
print("PI/4 radians is ",np.degrees(np.pi/4),"degrees \n")

# Converting pi/3 radians to equivalent angle in degrees
print("PI/3 radians is ",np.degrees(np.pi/3),"degrees \n")

# Converting pi/2 radians to equivalent angle in degrees
print("PI/2 radians is ",np.degrees(np.pi/2),"degrees \n")

# Converting pi radians to equivalent angle in degrees
print("PI radians is ",np.degrees(np.pi),"degrees ")

Output

PI/6 radians is  29.999999999999996 degrees 

PI/4 radians is  45.0 degrees

PI/3 radians is  59.99999999999999 degrees

PI/2 radians is  90.0 degrees

PI radians is  180.0 degrees

The above snippet is pretty much clear where a single angle(in radians) is passed as an argument to the np.degrees() function.

Using numpy.degrees() with the NumPy Array

import numpy as np

a = np.array((np.pi/3 , np.pi/2 , np.pi/4 , np.pi))
b = np.degrees(a)

print("Angles in radians: \n",a)

print("Corresponding angles in degrees: \n",b)

Output

Angles in radians: 
 [1.04719755 1.57079633 0.78539816 3.14159265]
Corresponding angles in degrees:
 [ 60.  90.  45. 180.]

A NumPy Array is created and assigned to the variable a in the above snippet. All the angles are in radians in this array, which is passed to the np.degrees() function.

2. numpy.deg2rad()

numpy.deg2rad() is a mathematical function that converts the angle in degrees to angles in radians.

Syntax: numpy.deg2rad(input) where input can be a single angle in degrees or a NumPy array of angles in degrees.

Working with numpy.deg2rad() function

Let’s try out a few degree 2 radians examples now.

Using numpy.deg2rad() with a single angle as an input

import numpy as np

print("30 degrees is equal to ",np.deg2rad(30),"radians\n")

print("45 degrees is equal to ",np.deg2rad(45),"radians\n")

print("60 degrees is equal to ",np.deg2rad(60),"radians\n")

print("90 degrees is equal to ",np.deg2rad(90),"radians\n")

print("360 degrees is equal to ",np.deg2rad(360),"radians")

Output

30 degrees is equal to  0.5235987755982988 radians

45 degrees is equal to  0.7853981633974483 radians

60 degrees is equal to  1.0471975511965976 radians

90 degrees is equal to  1.5707963267948966 radians

360 degrees is equal to  6.283185307179586 radians

Using numpy.deg2rad() with a NumPy Array

import numpy as np

a = np.array((30 , 45 , 60 , 90 , 180 , 270 , 360))

b = np.deg2rad(a)

print("Angles in Degrees :\n",a)

print("Angles in Radians :\n",b)

Output

Angles in Degrees :
 [ 30  45  60  90 180 270 360]
Angles in Radians :
 [0.52359878 0.78539816 1.04719755 1.57079633 3.14159265 4.71238898
 6.28318531]

3. numpy.rad2deg()

numpy.rad2deg() function of the NumPy library is equivalent to numpy.degrees() function. It converts the value of the angle in radians to the angle in degrees.

Syntax: numpy.rad2deg(input) where input can be a single angle in radians or a NumPy array of angles in radians.

Let’s try some examples to understand it better.

Working with numpy.rad2deg()

Now let’s move to radians to degrees function of Numpy.

Using numpy.rad2deg() with a NumPy Array

import numpy as np

a = np.array((-np.pi , np.pi/2 , np.pi/3 , np.pi/4 , np.pi))

b = np.rad2deg(a)

print("Angles in Radians:\n",a)

print("Angles in Degrees:\n",b)

Output

Angles in Radians:
 [-3.14159265  1.57079633  1.04719755  0.78539816  3.14159265]
Angles in Degrees:
 [-180.   90.   60.   45.  180.]

4. numpy.radians()

numpy.radians() is also one of the mathematical functions that convert the angles from degrees to radians.

Syntax: numpy.radians(input) where input can be a single angle or a NumPy array of angles in degrees.

Working with numpy.radians()

Let’s finally take a look at the radians function of numpy.

Using numpy.radians() with a NumPy Array

import numpy as np

a = np.array((60 , 90 , 45 , 180))
b = np.radians(a)

print("Angles in degrees: \n",a)

print("Corresponding angles in radians: \n",b)

Output

Angles in degrees: 
 [ 60  90  45 180]
Corresponding angles in radians: 
 [1.04719755 1.57079633 0.78539816 3.14159265]

That was all about NumPy degrees(), deg2rad(), rad2deg() and radians() functions. These functions are really simple to use and easy to understand. Do read this article twice and practice the codes along with reading the article. There is a task for you all, you have to use the numpy.rad2deg() and numpy.radians() function with single inputs.

Reference