The idea: one machine after another
Think of a function as a machine: a number goes in, one number comes out. Composing two functions means connecting two machines, so the output of the first is the input of the second. Feed x into g, then feed g(x) into f, and you get f(g(x)). That new function is written f∘g ("f after g"):
(f∘g)(x) = f(g(x))
Read it from the right: the function next to x acts first. With f(x) = 2x + 1 and g(x) = x², the input 2 becomes g(2) = 4 and then f(4) = 9, so (f∘g)(2) = 9. Substituting the formula of g for x in f gives the whole new function at once: f(g(x)) = 2x² + 1. (Demo 1: f(g(2)) step by step)
Composing on the graph
The canvas does the same thing on the plot. From x go up to the green curve: the height is g(x). Now that height has to become an input, a position on the x-axis. The line y = x does exactly that: go across to it, and you are above the x-value g(x). Go up or down to the blue curve: the height is f(g(x)). Go back across to the starting x, and you have a point of the purple curve y = f(g(x)).
Order matters
f∘g and g∘f are usually different functions. With f(x) = 2x + 1 and g(x) = x + 3:
| composition | formula | at x = 1 |
|---|---|---|
| f∘g: add 3, then double and add 1 | 2(x + 3) + 1 = 2x + 7 | f(g(1)) = f(4) = 9 |
| g∘f: double and add 1, then add 3 | (2x + 1) + 3 = 2x + 4 | g(f(1)) = g(3) = 6 |
Putting on socks and then shoes is not the same as shoes and then socks. Composition is associative, though: f∘(g∘h) = (f∘g)∘h, because both mean "h, then g, then f". (Demo 2: order matters, f∘g vs g∘f)
The domain of a composition
f(g(x)) exists only when g(x) exists and g(x) is an allowed input of f. Take f(x) = √x and g(x) = 2x + 1. At x = −2, g gives −3, and √(−3) is not a real number. So x = −2 is not in the domain of f∘g, even though it is fine for g. The domain of √(2x + 1) is 2x + 1 ≥ 0, that is x ≥ −½, and the purple curve starts there. (Demo 6: domain of √(2x + 1))
Undoing a function: the inverse
The inverse f⁻¹ is the machine that undoes f: if f turns a into b, then f⁻¹ turns b back into a. So f⁻¹(f(x)) = x and f(f⁻¹(x)) = x. On the graph, f(a) = b is the point (a, b) and f⁻¹(b) = a is the point (b, a). Swapping the coordinates of every point reflects the plane across the line y = x, so the graph of f⁻¹ is the graph of f mirrored in y = x. (Demo 3: mirror 2x + 1 in y = x)
To find the formula, write y = f(x), solve for x, and rename: y = 2x + 1 gives x = (y − 1)/2, so f⁻¹(x) = (x − 1)/2. The same idea gives ∛x for x³ and log₂ x for 2ˣ, which is why the logarithm's graph is the exponential's graph turned over; see Exponentials and Logarithms. (Demo 4: 2ˣ and log₂ x)
| f(x) | f⁻¹(x) | note |
|---|---|---|
| 2x + 1 | (x − 1)/2 | undo the steps in reverse order |
| x + 3 | x − 3 | |
| x³ | ∛x | one-to-one everywhere |
| x², x ≥ 0 | √x | needs the restriction |
| 2ˣ | log₂ x | defined for x > 0 only |
f⁻¹ means the inverse function, not 1/f. For f(x) = 2x + 1, 1/f(x) = 1/(2x + 1), which is something else.
One-to-one and the horizontal line test
An inverse can only exist if no two inputs give the same output. x² sends both −1 and 1 to 1, so "undo x² at 1" has two answers. Mirrored, the parabola becomes a sideways curve with two points above x = 1: it fails the vertical line test and is not a function. The rule on the original graph is the horizontal line test: f has an inverse when every horizontal line meets its graph at most once (one-to-one). The fix is to restrict the domain: x² with x ≥ 0 is one-to-one, and its inverse is √x.
The restriction leaves a trace. √(x²) is not x but |x|: f⁻¹(f(−2)) = √4 = 2. The pair only undoes each other on the part of the domain that was kept. (Demo 5: x² has no inverse until x ≥ 0)
The inverse of a composition
To undo "socks, then shoes", take off the shoes first. In symbols, (f∘g)⁻¹ = g⁻¹∘f⁻¹: undo the last step first. For f∘g(x) = 2(x + 3) + 1, undo "×2 + 1" with (x − 1)/2 and then undo "+3" with − 3: (f∘g)⁻¹(x) = (x − 1)/2 − 3. Matrices follow the same rule, (AB)⁻¹ = B⁻¹A⁻¹; see Composing Transforms and the Inverse Matrix.
Where it is used
- Transformations: y = a·f(b(x − h)) + k is f composed with a shift and stretch on the inside and another on the outside. See Function Transformations.
- Solving equations: to solve 2ˣ⁺³ = 32, apply the inverses of the outer step first: log₂ gives x + 3 = 5, so x = 2.
- Calculus: the chain rule differentiates a composition, (f∘g)′ = f′(g(x))·g′(x).
- Programs: a pipeline of functions (Unix pipes, method chains, compose in functional languages) is composition; encode/decode and encrypt/decrypt are inverse pairs.
What the page leaves out
Compositions of three or more functions, inverses of functions that are not on the list (rational functions, trig functions and their restricted domains for arcsin and arccos), the derivative of an inverse, f⁻¹′(b) = 1/f′(a), and functions that are their own inverse, such as 1/x and −x.
See where this page fits on the Algebra & Functions mind map.