NumPy arcsin()
The numpy.arcsin()
function computes the inverse sine (arcsin) of each element in an input array, returning the angle in radians within the range [-π/2, π/2]
.
Syntax
</>
Copy
numpy.arcsin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Values representing the y-coordinates on the unit circle. The valid input range is [-1, 1] . |
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 arcsin 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 containing the inverse sine values (in radians) for each input element. The output values lie in the range [-π/2, π/2]
. If the input is a scalar, a scalar is returned.
Examples
1. Computing the Inverse Sine of a Single Value
Here, we compute the inverse sine of a single value.
</>
Copy
import numpy as np
# Define a value within the valid range [-1, 1]
y = 0.5
# Compute the inverse sine (arcsin)
angle = np.arcsin(y)
# Print the result
print("Arcsin of 0.5 in radians:", angle)
print("Arcsin of 0.5 in degrees:", np.degrees(angle))
Output:
Arcsin of 0.5 in radians: 0.5235987755982988
Arcsin of 0.5 in degrees: 29.999999999999996

2. Computing the Inverse Sine for an Array
We compute the inverse sine for multiple values provided in an array.
</>
Copy
import numpy as np
# Define an array of values between -1 and 1
y_values = np.array([-1, -0.5, 0, 0.5, 1])
# Compute the inverse sine (arcsin)
angles = np.arcsin(y_values)
# Convert to degrees
angles_degrees = np.degrees(angles)
# Print the results
print("Input values:", y_values)
print("Arcsin values (radians):", angles)
print("Arcsin values (degrees):", angles_degrees)
Output:
Input values: [-1. -0.5 0. 0.5 1. ]
Arcsin values (radians): [-1.57079633 -0.52359878 0. 0.52359878 1.57079633]
Arcsin values (degrees): [-90. -30. 0. 30. 90.]

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 input values within the range [-1, 1]
y_values = np.array([-1, 0, 1])
# Create an output array with the same shape
output_array = np.ndarray(shape=[1, 3])
# Compute arcsin and store the result in output_array
np.arcsin(y_values, out=output_array)
# Print the results
print("Computed arcsin values:", output_array)
Output:
Computed arcsin values: [[-1.57079633 0. 1.57079633]]

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

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