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

ParameterTypeDescription
xarray_likeAngle values in radians. Each element will have its sine computed.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the sine function.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines 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.