Trigonometric functions Python Numpy
In this post, you will learn different trigonometric functions of the Python programming language.
The basic trigonometric functions are sine, cosine, and tangent. NumPy is the fundamental package for scientific computing with Python. It is a highly optimized library for numerical operations. Numpy provides various mathematical functions, including trigonometric functions. These functions provide the relationship between the angles and sides of a triangle. Trigonometric functions can be used by using "import math".
List of Trigonometric functions in Python
Trigonometric functions | Description | Example |
math.sin() | It returns a numeric value between -1 and 1, which represents the sine of the angle given in radians. | math.sin(x) |
math.cos() | It returns a numeric value between -1 and 1, which represents the cosine of the angle. | math.cos(x) |
math.tan() | It returns a numeric value that represents the tangent of the angle. | math.tan(x) |
math.acos() | It returns a numeric value between 0 and π radians for x between -1 and 1. | math.acos(x) |
math.asin() | It returns the arc sine of a number. | math.asin(x) |
math.atan() | It returns the arc tangent of the number in radians. | math.atan(x) |
math.atan2() | It returns the arc tangent of y/x, in radians. where x and y are the coordinates of a point (x,y). | math.atan2(y,x) |
math.hypot() | It returns the Euclidean norm. The Euclidian norm is the distance from the origin to the coordinates given. | math.hypot(x,y) |
Trigonometric sin() function
Sin function of an angle is the ratio between the opposite side length to that of the hypotenuse. The following mathematical function helps the user to calculate the trigonometric sine for all x.
numpy.sin(x[, out])
The given Python program demonstrates the uses of the sin() function.
import numpy as np
a = np.array([0,30,45,60,90])
x = np.sin(a*np.pi/2)
print(x)
Output of the above code:
[ 0.00000000e+00 5.38968388e-15 1.00000000e+00 -1.07793678e-14
1.95819692e-15]
Trigonometric cos() function
Cos of an angle is the ratio of the length of the adjacent side to the length of the hypotenuse. The following mathematical function helps the user to calculate the trigonometric cos for all x.
numpy.cos(x[, out])
The given Python program demonstrates the uses of cos() function:
import numpy as np
a = np.array([0,30,45,60,90])
x = np.cos(a*np.pi/2)
print(x)
Output of the above code:
[ 1.00000000e+00 -1.00000000e+00 9.79098459e-16 1.00000000e+00
-1.00000000e+00]
Trigonometric tan() function
The tangent tan() function is the ratio of the length of the opposite side to that of the adjacent side.
numpy.tan(x[, out])
The given Python program demonstrates the uses of tan() function:
import numpy as np
a = np.array([5,23,53,12,31,15])
x = np.tan(a*np.pi/2)
print(x)
Output of the above code:
[ 3.26624787e+15 3.14001254e+14 6.80755038e+14 -7.34788079e-16
2.72141473e+14 3.71079278e+14]
Trigonometric functions for finding angles
We can find the angles from the values of sine, cos, and tan. E.g., sin, cos, and tan inverse. NumPy provides ufuncs arcsin(), arccos() and arctan() functions that produce radian values for corresponding sin, cos and tan values given.
import numpy as np
x = np.arcsin(1.0)
y = np.arccos(-1.0)
z = np.arctan(0.3)
print(x)
print(y)
print(z)
Output of the above code:
1.5707963267948966
3.141592653589793
0.2914567944778671
Python numpy Trigonometric hypot() function
Python numpy hypot() function accepts the "legs" of a right triangle and returns the hypotenuse.
import numpy as np
base = 15
height = 7
hy = np.hypot(base, height)
print(hy)
Output of the above code:
16.55294535724685
Python numpy radians
The Python numpy radians function converts angles from degrees to radians in an array.
import numpy as np
arr = [0, 30, 90, 180 ]
print ("Input array : \n", arr)
radval = np.radians(arr)
print ("\n Radian value : \n", radval)
Output of the above code:
Input array :
[0, 30, 90, 180]
Radian value :
[0. 0.52359878 1.57079633 3.14159265]
Related Articles
Trigonometric functions Python Numpy
Python convert dataframe to numpy array
Convert binary to decimal in Python
Multiply all elements in list Python
Multiply each element of a list by a number in Python
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Python Pandas Plotting
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python take screenshot of specific window
Read data from excel file using Python Pandas
Quick Introduction to Python Pandas
Python requests GET method
Python Convert XML to CSV
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword