TExcelTAD
Home › Calculus Essentials › Composition and Inverses

Composition and Inverses

Composition feeds the output of one function into another, and an inverse runs a function backwards. Composition is the idea behind the chain rule, and inverses explain why logarithms undo exponentials and why arcsin⁡\arcsin exists. Both are everyday tools in programming too.

Composition

The composition f∘gf \circ g (read "ff after gg") means: apply gg first, then apply ff to the result.

(f∘g)(x)=f(g(x))(f \circ g)(x) = f(g(x))

With f(x)=x2f(x) = x^2 and g(x)=x+3g(x) = x + 3:

(f∘g)(x)=f(x+3)=(x+3)2(g∘f)(x)=g(x2)=x2+3\begin{aligned} (f \circ g)(x) &= f(x + 3) = (x + 3)^2 \\ (g \circ f)(x) &= g(x^2) = x^2 + 3 \end{aligned}

The two orders give different functions, so composition is not commutative. Test at x=1x = 1: (f∘g)(1)=16(f \circ g)(1) = 16 but (g∘f)(1)=4(g \circ f)(1) = 4.

The domain of f∘gf \circ g contains the xx values where g(x)g(x) is defined and g(x)g(x) lies in the domain of ff. For f(x)=xf(x) = \sqrt{x} and g(x)=x−5g(x) = x - 5, the composition x−5\sqrt{x - 5} needs x≥5x \ge 5.

python · try it yourself
import sympy as sp

x = sp.symbols('x')
f = x**2
g = x + 3

f_of_g = f.subs(x, g)
g_of_f = g.subs(x, f)
print("f(g(x)) =", sp.expand(f_of_g))
print("g(f(x)) =", g_of_f)
print("at x = 1:", f_of_g.subs(x, 1), "vs", g_of_f.subs(x, 1))
f(g(x)) = x**2 + 6*x + 9
g(f(x)) = x**2 + 3
at x = 1: 16 vs 4

Decomposing

Calculus often needs the reverse: seeing a complicated function as a composition of simple ones. That is the first step of the chain rule.

Function Inner g(x)g(x) Outer f(u)f(u)
(3x−1)5(3x - 1)^5 3x−13x - 1 u5u^5
x2+4\sqrt{x^2 + 4} x2+4x^2 + 4 u\sqrt{u}
e−x2e^{-x^2} −x2-x^2 eue^u
sin⁡(2x)\sin(2x) 2x2x sin⁡u\sin u
ln⁡(1+ex)\ln(1 + e^x) 1+ex1 + e^x ln⁡u\ln u

Inverse functions

The inverse f−1f^{-1} undoes ff: if f(a)=bf(a) = b, then f−1(b)=af^{-1}(b) = a.

f−1(f(x))=xfor every x in the domain of ff(f−1(y))=yfor every y in the range of f\begin{aligned} f^{-1}(f(x)) &= x && \text{for every } x \text{ in the domain of } f \\ f(f^{-1}(y)) &= y && \text{for every } y \text{ in the range of } f \end{aligned}

Note: f−1(x)f^{-1}(x) means the inverse function, not 1/f(x)1/f(x). The reciprocal is written (f(x))−1(f(x))^{-1} or 1/f(x)1/f(x).

Only one-to-one functions have inverses, because the inverse must send each output back to a single input. The horizontal line test checks this on a graph: no horizontal line may hit the curve twice. x2x^2 fails (both 2 and −2 give 4), but if you restrict it to x≥0x \ge 0 it passes, and its inverse is x\sqrt{x}. That restriction is exactly why  \sqrt{\ } returns only the non-negative root.

Finding an inverse

  1. Write y=f(x)y = f(x).
  2. Solve for xx in terms of yy.
  3. Swap the names: the result is f−1(x)f^{-1}(x).

For f(x)=(2x+1)/3f(x) = (2x + 1)/3:

y=2x+133y=2x+1x=3y−12f−1(x)=3x−12\begin{aligned} y &= \frac{2x + 1}{3} \\ 3y &= 2x + 1 \\ x &= \frac{3y - 1}{2} \\ f^{-1}(x) &= \frac{3x - 1}{2} \end{aligned}

The domain of f−1f^{-1} is the range of ff, and the other way round. Graphically, the graph of f−1f^{-1} is the mirror image of the graph of ff in the line y=xy = x.

python · try it yourself
import sympy as sp

x, y = sp.symbols('x y')
f = (2*x + 1) / 3

sol = sp.solve(sp.Eq(y, f), x)[0]     # solve y = f(x) for x
f_inv = sol.subs(y, x)                 # rename y -> x
print("f^-1(x) =", f_inv)
print("f^-1(f(x)) =", sp.simplify(f_inv.subs(x, f)))
print("f(f^-1(x)) =", sp.simplify(f.subs(x, f_inv)))
f^-1(x) = 3*x/2 - 1/2
f^-1(f(x)) = x
f(f^-1(x)) = x

Important inverse pairs

Function Inverse Restriction
x+ax + a x−ax - a none
a⋅xa \cdot x (a≠0a \ne 0) x/ax / a none
x2x^2 x\sqrt{x} x≥0x \ge 0
x3x^3 x3\sqrt[3]{x} none
exe^x ln⁡x\ln x inverse needs x>0x > 0
10x10^x log⁡10x\log_{10} x inverse needs x>0x > 0
sin⁡x\sin x arcsin⁡x\arcsin x −π/2≤x≤π/2-\pi/2 \le x \le \pi/2
tan⁡x\tan x arctan⁡x\arctan x −π/2<x<π/2-\pi/2 < x < \pi/2

When there is no formula, you can still invert a monotonic function numerically by searching for the input that produces a given output:

