NumPy's trigonometric functions are part of its universal functions (ufuncs), which are designed to operate efficiently on NumPy arrays, performing element-wise computations. Trigonometric ufuncs allow you to apply various trigonometric operations to each element of a NumPy array.
Explanation of some commonly used trigonometric ufuncs in NumPy:
import numpy as np
# Define an array of angles in radians
angles = np.array([0, np.pi/2, np.pi])
# Compute the sine of each angle
sine_values = np.sin(angles)
print("Sine of angles:", sine_values) # Output: [0. 1. 1.2246468e-16]
import numpy as np
# Define an array of angles in radians
angles = np.array([0, np.pi/2, np.pi])
# Compute the cosine of each angle
cosine_values = np.cos(angles)
print("Cosine of angles:", cosine_values) # Output: [ 1.000000e+00 6.123234e-17 -1.000000e+00]
import numpy as np
# Define an array of angles in radians
angles = np.array([0, np.pi/4, np.pi/2])
# Compute the tangent of each angle
tangent_values = np.tan(angles)
print("Tangent of angles:", tangent_values) # Output: [0.00000000e+00 1.00000000e+00 1.63312394e+16]
import numpy as np
# Define an array of sine values
sine_values = np.array([0, 0.5, 1])
# Compute the inverse sine of each value
inverse_sine_values = np.arcsin(sine_values)
print("Inverse sine values:", inverse_sine_values) # Output: [0. 0.52359878 1.57079633]
import numpy as np
# Define an array of cosine values
cosine_values = np.array([1, 0.5, 0])
# Compute the inverse cosine of each value
inverse_cosine_values = np.arccos(cosine_values)
print("Inverse cosine values:", inverse_cosine_values) # Output: [0. 1.04719755 1.57079633]
import numpy as np
# Define an array of tangent values
tangent_values = np.array([0, 1, np.inf])
# Compute the inverse tangent of each value
inverse_tangent_values = np.arctan(tangent_values)
print("Inverse tangent values:", inverse_tangent_values) # Output: [0. 0.78539816 1.57079633]
These examples demonstrate how to use trigonometric ufuncs in NumPy to perform various trigonometric operations on arrays of angles or trigonometric values.