TExcelTAD
Home › Calculus Essentials › Hyperbolic Functions

Hyperbolic Functions

The hyperbolic functions sinh, cosh and tanh are built from exe^x and e−xe^{-x}. They behave like cousins of sine and cosine, with similar names and similar identities, but they describe hanging cables, special relativity and one of the best-known activation functions in neural networks, tanh.

Definitions

sinh⁡x=ex−e−x2“hyperbolic sine” (say “shine” or “sinch”)cosh⁡x=ex+e−x2“hyperbolic cosine” (“kosh”)tanh⁡x=sinh⁡xcosh⁡x=ex−e−xex+e−x\begin{aligned} \sinh x &= \frac{e^x - e^{-x}}{2} && \text{``hyperbolic sine'' (say ``shine'' or ``sinch'')} \\ \cosh x &= \frac{e^x + e^{-x}}{2} && \text{``hyperbolic cosine'' (``kosh'')} \\ \tanh x &= \frac{\sinh x}{\cosh x} = \frac{e^x - e^{-x}}{e^x + e^{-x}} \end{aligned}

The reciprocal functions follow the trig pattern: sech⁡x=1/cosh⁡x\operatorname{sech} x = 1/\cosh x, csch⁡x=1/sinh⁡x\operatorname{csch} x = 1/\sinh x, coth⁡x=1/tanh⁡x\coth x = 1/\tanh x.

Function Domain Range Even / odd Value at 0
sinh⁡x\sinh x all reals all reals odd 0
cosh⁡x\cosh x all reals [1,∞)[1, \infty) even 1
tanh⁡x\tanh x all reals (−1,1)(-1, 1) odd 0

cosh⁡x\cosh x is the even part of exe^x and sinh⁡x\sinh x the odd part: ex=cosh⁡x+sinh⁡xe^x = \cosh x + \sinh x. For large positive xx, e−xe^{-x} is tiny, so sinh⁡x≈cosh⁡x≈ex/2\sinh x \approx \cosh x \approx e^x/2 and tanh⁡x\tanh x gets very close to 1.

python · try it yourself
import math

print("   x     sinh      cosh      tanh")
for x in [-3, -1, 0, 0.5, 1, 3]:
    s = (math.exp(x) - math.exp(-x)) / 2
    c = (math.exp(x) + math.exp(-x)) / 2
    print(f"{x:4}  {s:8.4f}  {c:8.4f}  {s / c:8.4f}")
print("library check:", math.sinh(1), math.cosh(1), math.tanh(1))
   x     sinh      cosh      tanh
  -3  -10.0179   10.0677   -0.9951
  -1   -1.1752    1.5431   -0.7616
   0    0.0000    1.0000    0.0000
 0.5    0.5211    1.1276    0.4621
   1    1.1752    1.5431    0.7616
   3   10.0179   10.0677    0.9951
library check: 1.1752011936438014 1.5430806348152437 0.7615941559557649

Why "hyperbolic"?

The trig functions live on the circle x2+y2=1x^2 + y^2 = 1: the point (cos⁡t,sin⁡t)(\cos t, \sin t) is always on it. The hyperbolic functions live on the hyperbola x2−y2=1x^2 - y^2 = 1: the point (cosh⁡t,sinh⁡t)(\cosh t, \sinh t) is always on its right branch, because

cosh⁡2t−sinh⁡2t=1\cosh^2 t - \sinh^2 t = 1

Compare this with cos⁡2t+sin⁡2t=1\cos^2 t + \sin^2 t = 1. Only a sign is different, and that sign difference runs through all the identities:

Trigonometric Hyperbolic
cos⁡2x+sin⁡2x=1\cos^2 x + \sin^2 x = 1 cosh⁡2x−sinh⁡2x=1\cosh^2 x - \sinh^2 x = 1
sin⁡2x=2sin⁡xcos⁡x\sin 2x = 2\sin x\cos x sinh⁡2x=2sinh⁡xcosh⁡x\sinh 2x = 2\sinh x\cosh x
cos⁡2x=cos⁡2x−sin⁡2x\cos 2x = \cos^2 x - \sin^2 x cosh⁡2x=cosh⁡2x+sinh⁡2x\cosh 2x = \cosh^2 x + \sinh^2 x
1+tan⁡2x=sec⁡2x1 + \tan^2 x = \sec^2 x 1−tanh⁡2x=sech⁡2x1 - \tanh^2 x = \operatorname{sech}^2 x
(sin⁡x)′=cos⁡x(\sin x)' = \cos x (sinh⁡x)′=cosh⁡x(\sinh x)' = \cosh x
(cos⁡x)′=−sin⁡x(\cos x)' = -\sin x (cosh⁡x)′=+sinh⁡x(\cosh x)' = +\sinh x
python · try it yourself
import sympy as sp

