Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

Universal NumPy Trigonometric functions to know

Universal NumPy Trigonometric Functions

Hello, readers! In this article, we will learn the universal NumPy trigonometric functions to know!

So, let us get started! 🙂

To being with, the mathematical functions in NumPy are framed as Universal functions. These Universal (mathematical NumPy functions) operate on the NumPy Array class and perform element-wise operations on the data values. The universal NumPy functions belong to the numpy.ufunc class in Python.

In the context of this topic, we will be focusing on the below types of Universal Trigonometric functions–

  1. Universal Trigonometric functions
  2. Functions that help us perform inter-conversion between degree and radian values
  3. Hyperbolic functions
  4. Calculation of Hypotenuse value
  5. Determining angle values from the trigonometric functions

1. Numpy Trigonometric functions

We’ll work on the following universal Numpy trigonometric functions for this tutorial–

  1. numpy.sin() function: Calculates the sine component for the array values.
  2. numpy.cos() function: Calculates the cosine component for the array values.
  3. numpy.tan() function: Calculates tangent value for the array data elements.

Example:

import numpy as np
arr = np.array([30,60,90])

val_sin = np.sin(arr)
print("Sine value",val_sin)

val_cos = np.cos(arr)
print("Cosine value",val_cos)

val_tan = np.tan(arr)
print("Tangent value",val_tan)

Output:

Sine value [-0.98803162 -0.30481062  0.89399666]
Cosine value [ 0.15425145 -0.95241298 -0.44807362]
Tangent value [-6.4053312   0.32004039 -1.99520041]

2. Inter-conversion between Degree and Radian values

While performing trigonometric operations in any language, we come across situations wherein we feed the need to convert degrees to radians and vice-versa.

For the same, NumPy offers us with Universal functions–

  1. deg2rad: Converts degree value of an angle to radians.
  2. rad2deg: Converts radian angle to degree.

Example:

import numpy as np
arr = np.array([30,60,90])

rad = np.deg2rad(arr)
print("Radian values for the array having degree values:", rad)

arr_rad = np.array([0.52359878, 1.04719755, 1.57079633])
degree = np.rad2deg(arr_rad)
print("Degree values for the array having radian values:", degree)

Output:

Radian values for the array having degree values: [0.52359878 1.04719755 1.57079633]
Degree values for the array having radian values: [30.00000025 59.99999993 90.00000018]

3. Determining angles from the trigonometric values

In the form of reverse engineering, we now feed the below functions with trigonometric values and try to get the angle values from them–

  1. arcsin() function: Calculates the angle value from the sine values.
  2. arccos() function: Calculates the angle value from the cosine values.
  3. arctan() function: Calculates the angle value from the tangent values.

Example:

import numpy as np
arr = np.array([1,0.5])

sin_ang = np.arcsin(arr)
print("Angle from the sin function:", sin_ang)

cos_ang = np.arccos(arr)
print("Angle from the cos function:", cos_ang)

tan_ang = np.arctan(arr)
print("Angle from the tan function:", tan_ang)

Output:

Angle from the sin function: [1.57079633 0.52359878]
Angle from the cos function: [0.         1.04719755]
Angle from the tan function: [0.78539816 0.46364761]

4. Hypotenuse

With numpy.hypot() function, we can calculate the hypotenuse value according to the Pythagoras’ standards by providing the function with the base and height values.

Syntax:

numpy.hypot() function

Example:

import numpy as np

b = 5
h = 8

hy = np.hypot(b, h)

print(hy)

Output:

9.433981132056603

5. Hyperbolic functions

NumPy provides us with the below functions to calculate the hyperbolic trigonometric values for the given values:

  1. numpy.sinh() function: Calculates the hyperbolic sine value for the array values.
  2. numpy.cosh() function: Calculates the hyperbolic cosine value for the array values.
  3. numpy.tanh() function: Calculates the hyperbolic tangent value for the array values.

Example:

import numpy as np
arr = np.array([30,60,90])

val_sin = np.sinh(arr)
print("Hyperbolic Sine value",val_sin)

val_cos = np.cosh(arr)
print("Hyperbolic Cosine value",val_cos)

val_tan = np.tanh(arr)
print("Hyperbolic Tangent value",val_tan)

Output:

Hyperbolic Sine value [5.34323729e+12 5.71003695e+25 6.10201647e+38]
Hyperbolic Cosine value [5.34323729e+12 5.71003695e+25 6.10201647e+38]
Hyperbolic Tangent value [1. 1. 1.]

Conclusion

By this, we have come to the end of the NumPy Trigonometric functions article. Feel free to comment below, in case you come across any questions. For more such posts related to Python programming, Stay tuned with us!

Till then, Happy Learning!! 🙂