Today, we are going to Understanding Trigonometric Function in NumPy. Numpy has support for Trigonometric functions like sine, cosine, tangent, etc. We will see the examples and details of these functions.
To understand how trigonometric functions works in NumPy first we need an array of angles. So we will create an array of angles with values in degrees. Then we will learn some of the techniques to convert the angles from degree to radians and vice versa. This is important in Trigonometry to understand the uses of angles in degree and radian. A lot of the functions work on radians and similarly, some of the functions work on degree values of angles.
Calculate Radian without built-in function
In this below example, we used the calculation but we have another method that we can use is np. radians() function to convert from degrees to radians
Program Example
import numpy as np
angles_array = np.array([0,30,45,60,90])
radians_array = angles_array * np.pi/180
print("Angle values in Radian",radians_array)
Output:
Angle values in Radian [0. 0.52359878 0.78539816 1.04719755 1.57079633]
NumPy Calulate radian Using np.radians()
np. radians() is a built-in function of NumPy to convert from degrees to radians.
import numpy as np
angles_array = np.array([0,30,45,60,90])
radians_array1 = np.radians(angles_array)
print("Angle values using Radian",radians_array1)
Output:
Angle values using Radian [0. 0.52359878 0.78539816 1.04719755 1.57079633]
np.degrees():Convert radians to degrees Python NumPy
Also, We have np.degrees() function which is used to converts radians to degrees if we need the angles in degrees.
import numpy as np
angles_array = np.array([0,30,45,60,90])
radians_array1 = np.radians(angles_array)
print ('Converting radian to degrees:' )
print (np.degrees(radians_array1))
Output:
Converting radian to degrees:
[ 0. 30. 45. 60. 90.]
Now we have our angles converted to radian, so we can use them to calculate different trigonometric values using functions.
1.Calculating Python numpy Sine
So the first function that we are going to learn is np. sin(). This function accepts the angles in radian as input and returns the sine value. Let us understand how to calculate sin in python numpy
import numpy as np
angles_array = np.array([0,30,45,60,90])
radians_array1 = np.radians(angles_array)
print('Calculating Sine :')
sine_value = np.sin(radians_array1)
print(sine_value)
When we execute the above example then we can that we get the sine values for the input angle values.
Output:
Calculating Sine :
[0. 0.5 0.70710678 0.8660254 1. ]
2. Calculating Python numpy Cosine
The next function that we have in the NumPy library for Trigonometric functions is np. cos(). This function helps us to determine the Cosine values of angles. Let us understand python np cos with below python program
import numpy as np
angles_array = np.array([0,30,45,60,90])
radians_array1 = np.radians(angles_array)
print('Calculating Cos :')
cos_value = np.cos(radians_array1)
print(cos_value)
As you can see from the example above we are able to get the cosine values of angles by using np.cos() function.
Output:
Calculating Cos :
[1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
6.12323400e-17]
3.Calculating Python numpy Tangent
The next function to calculate the Tangent of angles is np.tan(). This function accepts the angle value in radian and returns the tangent values.So let’s check this out with our example below:
import numpy as np
angles_array = np.array([0,30,45,60,90])
radians_array1 = np.radians(angles_array)
print('Calculating Tangent :')
tan_value = np.tan(radians_array1)
print(tan_value)
Output:
Calculating Tangent :
[0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
1.63312394e+16]
4.Calculating Python numpy Cotangent
The cotangent is inverse of Tangent, NumPy provides us function np.arctan() to calculate the Cotangent function for an angle value. We can use this function as shown in below example:
import numpy as np
angles_array = np.array([0,30,45,60,90])
radians_array1 = np.radians(angles_array)
tan_value = np.tan(radians_array1)
print('Calculating Cotangent :')
cot_value = np.arctan(tan_value)
print(cot_value)
As you can see we are passing the tan_value in this function to get the cotangent values.
Output:
Calculating Cotangent :
[0. 0.52359878 0.78539816 1.04719755 1.57079633]
5.Calculating Python numpy Cosecant
Cosecant is inverse of Sine. We have np.arcsin() function available in NumPy library to calculate the Cosecant value.
We are passing the sine_value to this function to calculate the Cosecant value.
import numpy as np
angles_array = np.array([0,30,45,60,90])
radians_array1 = np.radians(angles_array)
print('Calculating Cosecant :')
sine_value = np.sin(radians_array1)
cosec_value = np.arcsin(sine_value)
print(cosec_value)
Output:
Calculating Cosecant :
[0. 0.52359878 0.78539816 1.04719755 1.57079633]
6. Calculating Python numpy Secant
Secant is inverse of cos. We have np.arccos() function to calculate the secant of a value.
As you can see from the example below how we are using np.arccos() function:
import numpy as np
angles_array = np.array([0,30,45,60,90])
radians_array1 = np.radians(angles_array)
print('Calculating secant :')
cos_value = np.cos(radians_array1)
sec_value = np.arccos(cos_value)
print(sec_value)
Output:
Calculating secant :
[0. 0.52359878 0.78539816 1.04719755 1.57079633]
Conclusion
In this post, we have Understood Trigonometric Function in NumPy The Trigonometric functions in NumPy are of great use when we work on mathematical problems. As all the examples we showed above are using the Numpy ndarray, this becomes more helpful to perform the calculation on big chunks of data with just one line of code. A lot of scientific libraries in Python make use of these functions in taking advantage of these functions.
FAQ
Does NumPy have sin and cos?
Yes NumPy support trigonometry function like sin, cos, tan, Cotangent, Cosecant, Secant, and more
How to write cos in NumPy
With the help of np. cos() as mentioned above with the example
is NumPy Trig in radiant or degree
By default, the NumPy function takes radian as a parameter. The radian can be converted to degree and vice-versa. we can calculate radian by using the formula pi/180*degree _value
How do we write cos in NumPy
The numpy library nump.cos() function is used to write cos in Numpy