Trigonometric Functions – NumPy uFuncs (Python Tutorial)

Trigonometric Functions NumPy UFuncs (Python Tutorial)

This is a detailed tutorial of the NumPy Trigonometric Universal Functions. Learn the usage of these functions with the help of examples.

Trigonometric Functions

Trigonometric Functions are real functions that have a relation of the angle of a right-angled triangle to ratios of two side lengths. The trigonometric functions which are very common at use are sine, cosine and tangent. And the reciprocal of these functions is cosecant,  secant and cotangent.

So NumPy provides us with some of the Unfuncs with the help of which we can calculate the trigonometric functions. The function is as follows: sin() , cos() and tan()which will take values in radians and give us corresponding values.

Let us take an example to have a better understanding:

#first we will import the numpy package 
import numpy as np
#now in the next step we use sin() function
a=np.sin(np.pi/4)
#now we will print the result
print(a)

Output.

0.707106781187

Here we are getting the sin of the angle which we have given in.

let us take another example:

#first we will import the numpy package 
import numpy as np
#now in the next step we use sin() function
a=np.array([np.pi/4,np.pi/1,np.pi/7,np.pi/6])
b=np.sin(a)
#now we will print the result
print(b)

Output.

[ 7.07106781e-01 1.22464680e-16 4.33883739e-01 5.00000000e-01]

Degree to Radians

In the trigonometric function in the NumPy, radians are taken as values by default, but we can convert them into the degree. As radian is pi/180*degree, this is how they are related to each other.

let us take an example to understand it better:

#first we will import the numpy package 
import numpy as np
#now in the next step we will take an array of degrees
a=np.array([90,180,360])
#now we will write the function to convert them into radians
b=np.deg2rad(a)
#now we will print the result
print(b)

Output.

[ 1.57079633 3.14159265 6.28318531]

So here in this example, we see that degree values are turned into radians.

Radians to Degree

In order to covert radians onto a degree, we will use the rad2deg() function.

let u take an example to understand it better:

#first we will import the numpy package 
import numpy as np
#now in the next step we will take an array of radians
a=np.array([np.pi/4,np.pi/1,np.pi/7,np.pi/6])
#now we will write the function to convert them into degrees
b=np.rad2deg(a)
#now we will print the result
print(b)

Output.

[ 45. 180. 25.71428571 30. ]

As a result here all the radians are converted into degrees.

Finding Angles:

In order to find angles in NumPy, we have various functions. And we can also get the inverse of angles like sin, cos and tan. NumPy gives us Unfuncs like aecsin(), arccos() and arctan() which will give us the radian values. These will helps us in getting the related value to the sin, cos and tan value we have given.

let us take an example to understand it better:

#first we will import the numpy package 
import numpy as np
#now in the next step we use the function
a=np.arcsin(0.5)
#now we will print the result
print(a)

Output.

0.523598775598

Hypotenuse

In order to find the hypotenuse, we use the Pythagoras theorem. In NumPy, we have a function to find the hypotenuse which is known as hypot(). This function will take the value and produce hypotenuse for us. The value taken by this function is the base and perpendicular height of the triangle. And on the basis of the Pythagoras theorem, it will simplify that.

Let us take an example to understand it better:

#first we will import the numpy package 
import numpy as np
# we will take teh values for the base and prependicular value
base=4
prepend=6
#now in the next step we use the function
a=np.hypot(base,prepend)
#now we will print the result
print(a)

Output.

7.21110255093

So the height and base along with using the Pythagoras theorem give us the hypotenuse.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *