Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy arcsin() Function



The NumPy arcsin() function is used to compute the inverse sine (arcsine) of each element in an input array. It calculates the angle (in radians) whose sine is the input value.

  • Domain: The function accepts input values between -1 and 1, inclusive. Values outside this domain will result in an error.
  • Range: The output values lie in the range of [-/2, /2], as the arcsine function returns angles in this range.

Syntax

Following is the syntax of the NumPy arcsin() function −

numpy.arcsin(x, /, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Parameters

This function accepts the following parameters −

  • x: The input array or scalar, which should have values in the range [-1, 1]. The function computes the arcsine of each element of the array or scalar.
  • out (optional): A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
  • where (optional): This condition is broadcast over the input. At locations where the condition is True, the result will be computed. Otherwise, the result will retain its original value.
  • casting (optional): Controls what kind of data casting may occur. Defaults to 'same_kind'.
  • order (optional): Controls the memory layout order of the result. 'C' means C-order, 'F' means Fortran-order, 'A' means 'F' if inputs are all F, 'C' otherwise, 'K' means match the layout of the inputs as closely as possible.
  • dtype (optional): The type of the returned array and of the accumulator in which the elements are processed. The dtype of x is used by default unless dtype is specified.
  • subok (optional): If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array.

Return Value

This function returns an array where each element is the arcsine (inverse sine) of the corresponding element in the input array x, in radians.

Example: Basic Usage of arcsin() Function

In the following example, we use the arcsin() function to compute the arcsine of each element in a 1-dimensional array −

import numpy as np

# Creating a 1-dimensional array with values between -1 and 1
arr = np.array([-1, -0.5, 0, 0.5, 1])

# Applying arcsin to each element
result = np.arcsin(arr)
print(result)

The output obtained will be −

[-1.57079633 -0.52359878  0.          0.52359878  1.57079633]

Example: Arcsine of Angles in Degrees

In this example, we first calculate the arcsine in radians and then convert the result to degrees using numpy.degrees() function −

import numpy as np

# Creating an array of values between -1 and 1
values = np.array([-1, -0.5, 0, 0.5, 1])

# Calculate arcsine in radians
radians_result = np.arcsin(values)

# Convert radians to degrees
degrees_result = np.degrees(radians_result)
print(degrees_result)

This will produce the following result −

[-90. -30.   0.  30.  90.]

Example: Arcsine of a Single Scalar Value

In this example, we are using the arcsin() function to calculate the arcsine of a single scalar value −

import numpy as np

# Scalar value
scalar = 0.5

# Applying arcsin to the scalar
result = np.arcsin(scalar)
print(result)

The output obtained is −

0.5235987755982989

Example: Handling Out-of-Range Values

In this example, we attempt to compute the arcsine of values outside the valid range [-1, 1]. This will raise a runtime warning and return NaN values for such entries −

import numpy as np

# Array with a value outside the valid range
arr_out_of_range = np.array([2, -1.5, 0])

# Applying arcsin to the array
result = np.arcsin(arr_out_of_range)
print(result)

This will produce the following result with a runtime warning −

/home/cg/root/673ae90fcf07d/main.py:7: RuntimeWarning: invalid value encountered in arcsin
  result = np.arcsin(arr_out_of_range)
[nan nan  0.]
numpy_trigonometric_functions.htm
Advertisements