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 exists. Both are everyday tools in programming too.
Composition
The composition (read " after ") means: apply first, then apply to the result.
With and :
The two orders give different functions, so composition is not commutative. Test at : but .
The domain of contains the values where is defined and lies in the domain of . For and , the composition needs .
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 | Outer |
|---|---|---|
Inverse functions
The inverse undoes : if , then .
Note: means the inverse function, not . The reciprocal is written or .
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. fails (both 2 and −2 give 4), but if you restrict it to it passes, and its inverse is . That restriction is exactly why returns only the non-negative root.
Finding an inverse
- Write .
- Solve for in terms of .
- Swap the names: the result is .
For :
The domain of is the range of , and the other way round. Graphically, the graph of is the mirror image of the graph of in the line .
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 |
|---|---|---|
| none | ||
| () | none | |
| none | ||
| inverse needs | ||
| inverse needs | ||
When there is no formula, you can still invert a monotonic function numerically by searching for the input that produces a given output:
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,
mapchains, middleware stacks and neural network layers all apply one function to the output of another. A deep network is , 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 . - Hash functions are deliberately not invertible: many inputs map to the same output, so there is no .
- 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 and , find , and .
Answer
, , .
Solution
Exercise 2: Decompose
Write as a composition and give its domain.
Answer
Inner , outer ; the domain is all reals.
Solution
Since , the inner value is always , so the square root is defined for every . (You could also split it into three layers: , then , then .)
Exercise 3: Find an inverse
Find the inverse of , then check with SymPy.
Answer
.
Solution
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 . Find the inverse and use it to compute when the balance reaches 2000.
Answer
; the balance doubles after about 14.21 years.
Solution
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 and , what is ?
- 18
- 10
- 12
- 24
Why does on all reals have no inverse?
- It is not continuous
- Its range is all reals
- It is not one-to-one:
- Squares cannot be undone
What does mean?
- The function that undoes