NumPy Trigonometric Functions

numpy
Published

February 19, 2023

The Core Trigonometric Functions

NumPy offers a complete set of trigonometric functions, including:

  • sin(x): Computes the sine of x (in radians).
  • cos(x): Computes the cosine of x (in radians).
  • tan(x): Computes the tangent of x (in radians).
  • arcsin(x): Computes the inverse sine (arcsine) of x, returning values in the range [-π/2, π/2].
  • arccos(x): Computes the inverse cosine (arccosine) of x, returning values in the range [0, π].
  • arctan(x): Computes the inverse tangent (arctangent) of x, returning values in the range [-π/2, π/2].
  • arctan2(y, x): Computes the arctangent of y/x, considering the signs of both y and x to determine the correct quadrant. This function is particularly useful when dealing with angles in all four quadrants.
  • hypot(x, y): Computes the hypotenuse of a right-angled triangle with legs x and y (√(x²+y²)).
  • degrees(x): Converts angles from radians to degrees.
  • radians(x): Converts angles from degrees to radians.

Code Examples: Unleashing the Power of NumPy Trig Functions

Let’s illustrate the usage of these functions with some practical examples:

import numpy as np

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

sine_values = np.sin(angles_rad)
cosine_values = np.cos(angles_rad)
tangent_values = np.tan(angles_rad)

print("Angles (radians):", angles_rad)
print("Sine values:", sine_values)
print("Cosine values:", cosine_values)
print("Tangent values:", tangent_values)


y = 1
x = 1
angle_degrees = np.degrees(np.arctan2(y, x))
print(f"The angle (degrees) using arctan2 for y={y}, x={x} is: {angle_degrees}")

#Example with hypotenuse calculation
x = 3
y = 4
hypotenuse = np.hypot(x,y)
print(f"The hypotenuse of a triangle with legs {x} and {y} is: {hypotenuse}")

#Convert Degrees to Radians and vice versa

angles_deg = np.array([0, 90, 180, 270, 360])
angles_rad_converted = np.radians(angles_deg)
angles_deg_converted = np.degrees(angles_rad)

print("Angles (degrees):", angles_deg)
print("Angles (radians, converted):", angles_rad_converted)
print("Angles (degrees, converted from radians):", angles_deg_converted)

This code demonstrates the straightforward application of NumPy’s trigonometric functions to arrays. Notice how easily we can perform calculations on multiple angles simultaneously, a key advantage of NumPy’s vectorized operations. The output clearly shows the results of each trigonometric function applied to the array of angles. The arctan2 example highlights its ability to handle quadrants correctly. Finally, the degree to radian and vice versa conversion shows the utility of these functions.

Beyond the Basics: Advanced Applications

NumPy’s trigonometric functions are not limited to simple calculations. They are frequently used in more complex scenarios, such as:

  • Signal processing: Analyzing and manipulating waveforms.
  • Image processing: Implementing transformations and filters.
  • Physics and engineering: Solving equations involving angles and oscillations.
  • Machine learning: Building models involving cyclical patterns.

These examples only scratch the surface of NumPy’s capabilities. As you become more proficient, you will find yourself using these functions extensively in a wide variety of numerical applications.