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

NumPy arccos() Function



The NumPy arccos() function is used to compute the inverse cosine (arccosine) of each element in an input array. It calculates the angle (in radians) whose cosine 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 [0, ], as the arccosine function returns angles in this range.

Syntax

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

numpy.arccos(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 arccosine 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 arccosine (inverse cosine) of the corresponding element in the input array x, in radians.

Example: Basic Usage of arccos() Function

In the following example, we use the arccos() function to compute the arccosine 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 arccos to each element
result = np.arccos(arr)
print(result)

The output obtained will be −

[3.14159265 2.0943951  1.57079633 1.04719755 0.        ]

Example: Arccosine of Angles in Degrees

In this example, we first calculate the arccosine 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 arccosine in radians
radians_result = np.arccos(values)

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

This will produce the following result −

[180. 120.  90.  60.   0.]

Example: Arccosine of a Single Scalar Value

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

import numpy as np

# Scalar value
scalar = 0.5

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

The output obtained is −

1.0471975511965979

Example: Handling Out-of-Range Values

In this example, we attempt to compute the arccosine 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 arccos to the array
result = np.arccos(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 arccos
  result = np.arccos(arr_out_of_range)
[       nan        nan 1.57079633]
numpy_trigonometric_functions.htm
Advertisements