numpy.asin() function is same as numpy.arcsin() function.

NumPy asin()

The numpy.asin() 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.asin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeValues representing the y-coordinates on the unit circle. The valid input range is [-1, 1].
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 arcsin 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 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.asin(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
NumPy asin() - Trigonometric Inverse Sine Example

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.asin(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.asin(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.asin(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.