NumPy arctanh()

The numpy.arctanh() function computes the inverse hyperbolic tangent (arctanh) of each element in an input array. The input values must be in the range -1 < x < 1, as the function is undefined outside this range.

Syntax

</>
Copy
numpy.arctanh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput array containing values in the range (-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 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 with the inverse hyperbolic tangent values of the input elements. If the input is a scalar, a scalar is returned.


Examples

1. Computing Inverse Hyperbolic Tangent of a Single Value

Here, we compute the inverse hyperbolic tangent of a single number within the valid range (-1, 1).

</>
Copy
import numpy as np

# Define a value within the valid range (-1, 1)
value = 0.5  

# Compute the inverse hyperbolic tangent
result = np.arctanh(value)

# Print the result
print("arctanh(0.5):", result)

Output:

arctanh(0.5): 0.5493061443340549

2. Computing Inverse Hyperbolic Tangent for an Array of Values

We compute the inverse hyperbolic tangent for multiple values in an array.

</>
Copy
import numpy as np

# Define an array of values within the valid range (-1, 1)
values = np.array([-0.9, -0.5, 0, 0.5, 0.9])

# Compute the inverse hyperbolic tangent of each value
arctanh_values = np.arctanh(values)

# Print the results
print("Input values:", values)
print("Inverse hyperbolic tangent values:", arctanh_values)

Output:

Input values: [-0.9 -0.5  0.   0.5  0.9]
Inverse hyperbolic tangent values: [-1.47221949 -0.54930614  0.          0.54930614  1.47221949]

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 values within the valid range
values = np.array([-0.7, -0.3, 0.2, 0.6])

# Create an output array with the same shape
output_array = np.empty_like(values)

# Compute arctanh and store the result in output_array
np.arctanh(values, out=output_array)

# Print the results
print("Computed inverse hyperbolic tangent values:", output_array)

Output:

Computed inverse hyperbolic tangent values: [-0.86730053 -0.3095196   0.20273255  0.69314718]

4. Using the where Parameter

Using a condition to compute arctanh only for selected elements.

</>
Copy
import numpy as np

# Define an array of values within the valid range
values = np.array([-0.8, -0.4, 0, 0.4, 0.8])

# Define a mask (compute arctanh only where mask is True)
mask = np.array([True, False, True, False, True])

# Compute arctanh values where mask is True
result = np.arctanh(values, where=mask)

# Print the results
print("Computed inverse hyperbolic tangent values with mask:", result)

Output:

Computed inverse hyperbolic tangent values with mask: [-1.09861229  0.5         0.          1.5         1.09861229]

The arctanh values are computed only for elements where mask=True.