NumPy - Hyperbolic ufunc



Hyperbolic Universal Functions (ufunc)

Hyperbolic universal functions (ufuncs) in NumPy are functions that perform hyperbolic operations on each element of an array. These functions can calculate various hyperbolic values such as hyperbolic sine, cosine, and tangent, and their inverses for each element in the input array.

These functions operate element-wise on arrays and are optimized for performance, making them much faster than using Python loops.

NumPy Hyperbolic Sine Function

The numpy.sinh() function is used to calculate the hyperbolic sine of each element in an array.

The hyperbolic sine function is defined as sinh(x) = (ex - e-x) / 2.

Example

In the following example, we use the numpy.sinh() function to calculate the hyperbolic sine of each element in an array −

import numpy as np

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

# Calculate the hyperbolic sine of each value
sinh_values = np.sinh(values)

print("Hyperbolic sine values:", sinh_values)

The output obtained is as follows −

Hyperbolic sine values: [0.         1.17520119 3.62686041]

NumPy Hyperbolic Cosine Function

The numpy.cosh() function is used to calculate the hyperbolic cosine of each element in an array.

The hyperbolic cosine function is defined as cosh(x) = (ex + e-x) / 2.

Example

In the following example, we use the numpy.cosh() function to calculate the hyperbolic cosine of each element in an array −

import numpy as np

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

# Calculate the hyperbolic cosine of each value
cosh_values = np.cosh(values)

print("Hyperbolic cosine values:", cosh_values)

This will produce the following result −

Hyperbolic cosine values: [1.         1.54308063 3.76219569]

NumPy Hyperbolic Tangent Function

The numpy.tanh() function is used to calculate the hyperbolic tangent of each element in an array.

The hyperbolic tangent function is defined as tanh(x) = sinh(x) / cosh(x).

Example

In the following example, we use the numpy.tanh() function to calculate the hyperbolic tangent of each element in an array −

import numpy as np

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

# Calculate the hyperbolic tangent of each value
tanh_values = np.tanh(values)

print("Hyperbolic tangent values:", tanh_values)

The result produced is as follows −

Hyperbolic tangent values: [0.         0.76159416 0.96402758]
NumPy also provides functions for calculating the inverse hyperbolic functions (arcsinh, arccosh, and arctanh) of array elements. These functions return the value whose hyperbolic sine, cosine, or tangent is the given number.

NumPy Inverse Hyperbolic Sine Function

The numpy.arcsinh() function is used to calculate the inverse hyperbolic sine of each element in an array.

The inverse hyperbolic sine function is defined as arcsinh(x) = ln(x + sqrt(x2 + 1)).

Example

In this example, we use the numpy.arcsinh() function to calculate the inverse hyperbolic sine of each element in an array −

import numpy as np

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

# Calculate the inverse hyperbolic sine of each value
arcsinh_values = np.arcsinh(values)

print("Inverse hyperbolic sine values:", arcsinh_values)

We get the output as shown below −

Inverse hyperbolic sine values: [0.         0.88137359 1.44363548]

NumPy Inverse Hyperbolic Cosine Function

The numpy.arccosh() function is used to calculate the inverse hyperbolic cosine of each element in an array.

The inverse hyperbolic cosine function is defined as arccosh(x) = ln(x + sqrt(x2 - 1)).

Example

In this example, we use the numpy.arccosh() function to calculate the inverse hyperbolic cosine of each element in an array −

import numpy as np

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

# Calculate the inverse hyperbolic cosine of each value
arccosh_values = np.arccosh(values)

print("Inverse hyperbolic cosine values:", arccosh_values)

The output obtained is as follows −

Inverse hyperbolic cosine values: [0.         1.3169579  1.76274717]

NumPy Inverse Hyperbolic Tangent Function

The numpy.arctanh() function is used to calculate the inverse hyperbolic tangent of each element in an array.

The inverse hyperbolic tangent function is defined as arctanh(x) = 0.5 * ln((1 + x) / (1 - x)).

Example

In the example below, we use the numpy.arctanh() function to calculate the inverse hyperbolic tangent of each element in an array −

import numpy as np

# Define an array of values
values = np.array([0, 0.5, 0.9])

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

print("Inverse hyperbolic tangent values:", arctanh_values)

The output produced is as follows −

Inverse hyperbolic tangent values: [0.         0.54930614 1.47221949]
Advertisements