Hyperbolic Functions
The hyperbolic functions , and are built from and , in the same way that sine and cosine are built from and . They describe hanging cables, appear in relativity, and is one of the classic activation functions in neural networks.
Definitions
Compare with Euler's formulas: and . Dropping the 's gives the hyperbolic versions.
| Function | Domain | Range | Symmetry | Value at 0 |
|---|---|---|---|---|
| all reals | all reals | odd | 0 | |
| all reals | even | 1 | ||
| all reals | odd | 0 |
Unlike sine and cosine, these functions are not periodic. For large , is tiny, so and . For very negative , .
import math
print(f"{'x':>4} {'sinh':>10} {'cosh':>10} {'tanh':>9}")
for x in [-3, -1, 0, 0.5, 1, 3]:
print(f"{x:>4} {math.sinh(x):10.5f} {math.cosh(x):10.5f} {math.tanh(x):9.5f}")
x = 1.3
by_hand = (math.exp(x) - math.exp(-x)) / 2
print("sinh(1.3) by formula:", round(by_hand, 12), " math.sinh:", round(math.sinh(x), 12)) x sinh cosh tanh
-3 -10.01787 10.06766 -0.99505
-1 -1.17520 1.54308 -0.76159
0 0.00000 1.00000 0.00000
0.5 0.52110 1.12763 0.46212
1 1.17520 1.54308 0.76159
3 10.01787 10.06766 0.99505
sinh(1.3) by formula: 1.698382437293 math.sinh: 1.698382437293
Why "hyperbolic"?
The point lies on the circle . The point lies on the hyperbola (its right branch), because
This is the hyperbolic Pythagorean identity. Most trig identities have a hyperbolic twin, usually with a sign change (Osborn's rule: flip the sign of any product of two 's):
| Trigonometric | Hyperbolic |
|---|---|
(, and complete the family.)
import sympy as sp
x = sp.Symbol("x", real=True)
print("cosh² − sinh² :", sp.simplify(sp.cosh(x)**2 - sp.sinh(x)**2))
print("cosh 2x check :", sp.simplify(sp.cosh(2*x) - (sp.cosh(x)**2 + sp.sinh(x)**2)))
print("tanh as exp :", sp.tanh(x).rewrite(sp.exp))
print("asinh(x) :", sp.asinh(x).rewrite(sp.log))cosh² − sinh² : 1
cosh 2x check : 0
tanh as exp : (exp(x) - exp(-x))/(exp(x) + exp(-x))
asinh(x) : log(x + sqrt(x**2 + 1))
Inverse hyperbolic functions
Because they are built from , their inverses are logarithms:
To derive the first, set , so . Multiply by : , a quadratic in . Its positive root is , so . In Python they are math.asinh, math.acosh, math.atanh.
The catenary
A chain or cable hanging freely between two posts takes the shape , called a catenary (from the Latin for chain). It looks like a parabola but is not one: it grows exponentially rather than quadratically further out. Inverting the catenary's shape gives the strongest arch for its own weight, which is why the Gateway Arch in St. Louis follows a (weighted) catenary.
The derivatives are also simple: and , with no minus sign. You will meet them in the calculus course's hyperbolic lesson and, in depth, in /calculus-stewart/hyperbolic-functions/.
In software
- as an activation function. A neural network neuron computes . squashes any number into , is zero-centred and smooth, and its derivative is the cheap . LSTMs and GRUs (recurrent networks) still use it inside their gates. It is closely related to the logistic sigmoid: .
- Soft clipping in audio. Guitar distortion plugins use to push loud samples smoothly toward instead of clipping them hard.
- Numerical care. Computing from its definition overflows for large ( is infinite), but
math.tanh(1000)returns 1.0. Library functions handle these edge cases; prefer them over hand-written formulas.
import math
def tanh_naive(x):
return (math.exp(x) - math.exp(-x)) / (math.exp(x) + math.exp(-x))
print("tanh(2) naive vs library:", round(tanh_naive(2), 12), round(math.tanh(2), 12))
try:
tanh_naive(1000)
except OverflowError as e:
print("naive tanh(1000):", e)
print("math.tanh(1000):", math.tanh(1000))
sigmoid = lambda x: 1 / (1 + math.exp(-x))
print("sigmoid(1.5) =", round(sigmoid(1.5), 12), " (1+tanh(0.75))/2 =", round((1 + math.tanh(0.75)) / 2, 12))
print("soft clip:", [round(math.tanh(s), 3) for s in (0.1, 0.5, 1.0, 2.0, 5.0)])tanh(2) naive vs library: 0.964027580076 0.964027580076
naive tanh(1000): math range error
math.tanh(1000): 1.0
sigmoid(1.5) = 0.817574476194 (1+tanh(0.75))/2 = 0.817574476194
soft clip: [0.1, 0.462, 0.762, 0.964, 1.0]
Exercises
Exercise 1: Evaluate exactly
Find and as fractions.
Answer
, .
Solution
Exercise 2: From one to the others
If , find and .
Answer
, .
Solution
Exercise 3: Prove an identity
Prove that , and use it to show .
Answer
Both follow from the definitions and the exponent rule.
Solution
This is the hyperbolic version of De Moivre's formula.
Exercise 4: Inverse by formula
Compute with the log formula and compare with math.asinh. Then compute both ways.
Answer
, .
Solution
import math
print(round(math.log(2 + math.sqrt(5)), 6), round(math.asinh(2), 6))
print(round(0.5 * math.log((1 + 0.6) / (1 - 0.6)), 6), round(math.atanh(0.6), 6))1.443635 1.443635
0.693147 0.693147
For : , and .
Exercise 5: Sag of a cable
A cable hangs as between posts at and (metres). How far does the middle sag below the post tops?
Answer
About 5.89 m.
Solution
import math
print(round(20 * math.cosh(15 / 20) - 20, 3))5.894
Test yourself
Which identity is correct?
What is the range of ?
- All reals
What is ?
- 0
- 1
- Undefined