Understanding numpy.deg2rad() and numpy.rad2deg() functions (4 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Introduction

Numpy is a fundamental package for scientific computing in Python, offering a powerful N-dimensional array object among other functionalities. A common use case in fields such as physics, engineering, and computer graphics is converting angles from degrees to radians and vice versa. For these tasks, Numpy provides two convenient functions: numpy.deg2rad() and numpy.rad2deg(). Understanding how to use these functions can simplify your calculations and make your code more readable. In this tutorial, we’ll explore these two functions through four examples, each progressing in complexity.

Basic Usage

First, let’s look at the most straightforward application of these functions. Make sure you have Numpy installed in your environment; if not, you can install it using pip:

pip install numpy

Now, let’s convert an angle from degrees to radians and vice versa:

import numpy as np

# Convert degrees to radians
degree_angle = 90
rad = np.deg2rad(degree_angle)
print(f'90 degrees in radians is: {rad}')

# Convert radians to degrees
rad_angle = np.pi / 2
deg = np.rad2deg(rad_angle)
print(f'Ï€/2 radians in degrees is: {deg}')

Output:

90 degrees in radians is: 1.5707963267948966
Ï€/2 radians in degrees is: 90.0

Working with Arrays

Numpy’s true power comes when working with arrays. Both deg2rad() and rad2deg() can operate on entire arrays of angles:

angles_deg = np.array([0, 30, 45, 60, 90])
angles_rad = np.deg2rad(angles_deg)
print('Degrees to radians:', angles_rad)

angles_rad = np.array([0, np.pi/6, np.pi/4, np.pi/3, np.pi/2])
angles_deg = np.rad2deg(angles_rad)
print('Radians to degrees:', angles_deg)

Output:

Degrees to radians: [0. 0.52359878 0.78539816 1.04719755 1.57079633]
Radians to degrees: [ 0. 30. 45. 60. 90.]

Applying to Mathematical Functions

These conversions become even more crucial when applying mathematical functions that expect angles in radians, such as np.sin() or np.cos(). Here’s how you can use deg2rad() for accurate calculations:

angles_deg = np.array([0, 30, 45, 60, 90])
angles_rad = np.deg2rad(angles_deg)
sines = np.sin(angles_rad)
print('Sine of angles:', sines)

Output:

Sine of angles: [0. 0.5 0.70710678 0.8660254 1. ]

Visualizing Conversions

Lastly, let’s use a more advanced example involving visualization. Assuming you’re familiar with matplotlib, the following code snippet shows how to plot the conversion of angles from degrees to radians:

import matplotlib.pyplot as plt

angles_deg = np.arange(0, 360, 15)
angles_rad = np.deg2rad(angles_deg)

plt.plot(angles_deg, angles_rad, 'bo-')
plt.xlabel('Degrees')
plt.ylabel('Radians')
plt.title('Degrees to Radians Conversion')
plt.show()

This visualization not only reinforces understanding but also serves as a clear demonstration of the linear relationship between degrees and radians.

Conclusion

Understanding the numpy.deg2rad() and numpy.rad2deg() functions is crucial for efficiently converting between degrees and radians in mathematical calculations and visual representations. These functions simplify the conversion process, make your code more readable, and enable direct operations on arrays. Whether you’re a student, researcher, or developer, mastering these conversions will undoubtedly enhance your numerical computing tasks.