Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
NumPy arcsin()
In this tutorial, you will learn about the numpy.arcsin() method with the help of examples.
The arcsin() method computes the arcsine (inverse sine) of each element in an array.
Example
import numpy as np
# create an array of values between -1 and 1
values = np.array([-1, -0.5, 0, 0.5, 1])
# calculate the inverse sine of each value
inverseSine = np.arcsin(values)
print(inverseSine)
# Output: [-1.57079633 -0.52359878 0. 0.52359878 1.57079633]
arcsin() Syntax
The syntax of arcsin() is:
numpy.arcsin(x, out=None, where = True, dtype = None)
arcsin() Arguments
The arcsin() method takes following arguments:
x- an input arrayout(optional) - the output array where the result will be storedwhere(optional) - a boolean array or condition indicating where to compute the arcsinedtype(optional) - data type of the output array
arcsin() Return Value
The arcsin() method returns an array with the corresponding inverse sine values.
Example 1: Use of out and where in arcsin()
import numpy as np
# create an array of values between -0.5 and 0.5
values = np.array([-0.5, -0.2, 1, 0.2, 0.5])
# create an array of zeros with the same shape as values
result = np.zeros_like(values, dtype=float)
# calculate inverse sine where values >= 0 and store in result.
np.arcsin(values, out=result, where=(values >= 0))
print(result)
Output
[0. 0. 1.57079633 0.20135792 0.52359878]
Here,
out=resultspecifies that the output of thenp.arcsin()function should be stored in the result arraywhere=(values >= 0)specifies that the inverse sine operation should only be applied to elements in values that are greater than or equal to 0
Example 2: Use of dtype Argument in arcsin()
import numpy as np
# create an array of values between -0.5 and 0.5
values = np.array([-0.5, -0.2, 0, 0.2, 0.5])
# calculate the inverse sine of each value with a specific dtype
inverse_sines_float = np.arcsin(values, dtype=float)
inverse_sines_complex = np.arcsin(values, dtype=complex)
print("Inverse sines with 'float' dtype:")
print(inverse_sines_float)
print("\nInverse sines with 'complex' dtype:")
print(inverse_sines_complex)
Output
Inverse sines with 'float' dtype: [-0.52359878 -0.20135792 0. 0.20135792 0.52359878] Inverse sines with 'complex' dtype: [-0.52359878+0.j -0.20135792+0.j 0. +0.j 0.20135792+0.j 0.52359878+0.j]
Here, by specifying the desired dtype, we can control the data type of the output array according to our requirements.
Note: To learn more about the dtype argument, please visit NumPy Data Types.