NumPy arccosh() Function



The NumPy arccosh() function is used to compute the inverse hyperbolic cosine (arccosine hyperbolic) of each element in an input array. It calculates the value of the angle (in radians) whose hyperbolic cosine is the input value. The input values must be greater than or equal to 1.

  • Domain: The function accepts input values for all real numbers greater than or equal to 1. It can handle scalars or arrays with values that meet this requirement.
  • Range: The output values are in the range [0, ), as the arccosh function can only return non-negative values.

Syntax

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

numpy.arccosh(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. The function computes the inverse hyperbolic cosine of each element of the array or scalar. The values must be greater than or equal to 1.
  • 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 inverse hyperbolic cosine (arccosine hyperbolic) of the corresponding element in the input array x, in radians.

Example: Basic Usage of arccosh() Function

In the following example, we use the arccosh() function to compute the inverse hyperbolic cosine of each element in a 1-dimensional array −

import numpy as np

# Creating a 1-dimensional array
arr = np.array([1, 2, 3, 4])

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

The output obtained will be −

[0.          1.3169579   1.76274717  2.06343707]

Example: Arccosine of Angles in Degrees

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

import numpy as np

# Creating an array of values
values = np.array([1, 2, 3, 4])

# Calculate arccosh in radians
radians_result = np.arccosh(values)

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

This will produce the following result −

[  0.          75.45612929 100.99797342 118.22623534]

Example: Arccosine of a Single Scalar Value

In the example below, we are using the arccosh() function to calculate the inverse hyperbolic cosine of a single scalar value −

import numpy as np

# Scalar value
scalar = 2

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

The output obtained is −

1.3169578969248166

Example: Arccosine of Values Less Than 1

In this example, we attempt to calculate the inverse hyperbolic cosine of values less than 1. The function will return a RuntimeWarning and produce an invalid result for inputs less than 1 −

import numpy as np

# Values less than 1
invalid_values = np.array([0.5, 0, -1])

# Applying arccosh to the array
result = np.arccosh(invalid_values)
print(result)

This will produce the following result −

/home/cg/root/6752d9ebbe88d/main.py:7: RuntimeWarning: invalid value encountered in arccoshresult = np.arccosh(invalid_values)
[nan nan nan]
numpy_trigonometric_functions.htm
Advertisements