x = sp.symbols('x')
print(sp.simplify((sp.cosh(x)**2 - sp.sinh(x)**2).rewrite(sp.exp)))
print(sp.simplify((2*sp.sinh(x)*sp.cosh(x) - sp.sinh(2*x)).rewrite(sp.exp)))
print(sp.diff(sp.sinh(x), x), "|", sp.diff(sp.cosh(x), x), "|", sp.diff(sp.tanh(x), x))
1
0
cosh(x) | sinh(x) | 1 - tanh(x)**2

.rewrite(sp.exp) writes each function in terms of exe^x, which makes the cancellation easy for simplify. The derivative of tanh, 1−tanh⁡2x1 - \tanh^2 x, is the same as sech⁡2x\operatorname{sech}^2 x.

Inverse hyperbolic functions

Because the hyperbolic functions are built from exe^x, their inverses are logarithms:

arsinh⁡x=ln⁡(x+x2+1)all realsarcosh⁡x=ln⁡(x+x2−1)x≥1artanh⁡x=12ln⁡1+x1−x−1<x<1\begin{aligned} \operatorname{arsinh} x &= \ln\left(x + \sqrt{x^2 + 1}\right) && \text{all reals} \\ \operatorname{arcosh} x &= \ln\left(x + \sqrt{x^2 - 1}\right) && x \ge 1 \\ \operatorname{artanh} x &= \tfrac{1}{2}\ln\frac{1 + x}{1 - x} && -1 < x < 1 \end{aligned}

In Python these are math.asinh, math.acosh, math.atanh (and np.arcsinh and so on).

The hanging cable

A chain or cable hanging freely between two posts takes the shape of a catenary, y=acosh⁡(x/a)y = a\cosh(x/a), not a parabola as Galileo guessed. The two curves look alike near the bottom, because cosh⁡(x)≈1+x2/2\cosh(x) \approx 1 + x^2/2 for small xx, but they separate further out.

In software: tanh as an activation

tanh⁡\tanh squashes any input into (−1,1)(-1, 1). It is zero-centred and odd, which made it popular in early neural networks and it is still used inside LSTM cells and some attention variants. It is closely related to the sigmoid σ\sigma:

tanh⁡x=2σ(2x)−1where σ(z)=11+e−z\tanh x = 2\sigma(2x) - 1 \qquad \text{where } \sigma(z) = \frac{1}{1 + e^{-z}}

so tanh is just a sigmoid stretched to the range (−1,1)(-1, 1). Its derivative 1−tanh⁡2x1 - \tanh^2 x is easy to compute from the output value itself, which backpropagation exploits. But for large ∣x∣\lvert x \rvert the derivative is nearly zero: the neuron saturates and learning slows down (the "vanishing gradient" problem).

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-4, 4, 401)
t = np.tanh(x)
sig = 1 / (1 + np.exp(-x))

plt.figure(figsize=(6, 4))
plt.plot(x, t, label="tanh x")
plt.plot(x, 1 - t**2, "--", label="derivative 1 − tanh²x")
plt.plot(x, sig, ":", label="sigmoid x")
plt.grid(alpha=0.3); plt.legend()
plt.title("tanh, its derivative and the sigmoid")

print("tanh(x) == 2σ(2x) − 1 :", np.allclose(t, 2 / (1 + np.exp(-2 * x)) - 1))
for v in [0, 1, 2, 4]:
    print(f"slope at x = {v}: {1 - np.tanh(v)**2:.5f}")
tanh(x) == 2σ(2x) − 1 : True
slope at x = 0: 1.00000
slope at x = 1: 0.41997
slope at x = 2: 0.07065
slope at x = 4: 0.00134

At x=4x = 4 the slope is about 0.0013: a gradient flowing back through that neuron is multiplied by almost zero. See Neural Networks for how modern networks avoid this.

Exercises

Exercise 1: Evaluate from the definition

Compute cosh⁡(ln⁡2)\cosh(\ln 2) and sinh⁡(ln⁡2)\sinh(\ln 2) exactly.

Answer

cosh⁡(ln⁡2)=5/4\cosh(\ln 2) = 5/4, sinh⁡(ln⁡2)=3/4\sinh(\ln 2) = 3/4.

Solution
eln⁡2=2,e−ln⁡2=12cosh⁡(ln⁡2)=2+1/22=54sinh⁡(ln⁡2)=2−1/22=34check: (54)2−(34)2=2516−916=1\begin{aligned} &e^{\ln 2} = 2, \quad e^{-\ln 2} = \tfrac{1}{2} \\ &\cosh(\ln 2) = \frac{2 + 1/2}{2} = \frac{5}{4} \\ &\sinh(\ln 2) = \frac{2 - 1/2}{2} = \frac{3}{4} \\ &\text{check: } \left(\tfrac{5}{4}\right)^2 - \left(\tfrac{3}{4}\right)^2 = \tfrac{25}{16} - \tfrac{9}{16} = 1 \end{aligned}

