ufunc Hyperbolic
Hyperbolic functions are cousins of the trigonometric functions. Where sine and cosine are defined by a circle, the hyperbolic sine and cosine are defined by a hyperbola, and they are built from the exponential function:
| Function | Definition | NumPy |
|---|---|---|
| sinh x | (eˣ − e⁻ˣ) / 2 | np.sinh |
| cosh x | (eˣ + e⁻ˣ) / 2 | np.cosh |
| tanh x | sinh x / cosh x | np.tanh |
| inverses | np.arcsinh, np.arccosh, np.arctanh |
They appear in physics and engineering (the shape of a hanging cable is a cosh curve, called a catenary), in special relativity, and in machine learning, where tanh is a classic activation function.
sinh, cosh and tanh
import numpy as np
x = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
print(np.sinh(x).round(4))
print(np.cosh(x).round(4))
print(np.tanh(x).round(4))
# Check the definition of sinh
print(np.allclose(np.sinh(x), (np.exp(x) - np.exp(-x)) / 2))[-3.6269 -1.1752 0. 1.1752 3.6269]
[3.7622 1.5431 1. 1.5431 3.7622]
[-0.964 -0.7616 0. 0.7616 0.964 ]
True
Unlike their trigonometric cousins, these functions are not periodic. cosh is symmetric and always ≥ 1, sinh is odd (sinh(−x) = −sinh(x)), and tanh squeezes every input into the range −1 to 1.
The hyperbolic identity
The circle identity is sin² + cos² = 1. The hyperbola version has a minus sign: cosh² − sinh² = 1:
import numpy as np
x = np.linspace(-3, 3, 7)
print((np.cosh(x) ** 2 - np.sinh(x) ** 2).round(10))[1. 1. 1. 1. 1. 1. 1.]
Inverse hyperbolic functions
np.arcsinh, np.arccosh and np.arctanh undo the hyperbolic functions. Note the domains: arccosh needs inputs ≥ 1 and arctanh needs inputs strictly between −1 and 1.
import numpy as np
print(np.arcsinh(np.sinh(1.5)))
print(np.arccosh([1.0, 2.0, 10.0]).round(4))
print(np.arctanh([0.0, 0.5, 0.9]).round(4))1.5
[0. 1.317 2.9932]
[0. 0.5493 1.4722]
tanh as an activation function
In a neural network, an activation function turns a neuron's raw score into an output. tanh maps any score smoothly into (−1, 1), is centred on 0, and its slope 1 − tanh²(x) is easy to compute:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-4, 4, 200)
plt.figure(figsize=(6, 3.2))
plt.plot(x, np.tanh(x), label="tanh(x)", color="#4dabcf")
plt.plot(x, 1 - np.tanh(x) ** 2, label="slope 1 - tanh²(x)", color="#e07a5f", linestyle="--")
plt.plot(x, 1 / (1 + np.exp(-x)), label="sigmoid(x)", color="#81b29a")
plt.axhline(0, color="gray", linewidth=0.5)
plt.legend()
plt.tight_layout()
plt.show()
scores = np.array([-3.0, -0.5, 0.0, 0.5, 3.0])
print("tanh outputs:", np.tanh(scores).round(3))tanh outputs: [-0.995 -0.462 0. 0.462 0.995]
The plot shows that tanh flattens out for large positive or negative inputs (its slope goes to 0). That is the cause of the "vanishing gradient" problem in deep networks, and one reason modern networks often prefer ReLU, np.maximum(0, x).
Example: a hanging cable
A cable hanging between two poles follows the curve y = a·cosh(x / a). With a = 10 and poles 30 m apart (x from −15 to 15), how much does the cable sag?
import numpy as np
a = 10
x = np.linspace(-15, 15, 7)
y = a * np.cosh(x / a)
print(np.column_stack((x, y.round(3))))
print("sag:", (y.max() - y.min()).round(3), "m")[[-15. 23.524]
[-10. 15.431]
[ -5. 11.276]
[ 0. 10. ]
[ 5. 11.276]
[ 10. 15.431]
[ 15. 23.524]]
sag: 13.524 m
Test yourself
Which identity holds for hyperbolic functions?
- sinh^2 + cosh^2 = 1
- cosh^2 - sinh^2 = 1
- tanh = cosh / sinh
- sinh = cosh
What is the range of np.tanh(x) for real x?
- 0 to 1
- -1 to 1 (exclusive)
- Any real number
- 1 and above
What is np.cosh(0)?
- 0.0
- 1.0
- -1.0
- inf