Sub topic 3
Why the order of composition matters
This chapter assumes you can read function notation such as f(x), find the domain of a simple function, and solve linear and simple quadratic equations. Here you will see that composing functions in a different order usually gives a different function. You will also learn when the order doesn't matter, and how to avoid the mistakes that come from reading the notation backwards.
The notation hides the order
Composition feeds the output of one function into another. The notation (f∘g)(x) means f(g(x)). The function written closest to x runs first.
Reading the symbols left to right gives the wrong order. Read them right to left, or read f(g(x)) from the inside out:
- Evaluate g at x.
- Use that result as the input to f.
The symbol ∘ is read "after" or "of". So f∘g is "f after g". Many learners find it helps to say "f after g" aloud until it becomes automatic.
A worked example in both orders
Let f(x) = 2x + 3 and g(x) = x². Compute both composites at x = 4.
- (f∘g)(4): g(4) = 16, then f(16) = 2·16 + 3 = 35.
- (g∘f)(4): f(4) = 11, then g(11) = 121.
The same two functions and the same input give 35 in one order and 121 in the other. The code below shows this.
def f(x):
return 2 * x + 3
def g(x):
return x ** 2
def compose(outer, inner):
return lambda x: outer(inner(x))
f_after_g = compose(f, g) # (f∘g)(x) = 2x^2 + 3
g_after_f = compose(g, f) # (g∘f)(x) = (2x + 3)^2
print(f_after_g(4)) # g(4) = 16, then f(16) = 35
print(g_after_f(4)) # f(4) = 11, then g(11) = 121The compose helper takes the outer function first. That matches the written order f∘g, so compose(f, g) means "f after g".
Comparing the formulas
Working with formulas shows why the results differ. Substitute the inner function's whole expression wherever the outer function has x.
| Composite | Substitution | Simplified |
|---|---|---|
| (f∘g)(x) | 2(x²) + 3 | 2x² + 3 |
| (g∘f)(x) | (2x + 3)² | 4x² + 12x + 9 |
The first formula doubles the square and then adds 3. The second adds 3 to a doubled input and then squares everything. These are different operations, so they give different functions.
A diagram makes the two pipelines concrete.
Composition is not commutative
An operation is commutative if swapping its two inputs never changes the result. Addition is commutative, since 2 + 5 = 5 + 2. Composition is not, so in general f∘g ≠ g∘f.
Do not read this as "the two composites are always different". For specific inputs they can agree by coincidence. For the functions above, set the formulas equal:
2x² + 3 = 4x² + 12x + 9
Rearranging gives 2x² + 12x + 6 = 0, which simplifies to x² + 6x + 3 = 0. The quadratic formula gives x = −3 ± √6. At those two inputs the composites match. At every other input they differ, so as functions they are not equal.
Two functions are equal only when they agree on every input in their shared domain. A single matching point proves nothing, and a single mismatch (like x = 4 above) is enough to show that two functions are different.
When the order does not matter
Some pairs of functions do commute. Recognizing them helps you avoid wasted work and sharpens your sense of what the order is actually doing.
- Two shifts. If f(x) = x + 2 and g(x) = x + 5, both composites give x + 7. Adding in either order lands in the same place.
- Two scalings. If f(x) = 2x and g(x) = 3x, both composites give 6x.
- Two powers. If f(x) = x² and g(x) = x³, both composites give x⁶, because (x³)² = (x²)³.
- A function and its inverse. If f has an inverse, the composites of f and that inverse both return x. A later chapter develops this fully.
For two linear functions f(x) = ax + b and g(x) = cx + d, you can say exactly when they commute:
- (f∘g)(x) = a(cx + d) + b = acx + ad + b
- (g∘f)(x) = c(ax + b) + d = acx + cb + d
The x-coefficients are always equal (ac). The constants are ad + b and cb + d, so the composites match exactly when ad + b = cb + d. For f(x) = 2x + 1 and g(x) = 3x + 4, the constants are 2·4 + 1 = 9 and 3·1 + 4 = 7. They differ, so these two do not commute.
The next program tests candidate pairs on a range of integers. A test on sample points can prove two functions differ, but it cannot prove they are equal.
def compose(outer, inner):
return lambda x: outer(inner(x))
def agree_on(f, g, points):
return all(compose(f, g)(x) == compose(g, f)(x) for x in points)
points = range(-3, 4)
print(agree_on(lambda x: x + 2, lambda x: x + 5, points)) # True
print(agree_on(lambda x: 2 * x, lambda x: 3 * x, points)) # True
print(agree_on(lambda x: x ** 2, lambda x: x ** 3, points)) # True
print(agree_on(lambda x: 2 * x + 1, lambda x: 3 * x + 4, points)) # FalseThe first three results are True because the algebra above proves those pairs commute. The last is False, and that single mismatch proves the functions do not commute.
Order also changes the domain
The order can change which inputs are allowed, not only the output values. Let f(x) = √x and g(x) = x − 4.
- (f∘g)(x) = √(x − 4). The square root needs x − 4 ≥ 0, so the domain is x ≥ 4.
- (g∘f)(x) = √x − 4. The square root needs x ≥ 0, so the domain is x ≥ 0.
At x = 1, the second composite gives √1 − 4 = −3. The first composite is undefined at x = 1, because it would need √(−3), which is not a real number. So the two composites differ in both formula and domain.
Another pair shows a subtler effect. Take f(x) = √x and g(x) = x².
- (f∘g)(x) = √(x²) = |x|, defined for all real x.
- (g∘f)(x) = (√x)² = x, defined only for x ≥ 0.
Both simplify to something that looks like "x", but they are different functions. The first returns 5 for x = −5, and the second does not accept −5 at all. The domain rules for composites get a full treatment in a later chapter. For now, remember that the order decides which function checks the input first.
Common mistakes
- Reading ∘ left to right. Computing f first for f∘g reverses the composite. Say "f after g" and start from g.
- Confusing composition with multiplication. (f∘g)(x) is f(g(x)). It is not f(x)·g(x). With f(x) = 2x + 3 and g(x) = x², the product is (2x + 3)x², a different function again.
- Assuming commutativity. Swapping the order is not a harmless rewrite. Always say which composite you computed.
- Substituting only part of the inner expression. For f(x) = 2x + 3, writing f(g(x)) = 2x² + 3 is correct, but writing g(f(x)) = x² + 3 or (2x)² + 3 is wrong. The whole output 2x + 3 goes into g, giving (2x + 3)².
- Checking one input and declaring equality. Matching at one point (like x = −3 + √6) does not make two composites equal.
A reliable habit is to write the intermediate result explicitly: let u = g(x), then compute f(u). This keeps the order visible and makes substitution mistakes easy to spot.
Why this matters for inverses
To undo a composite, you must reverse its steps. If f∘g means "do g, then f", then undoing it means "undo f, then undo g". The order of the inverse steps is the reverse of the original order. A later chapter proves this as (f∘g)⁻¹ = g⁻¹∘f⁻¹. It only makes sense once you are certain which function runs first.
Practice
Exercise
Let f(x) = 3x − 2 and g(x) = x + 5. Find formulas for (f∘g)(x) and (g∘f)(x), and evaluate each at x = 2. Do the two composites agree?
Show answer
(f∘g)(x) = f(x + 5) = 3(x + 5) − 2 = 3x + 13, so (f∘g)(2) = 19. (g∘f)(x) = g(3x − 2) = (3x − 2) + 5 = 3x + 3, so (g∘f)(2) = 9. They do not agree. Using the linear-function test, the constants are a·d + b = 3·5 − 2 = 13 and c·b + d = 1·(−2) + 5 = 3, which differ.
Exercise
Let f(x) = √x and g(x) = x − 4. Give the domain of f∘g and of g∘f, and evaluate whichever of the two composites is defined at x = 1.
Show answer
(f∘g)(x) = √(x − 4) has domain x ≥ 4, so it is undefined at x = 1. (g∘f)(x) = √x − 4 has domain x ≥ 0, so at x = 1 it equals √1 − 4 = −3.