NumPy sin()
The numpy.sin()
function computes the trigonometric sine of each element in an input array.
The input values should be in radians.
Syntax
</>
Copy
numpy.sin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Angle values in radians. Each element will have its sine computed. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the sine function. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array with the sine values of the input array elements. If the input is a scalar, a scalar is returned.
Examples
1. Computing Sine of a Single Value
Here, we compute the sine of a single angle in radians.
</>
Copy
import numpy as np
# Define an angle in radians
angle = np.pi / 2 # 90 degrees in radians
# Compute the sine of the angle
result = np.sin(angle)
# Print the result
print("Sine of 90 degrees (π/2 radians):", result)
Output:
Sine of 90 degrees (π/2 radians): 1.0

2. Computing Sine for an Array of Angles
We compute the sine values for multiple angles provided in an array.
</>
Copy
import numpy as np
# Define an array of angles in radians
angles = np.array([0, np.pi/6, np.pi/4, np.pi/3, np.pi/2]) # [0°, 30°, 45°, 60°, 90°]
# Compute the sine of each angle
sine_values = np.sin(angles)
# Print the results
print("Angles (in radians):", angles)
print("Sine values:", sine_values)
Output:
Angles (in radians): [0. 0.52359878 0.78539816 1.04719755 1.57079633]
Sine values: [0. 0.5 0.70710678 0.8660254 1. ]

3. Using the out
Parameter
Using an output array to store results instead of creating a new array.
</>
Copy
import numpy as np
# Define an array of angles
angles = np.array([0, np.pi/4, np.pi/2, np.pi])
# Create an output array with the same shape
output_array = np.empty_like(angles)
# Compute sine and store the result in output_array
np.sin(angles, out=output_array)
# Print the results
print("Computed sine values:", output_array)
Output:
Computed sine values: [0.00000000e+00 7.07106781e-01 1.00000000e+00 1.22464680e-16]

4. Using the where
Parameter
Using a condition to compute sine only for selected elements.
</>
Copy
import numpy as np
# Define an array of angles
angles = np.array([0, np.pi/4, np.pi/2, np.pi])
# Define a mask (compute sine only where mask is True)
mask = np.array([True, False, True, False])
# Compute sine values where mask is True
result = np.sin(angles, where=mask)
# Print the results
print("Computed sine values with mask:", result)
Output:
Computed sine values with mask: [0. 0. 1. 0.]

The sine values are computed only for elements where mask=True
. The other values remain unchanged.