Python NumPy ufunc Hyperbolic Functions – A Complete Guide

Last updated 3 weeks, 4 days ago | 86 views 75     5

Tags:- Python NumPy

Hyperbolic functions play a crucial role in advanced mathematics, physics, and engineering. Just as trigonometric functions describe circles, hyperbolic functions describe hyperbolas. NumPy provides fast, vectorized universal functions (ufuncs) to compute these functions efficiently.

In this article, you'll learn how to use NumPy's hyperbolic ufuncs, including their inverses, use cases, and best practices.


What Are Hyperbolic Functions?

Hyperbolic functions are analogs of trigonometric functions but for a hyperbola. The primary hyperbolic functions include:

Function Definition
sinh(x) ex−e−x2\frac{e^x - e^{-x}}{2}
cosh(x) ex+e−x2\frac{e^x + e^{-x}}{2}
tanh(x) sinh⁡(x)cosh⁡(x)\frac{\sinh(x)}{\cosh(x)}

They are useful in:

  • Relativity and spacetime geometry

  • Signal processing

  • Calculus and differential equations

  • Neural network activations (e.g. tanh)


NumPy Hyperbolic ufuncs

✅ Basic Hyperbolic Functions

Function Description
np.sinh(x) Hyperbolic sine
np.cosh(x) Hyperbolic cosine
np.tanh(x) Hyperbolic tangent

Inverse Hyperbolic Functions

Function Description
np.arcsinh(x) Inverse hyperbolic sine
np.arccosh(x) Inverse hyperbolic cosine
np.arctanh(x) Inverse hyperbolic tangent

Step-by-Step Code Examples


1. Basic Hyperbolic Functions

import numpy as np

x = np.array([-2, -1, 0, 1, 2])

print("sinh(x):", np.sinh(x))
print("cosh(x):", np.cosh(x))
print("tanh(x):", np.tanh(x))

✅ Output:

sinh(x): [-3.62686 -1.1752  0.       1.1752  3.62686]
cosh(x): [3.7622  1.5431  1.      1.5431  3.7622]
tanh(x): [-0.9640 -0.7615  0.      0.7615  0.9640]

2. Inverse Hyperbolic Functions

x = np.array([-2, -1, 0, 1, 2])

print("arcsinh(x):", np.arcsinh(x))   # Defined for all real x
print("arccosh(x):", np.arccosh(x[x >= 1]))  # Defined for x >= 1
print("arctanh(x):", np.arctanh(x[np.abs(x) < 1]))  # |x| < 1

✅ Output:

arcsinh(x): [-1.4436 -0.8814  0.      0.8814  1.4436]
arccosh(x): [0.      1.3169]
arctanh(x): [-0.5493  0.      0.5493]

3. Graphing Hyperbolic Functions (Optional)

import matplotlib.pyplot as plt

x = np.linspace(-4, 4, 400)
plt.plot(x, np.sinh(x), label='sinh(x)')
plt.plot(x, np.cosh(x), label='cosh(x)')
plt.plot(x, np.tanh(x), label='tanh(x)')
plt.title("Hyperbolic Functions")
plt.xlabel("x")
plt.ylabel("y")
plt.grid(True)
plt.legend()
plt.show()

✅ Full Working Example

import numpy as np

# Input array
x = np.linspace(-2, 2, 5)

print("Input:", x)

# Basic hyperbolic functions
print("sinh:", np.sinh(x))
print("cosh:", np.cosh(x))
print("tanh:", np.tanh(x))

# Inverse functions
print("arcsinh:", np.arcsinh(x))
print("arccosh (x >= 1):", np.arccosh(x[x >= 1]))
print("arctanh (|x| < 1):", np.arctanh(x[np.abs(x) < 1]))

⚠️ Common Pitfalls

Issue Why It Happens How to Avoid
np.arccosh(x) throws error for x < 1 Domain of arccosh is [1, ∞) Filter input: x[x >= 1]
np.arctanh(x) throws error for x ≥ 1
❌ Input in degrees All inputs are in radians Don’t convert; these functions expect real numbers
❌ Overflow on large inputs Exponential growth of cosh, sinh Use float64, or limit input range

Tips and Best Practices

  • Use np.clip(x, -0.9999, 0.9999) to avoid arctanh domain errors.

  • Combine tanh with np.gradient for signal smoothing or activation functions.

  • For large inputs, consider logarithmic equivalents of inverse hyperbolic functions to avoid overflow.

  • Plot functions to visualize growth or decay trends.


Mathematical Reference

Function Formula
sinh(x) ex−e−x2\frac{e^x - e^{-x}}{2}
cosh(x) ex+e−x2\frac{e^x + e^{-x}}{2}
tanh(x) sinh⁡(x)cosh⁡(x)\frac{\sinh(x)}{\cosh(x)}
arcsinh(x) ln⁡(x+x2+1)\ln(x + \sqrt{x^2 + 1})
arccosh(x) ln⁡(x+x2−1),  x≥1\ln(x + \sqrt{x^2 - 1}), \; x \geq 1
arctanh(x) (\frac{1}{2} \ln\left(\frac{1 + x}{1 - x}\right), ;

Summary Table

Function Description Valid Input Domain Output Range
np.sinh(x) Hyperbolic sine
np.cosh(x) Hyperbolic cosine [1, ∞)
np.tanh(x) Hyperbolic tangent (-1, 1)
np.arcsinh(x) Inverse hyperbolic sine
np.arccosh(x) Inverse hyperbolic cosine [1, ∞) [0, ∞)
np.arctanh(x) Inverse hyperbolic tangent (-1, 1)

Conclusion

NumPy's hyperbolic ufuncs provide a fast, reliable, and vectorized way to handle computations involving hyperbolic geometry, signal modeling, and advanced mathematics. With careful domain handling and input validation, these tools can be seamlessly integrated into a wide range of scientific computing applications.