NumPy Sin and Cos

numpy
Published

August 31, 2024

The trigonometric functions sin() and cos() are frequently used in scientific computing, data analysis, and signal processing. This post will look into the practical application of these functions, showcasing their versatility and power with clear code examples.

NumPy’s sin() Function: Calculating Sine Values

The sin() function in NumPy calculates the trigonometric sine of an angle, provided in radians. It’s significantly faster than using Python’s built-in math.sin() for array operations, making it ideal for large datasets.

import numpy as np

angle_rad = np.pi / 4  # 45 degrees in radians
sine_value = np.sin(angle_rad)
print(f"Sine of {angle_rad} radians: {sine_value}")


angles_rad = np.array([0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi])
sines = np.sin(angles_rad)
print(f"Sines of angles: {sines}")

#Example using degrees
degrees = np.array([0, 30, 45, 60, 90])
radians = np.deg2rad(degrees)
sines_degrees = np.sin(radians)
print(f"Sines of angles in degrees: {sines_degrees}")

This code demonstrates how to compute the sine of both single angles and arrays of angles. Note the use of np.deg2rad() to convert degrees to radians before calculating the sine.

NumPy’s cos() Function: Calculating Cosine Values

Similar to sin(), the cos() function computes the cosine of an angle (in radians). Its efficiency makes it indispensable when dealing with large-scale trigonometric computations.

import numpy as np

angle_rad = np.pi / 3 # 60 degrees in radians
cosine_value = np.cos(angle_rad)
print(f"Cosine of {angle_rad} radians: {cosine_value}")


angles_rad = np.array([0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi])
cosines = np.cos(angles_rad)
print(f"Cosines of angles: {cosines}")

#Example using degrees
degrees = np.array([0, 30, 45, 60, 90])
radians = np.deg2rad(degrees)
cosines_degrees = np.cos(radians)
print(f"Cosines of angles in degrees: {cosines_degrees}")

The example above mirrors the sin() example, showcasing the calculation of cosine for both single values and arrays, again highlighting the importance of converting degrees to radians when necessary.

Beyond Basic Usage: Broadcasting and More

NumPy’s sin() and cos() functions seamlessly integrate with NumPy’s broadcasting capabilities, allowing for efficient element-wise operations on arrays of different shapes. This opens up possibilities for complex calculations involving trigonometric functions and other array operations. Further exploration into NumPy’s documentation will reveal advanced features and applications.