python · try it yourself
import math

def f(x):
    return x**3 + x          # strictly increasing, so one-to-one

def inverse(y, lo=-100.0, hi=100.0):
    for _ in range(60):      # bisection: halve the bracket each time
        mid = (lo + hi) / 2
        if f(mid) < y:
            lo = mid
        else:
            hi = mid
    return (lo + hi) / 2

for target in [2, 10, 30]:
    x = inverse(target)
    print(f"f^-1({target}) = {x:.6f}   check f(x) = {f(x):.6f}")
f^-1(2) = 1.000000   check f(x) = 2.000000
f^-1(10) = 2.000000   check f(x) = 10.000000
f^-1(30) = 3.000000   check f(x) = 30.000000

In software

  • Pipelines are compositions. Unix pipes, map chains, middleware stacks and neural network layers all apply one function to the output of another. A deep network is f5∘f4∘f3∘f2∘f1f_5 \circ f_4 \circ f_3 \circ f_2 \circ f_1, and backpropagation differentiates that composition with the chain rule.
  • Encode / decode pairs are inverses: serialise and parse, compress and decompress, encrypt and decrypt. Round-trip tests check decode(encode(x)) == x, the identity f−1(f(x))=xf^{-1}(f(x)) = x.
  • Hash functions are deliberately not invertible: many inputs map to the same output, so there is no f−1f^{-1}.
  • Graphics converts between spaces with a matrix and its inverse, for example screen to world coordinates (Screen to World).

Exercises

Exercise 1: Compose by hand

With f(x)=2x−1f(x) = 2x - 1 and g(x)=x2+1g(x) = x^2 + 1, find f(g(x))f(g(x)), g(f(x))g(f(x)) and (f∘f)(x)(f \circ f)(x).

Answer

f(g(x))=2x2+1f(g(x)) = 2x^2 + 1, g(f(x))=4x2−4x+2g(f(x)) = 4x^2 - 4x + 2, f(f(x))=4x−3f(f(x)) = 4x - 3.

Solution
f(g(x))=2(x2+1)−1=2x2+1g(f(x))=(2x−1)2+1=4x2−4x+1+1=4x2−4x+2f(f(x))=2(2x−1)−1=4x−3\begin{aligned} f(g(x)) &= 2(x^2 + 1) - 1 = 2x^2 + 1 \\ g(f(x)) &= (2x - 1)^2 + 1 = 4x^2 - 4x + 1 + 1 = 4x^2 - 4x + 2 \\ f(f(x)) &= 2(2x - 1) - 1 = 4x - 3 \end{aligned}

Exercise 2: Decompose

Write h(x)=1−cos⁡xh(x) = \sqrt{1 - \cos x} as a composition f(g(x))f(g(x)) and give its domain.

Answer

Inner g(x)=1−cos⁡xg(x) = 1 - \cos x, outer f(u)=uf(u) = \sqrt{u}; the domain is all reals.

Solution

Since cos⁡x≤1\cos x \le 1, the inner value 1−cos⁡x1 - \cos x is always ≥0\ge 0, so the square root is defined for every xx. (You could also split it into three layers: cos⁡x\cos x, then 1−u1 - u, then u\sqrt{u}.)

Exercise 3: Find an inverse

Find the inverse of f(x)=5−3xf(x) = 5 - 3x, then check f(f−1(x))=xf(f^{-1}(x)) = x with SymPy.

Answer

f−1(x)=(5−x)/3f^{-1}(x) = (5 - x)/3.

Solution
y=5−3x  ⇒  3x=5−y  ⇒  x=5−y3y = 5 - 3x \;\Rightarrow\; 3x = 5 - y \;\Rightarrow\; x = \frac{5 - y}{3}
python · try it yourself
import sympy as sp

x = sp.symbols('x')
f = 5 - 3*x
f_inv = (5 - x) / 3
print(sp.simplify(f.subs(x, f_inv)), sp.simplify(f_inv.subs(x, f)))
x x

Exercise 4: Inverse of a growth formula

A balance grows as B(t)=1000⋅1.05tB(t) = 1000 \cdot 1.05^t. Find the inverse t(B)t(B) and use it to compute when the balance reaches 2000.

Answer

t=ln⁡(B/1000)/ln⁡(1.05)t = \ln(B/1000)/\ln(1.05); the balance doubles after about 14.21 years.

Solution
B=1000⋅1.05t  ⇒  B1000=1.05t  ⇒  ln⁡B1000=tln⁡1.05  ⇒  t=ln⁡(B/1000)ln⁡1.05B = 1000 \cdot 1.05^t \;\Rightarrow\; \frac{B}{1000} = 1.05^t \;\Rightarrow\; \ln\frac{B}{1000} = t \ln 1.05 \;\Rightarrow\; t = \frac{\ln(B/1000)}{\ln 1.05}
python · try it yourself
import math

t = math.log(2000 / 1000) / math.log(1.05)
print(round(t, 2), round(1000 * 1.05**t, 6))
14.21 2000.0

Test yourself

If f(x)=x+4f(x) = x + 4 and g(x)=3xg(x) = 3x, what is f(g(2))f(g(2))?

  1. 18
  2. 10
  3. 12
  4. 24

Why does f(x)=x2f(x) = x^2 on all reals have no inverse?

  1. It is not continuous
  2. Its range is all reals
  3. It is not one-to-one: f(3)=f(−3)f(3) = f(-3)
  4. Squares cannot be undone

What does f−1(x)f^{-1}(x) mean?

  1. 1/f(x)1/f(x)
  2. The function that undoes ff
  3. −f(x)-f(x)
  4. f(−x)f(-x)