NumPy tanh()
The numpy.tanh()
function computes the hyperbolic tangent of each element in an input array, element-wise.
The hyperbolic tangent function is defined as:
\( \tanh(x) = \dfrac{\sinh(x)}{\cosh(x)} = \dfrac{e^x – e^{-x}}{e^x + e^{-x}} \)
Syntax
numpy.tanh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Input array whose elements are to be transformed using the hyperbolic tangent function. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the hyperbolic tangent function. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array with the hyperbolic tangent values of the input array elements. If the input is a scalar, a scalar is returned.
Examples
1. Computing Hyperbolic Tangent of a Single Value
Here, we compute the hyperbolic tangent of a single input value.
import numpy as np
# Define a single value
x = 1.0
# Compute the hyperbolic tangent
result = np.tanh(x)
# Print the result
print("tanh(1.0):", result)
Output:
tanh(1.0): 0.7615941559557649

2. Computing Hyperbolic Tangent for an Array
We compute the hyperbolic tangent values for multiple numbers using an array.
import numpy as np
# Define an array of values
values = np.array([-2, -1, 0, 1, 2])
# Compute the hyperbolic tangent of each value
tanh_values = np.tanh(values)
# Print the results
print("Input values:", values)
print("Hyperbolic Tangent values:", tanh_values)
Output:
Input values: [-2 -1 0 1 2]
Hyperbolic Tangent values: [-0.96402758 -0.76159416 0. 0.76159416 0.96402758]

3. Using the out
Parameter
Using an output array to store the results instead of creating a new one.
import numpy as np
# Define an array of input values
values = np.array([-1, 0, 1])
# Create an output array with the same shape
output_array = np.ndarray(shape=values.shape)
# Compute hyperbolic tangent and store the result in output_array
np.tanh(values, out=output_array)
# Print the results
print("Computed tanh values:", output_array)
Output:
Computed tanh values: [-0.76159416 0. 0.76159416]

4. Using the where
Parameter
Using a condition to compute the hyperbolic tangent only for selected elements.
import numpy as np
# Define an array of values
values = np.array([-1, 0, 1])
# Define a mask (compute tanh only where mask is True)
mask = np.array([True, False, True])
# Compute hyperbolic tangent values where mask is True
result = np.tanh(values, where=mask)
# Print the results
print("Computed tanh values with mask:", result)
Output:
Computed tanh values with mask: [-7.61594156e-001 3.33209524e-315 7.61594156e-001]

The hyperbolic tangent values are computed only for elements where mask=True
. The other values remain unchanged.