NumPy cosh()

The numpy.cosh() function computes the hyperbolic cosine of each element in an input array. It is equivalent to \( \dfrac{e^x + e^{-x}}{2} \) and can also be expressed using the complex cosine function as \( \cosh(x) = \cos(1j \cdot x) \).

Syntax

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

Parameters

ParameterTypeDescription
xarray_likeInput values for which to compute the hyperbolic cosine.
outndarray, None, or tuple of ndarray and None, optionalOptional output array to store the result. If None, a new array is created.
wherearray_like, optionalBoolean mask that determines which elements should be computed. Other elements retain their original values.
castingstr, optionalDefines the casting behavior when computing the hyperbolic cosine function.
orderstr, optionalSpecifies the memory 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 hyperbolic cosine values of the input array elements. If the input is a scalar, a scalar is returned.


Examples

1. Computing Hyperbolic Cosine of a Single Value

Here, we compute the hyperbolic cosine of a single input value.

</>
Copy
import numpy as np

# Define a single input value
x = 0  

# Compute the hyperbolic cosine
result = np.cosh(x)

# Print the result
print("cosh(0):", result)

Output:

cosh(0): 1.0

2. Computing Hyperbolic Cosine for an Array of Values

We compute the hyperbolic cosine values for multiple elements in an array.

</>
Copy
import numpy as np

# Define an array of values
values = np.array([-2, -1, 0, 1, 2])

# Compute the hyperbolic cosine for each value
cosh_values = np.cosh(values)

# Print the results
print("Input values:", values)
print("Hyperbolic cosine values:", cosh_values)

Output:

Input values: [-2 -1  0  1  2]
Hyperbolic cosine values: [3.76219569 1.54308063 1.         1.54308063 3.76219569]

3. Using the out Parameter

Using an output array to store the results instead of creating a new array.

</>
Copy
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(values.shape)

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

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

Output:

Computed hyperbolic cosine values: [1.54308063 1.         1.54308063]

4. Using the where Parameter

Using a condition to compute hyperbolic cosine only for selected elements.

</>
Copy
import numpy as np

# Define an array of input values
values = np.array([-2, -1, 0, 1, 2])

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

# Compute hyperbolic cosine values where mask is True
result = np.cosh(values, where=mask)

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

Output:

Computed hyperbolic cosine values with mask: [3.76219569 0.5        1.         1.5        3.76219569]

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