Exercise 2: Prove an identity

Show from the definitions that cosh⁡x+sinh⁡x=ex\cosh x + \sinh x = e^x and cosh⁡x−sinh⁡x=e−x\cosh x - \sinh x = e^{-x}, and use them to prove cosh⁡2x−sinh⁡2x=1\cosh^2 x - \sinh^2 x = 1.

Answer

cosh⁡2x−sinh⁡2x=(cosh⁡x+sinh⁡x)(cosh⁡x−sinh⁡x)=ex⋅e−x=1\cosh^2 x - \sinh^2 x = (\cosh x + \sinh x)(\cosh x - \sinh x) = e^x \cdot e^{-x} = 1.

Solution
cosh⁡x+sinh⁡x=ex+e−x+ex−e−x2=excosh⁡x−sinh⁡x=ex+e−x−ex+e−x2=e−xcosh⁡2x−sinh⁡2x=(cosh⁡x+sinh⁡x)(cosh⁡x−sinh⁡x)difference of squares=ex⋅e−x=e0=1\begin{aligned} \cosh x + \sinh x &= \frac{e^x + e^{-x} + e^x - e^{-x}}{2} = e^x \\ \cosh x - \sinh x &= \frac{e^x + e^{-x} - e^x + e^{-x}}{2} = e^{-x} \\ \cosh^2 x - \sinh^2 x &= (\cosh x + \sinh x)(\cosh x - \sinh x) && \text{difference of squares} \\ &= e^x \cdot e^{-x} = e^0 = 1 \end{aligned}

Exercise 3: tanh from sigmoid

Check numerically that tanh⁡(x)=2σ(2x)−1\tanh(x) = 2\sigma(2x) - 1 at x=0.3x = 0.3 and x=−1.7x = -1.7, with σ\sigma written by hand.

Answer

Both sides agree: about 0.291313 at x=0.3x = 0.3 and −0.935409 at x=−1.7x = -1.7.

Solution
python · try it yourself
import math

def sigma(z):
    return 1 / (1 + math.exp(-z))

for x in [0.3, -1.7]:
    print(x, round(math.tanh(x), 6), round(2 * sigma(2 * x) - 1, 6))
0.3 0.291313 0.291313
-1.7 -0.935409 -0.935409

Algebraically: 2/(1+e−2x)−1=(1−e−2x)/(1+e−2x)2/(1 + e^{-2x}) - 1 = (1 - e^{-2x})/(1 + e^{-2x}); multiply top and bottom by exe^x to get (ex−e−x)/(ex+e−x)=tanh⁡x(e^x - e^{-x})/(e^x + e^{-x}) = \tanh x.

Exercise 4: Inverse tanh

Solve tanh⁡y=0.6\tanh y = 0.6 for yy using the log formula, then check with math.atanh.

Answer

y=12ln⁡4=ln⁡2≈0.693147y = \frac{1}{2}\ln 4 = \ln 2 \approx 0.693147

Solution
y=12ln⁡1+0.61−0.6=12ln⁡1.60.4=12ln⁡4=ln⁡2y = \tfrac{1}{2}\ln\frac{1 + 0.6}{1 - 0.6} = \tfrac{1}{2}\ln\frac{1.6}{0.4} = \tfrac{1}{2}\ln 4 = \ln 2
python · try it yourself
import math

print(round(0.5 * math.log(1.6 / 0.4), 6), round(math.atanh(0.6), 6), round(math.log(2), 6))
0.693147 0.693147 0.693147

Test yourself

What is the range of cosh⁡x\cosh x?

  1. (−1,1)(-1, 1)
  2. all reals
  3. [0,∞)[0, \infty)
  4. [1,∞)[1, \infty)

Which identity is true for hyperbolic functions?

  1. cosh⁡2x+sinh⁡2x=1\cosh^2 x + \sinh^2 x = 1
  2. cosh⁡2x−sinh⁡2x=1\cosh^2 x - \sinh^2 x = 1
  3. sinh⁡2x−cosh⁡2x=1\sinh^2 x - \cosh^2 x = 1
  4. tanh⁡2x+1=sech⁡2x\tanh^2 x + 1 = \operatorname{sech}^2 x

Why can tanh neurons learn slowly when their inputs are large?

  1. tanh overflows for big inputs
  2. The derivative 1−tanh⁡2x1 - \tanh^2 x is close to 0 there, so gradients nearly vanish
  3. tanh is not differentiable
  4. tanh outputs are always negative