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
= np.array([0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi])
angles_rad
= np.sin(angles_rad)
sine_values = np.cos(angles_rad)
cosine_values = np.tan(angles_rad)
tangent_values
print("Angles (radians):", angles_rad)
print("Sine values:", sine_values)
print("Cosine values:", cosine_values)
print("Tangent values:", tangent_values)
= 1
y = 1
x = np.degrees(np.arctan2(y, x))
angle_degrees print(f"The angle (degrees) using arctan2 for y={y}, x={x} is: {angle_degrees}")
#Example with hypotenuse calculation
= 3
x = 4
y = np.hypot(x,y)
hypotenuse print(f"The hypotenuse of a triangle with legs {x} and {y} is: {hypotenuse}")
#Convert Degrees to Radians and vice versa
= np.array([0, 90, 180, 270, 360])
angles_deg = np.radians(angles_deg)
angles_rad_converted = np.degrees(angles_rad)
angles_deg_converted
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.