Introduction to Python Numpy Trigonometric Functions

In This Article, You Will Learn About Python Numpy Trigonometric.

Python Numpy Trigonometric – Before moving ahead, let’s know a bit of Python Numpy HCF

Contents

Introduction

NumPy includes the ufuncs sin(), cos() and tan() that take values in radians and generate the sin cos, tan, and sin values.

Example – Finding cos value at Pi/1.

				
					import numpy as np

x = np.cos(np.pi/1)

print(x)

				
			
Output - 

-1.0

As shown above, it returned value of cos at pi/1.

Example – Finding cos value at given array.

				
					import numpy as np

array = np.array([np.pi/1, np.pi/2, np.pi/3, np.pi/4])

x = np.cos(array)

print(x)

				
			
Output - 

[-1.00000000e+00 6.12323400e-17 5.00000000e-01 7.07106781e-01]

As shown above, it returned an array containing values of given array.

Convert Degrees into Radians

By default, all trigonometric functions take radians as parameters, but we can convert radians into degrees, and vice versa, in NumPy.

Note: radians values are pi/180 * degree_values.

Example – Converting all the values of array in radians.

				
					import numpy as np

array = np.array([60, 80, 90, 110])

x = np.deg2rad(array)

print(x)

				
			
Output - 

[1.04719755 1.3962634 1.57079633 1.91986218]

As a result, it returned an array converting all values in radians.

Radians to Degrees

Example – Converting all the values of array in degrees.

				
					import numpy as np

array = np.array([np.pi/1, np.pi, 2*np.pi, 3*np.pi])

x = np.rad2deg(array)

print(x)

				
			

Output – 

[180. 180. 360. 540.]

As a result, it returned an array converting all values in degrees.

Finding Angles 

Numpy provides unfunc arcsin(), arccos() and arctan() that returns the radians value of inverse of sin, cos, tan.

Example – Finding radians value of inverse of cos at 1.0. 

				
					import numpy as np

x = np.arccos(1.0)

print(x)

				
			
Output - 

0.0

As a result, it returned a value as  radians value of inverse of cos at 1.0.

Angles of Each Value in Arrays

Example – Finding the angle of all values of sine in the array.

				
					import numpy as np

array = np.array([0, -1, 0.1])

x = np.arcsin(array)

print(x)

				
			
Output - 

[ 0.         -1.57079633  0.10016742]

As can be seen, it returned an array with the angel of all values of sine.

Find Hypotenuse value

NumPy includes its hypot() function which utilizes the perpendicular and base values and creates an hypotenuse using the Pythagoras theorem.

Example – Finding the value of Hypotenuse using Pythagoras theorem.  

				
					import numpy as np

b = 12
p = 5

x = np.hypot(b, p)

print(x) 

				
			
python-numpy-trigonometric

As a result, it returned a value of  Hypotenuse using Pythagoras theorem.  

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

1 thought on “Introduction to Python Numpy Trigonometric Functions”

Leave a Comment

Stay in the loop

codingstreets