Sub topic 6
Associativity: regrouping a chain of compositions
Assume you know function notation, domains and ranges, and how to evaluate a composite such as \((f\circ g)(x)\). This chapter focuses on how three or more functions can be grouped without changing their order.
What associativity means
Composition is associative. This means that regrouping a chain of compositions does not change the resulting function:
\[ (f\circ g)\circ h=f\circ(g\circ h) \]
Both sides apply the functions in the same order:
- Apply \(h\) to the input.
- Apply \(g\) to the result.
- Apply \(f\) to that result.
The parentheses only tell you which pair to calculate first. They do not change the order in which the functions receive values.
For an input \(x\),
\[ ((f\circ g)\circ h)(x) =(f\circ g)(h(x)) =f(g(h(x))) \]
On the other side,
\[ (f\circ(g\circ h))(x) =f((g\circ h)(x)) =f(g(h(x))) \]
Since both expressions produce the same output wherever all three steps are defined, the compositions are equal.
A numerical example
Let
\[ f(x)=2x+3,\qquad g(x)=x^2,\qquad h(x)=x-1. \]
Using the left grouping:
\[ ((f\circ g)\circ h)(4) =(f\circ g)(h(4)) =(f\circ g)(3) =f(g(3)) =f(9) =21. \]
Using the right grouping:
\[ (f\circ(g\circ h))(4) =f((g\circ h)(4)) =f(g(h(4))) =f(g(3)) =f(9) =21. \]
The intermediate expressions differ, but the final computation is the same.
def f(x):
return 2 * x + 3
def g(x):
return x ** 2
def h(x):
return x - 1
def compose(outer, inner):
return lambda x: outer(inner(x))
left = compose(compose(f, g), h)
right = compose(f, compose(g, h))
print(left(4)) # 21
print(right(4)) # 21Associativity is not commutativity
Associativity allows regrouping:
\[ (f\circ g)\circ h=f\circ(g\circ h). \]
It does not allow swapping functions. In general,
\[ f\circ g\ne g\circ f. \]
For example, with \(f(x)=2x+3\) and \(g(x)=x^2\),
\[ (f\circ g)(4)=35, \qquad (g\circ f)(4)=121. \]
The order remains \(h\), then \(g\), then \(f\). Only the parentheses change.
Domain conditions
Associativity assumes that both sides are defined for the input being considered. A function in the middle must accept the output from the function before it.
For example, if \(h(x)\) produces a negative value but \(g\) is defined only for nonnegative inputs, then the three-function composite is undefined for that input. Regrouping does not repair an invalid intermediate value.
Exercise
If (f ∘ g)(x) = f(g(x)), what does ((f ∘ g) ∘ h)(x) simplify to?
Show answer
It simplifies to f(g(h(x))), which is also equal to (f ∘ (g ∘ h))(x) wherever all three functions are defined.