Converting Degrees to Radians Using Numpy in Python

Numpy Deg2rad

Geometry is a fascinating subject to be engaged with. All those shapes and figures possess underpinnings that are mysterious to the naked eye which however can be stripped down using Mathematics. It is called the Queen of all Sciences for a reason. This article would be dealing with one such property that is associated with geometry – the angles!

Like any metric, the angle can also be measured using a specific unit of measurement. A recall of the good old school days would help us remember the time when we used the protractor to draw angled shapes & what would be engraved on the protractor to help us draw the angle. Yep, it is the degrees!

Just like miles is to kilometers, degrees also have a synonymous metric that can be used to measure the angle – the radians! Having its foundation in the irrational number pi (π), radians are used to find the arc length produced by an angle whilst degrees give us the rotation produced by an angle.

Different Ways to Convert Degrees to Radians using Numpy

Now that we have an overview of what is what, let us jump straight into the programming part of it. We shall get started by importing the NumPy library within Python using the following code.

import numpy as np

Following are the different techniques that can be put into use for converting degrees to radians using Python.

  • Using numpy.radians( )
  • Using numpy.deg2rad( )
  • Using pi (π)

Technique I – Using numpy.radians( )

One can convert any given angle in degrees either as a single entity or as an array using the radians( ) function from the numpy library. Let us first try converting a single entity of 180 degrees using the below code.

np.radians(180)
Converting Single Entity Using Numpy Radians
Converting Single Entity Using Numpy Radians

From the above, it can be observed that the value returned 3.141592… is that of pi (π) since 180 degrees is the same as pi radians.


Technique II – Using numpy.deg2rad( )

While one can also convert a single entity using the deg2rad( ) function from the numpy library, the same can also be done for an array. So we shall demonstrate the conversion of an array of degree values into radians using deg2rad( ) function. It is true for the above technique too and the procedure is the same as given below.

ar1 = [[10, 50, 120],
       [150, 180, 266]]
np.deg2rad(ar1)
Converting An Array Of Degree Values
Converting An Array Of Degree Values

Technique III – Using pi (π)

This technique makes use of the underlying principle based on which the radians are being converted from the degrees; the very mathematical foundation that all software including Python makes use of. Some things are better shown than told, so here it is, the formula for converting degrees to radians.

Formula For Degrees To Radians Conversion
Formula For Degrees To Radians Conversion

It has been stated in the previous technique that π is the radian equivalent of 180 degrees. So any angle is to be multiplied with π and then divided by 180 degrees to obtain its radian equivalent.

The good thing is, we shall do just that in Python too! But we need to import another library to pull in the exact value of pi, rather than using the approximation of 3.14. We shall use the following code to import the math library in Python.

import math
from math import pi

Then one shall assign a constant for the value of π/180° as shown below.

conv = pi/180

Then we can use the multiply( ) function from the numpy library for the conversion as shown below.

Converting Degrees To Radians Using Pi
Converting Degrees To Radians Using Pi

Conclusion

Now that we have reached the end of this article, hope it has elaborated on how to convert entities from degrees to radians using numpy library in Python. Here’s another article which details how to divide entities using numpy in Python. There are numerous other enjoyable & equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Whilst you enjoy those, adios!