Sub topic 2
Associativity: regrouping a chain of compositions
This chapter assumes you can evaluate f(x), compose two functions, and check whether a function is one-to-one. Composition means feeding the output of one function into another, written (f∘g)(x) = f(g(x)).
Associativity: regrouping a chain of compositions
Composition is associative. For three functions, ((h∘g)∘f)(x) equals (h∘(g∘f))(x) for every x in the domain. You can group a chain of compositions in any way, but you cannot change the order of the functions.
def f(x):
return x + 1
def g(x):
return 2 * x
def h(x):
return x - 3
def compose(outer, inner):
return lambda x: outer(inner(x))
left = compose(compose(h, g), f)
right = compose(h, compose(g, f))
for x in [-2, 0, 5]:
assert left(x) == right(x)
print(left(4), right(4)) # 7 7The identity function as the do-nothing function
The identity function is I(x) = x. Composing any function with it leaves the function unchanged: (f∘I)(x) = (I∘f)(x) = f(x).
One-to-one functions and the horizontal line test
A function is one-to-one when different inputs always give different outputs. Draw a horizontal line across the graph. If any horizontal line crosses the graph more than once, the function is not one-to-one. Only one-to-one functions have inverses. For example, f(x) = x² fails the test because f(3) and f(−3) both equal 9.
What an inverse function is
The inverse of f, written f⁻¹, reverses f. It satisfies f⁻¹(f(x)) = x and f(f⁻¹(x)) = x. Do not confuse f⁻¹ with 1/f. The superscript −1 means the inverse function, not a reciprocal.
Finding an inverse step by step
- Replace f(x) with y.
- Swap x and y.
- Solve for y.
- Replace y with f⁻¹(x).
For f(x) = 2x + 3, swapping gives x = 2y + 3, so y = (x − 3)/2. Therefore f⁻¹(x) = (x − 3)/2.
Exercise
If f(x) = 2x + 3, what is f⁻¹(11)?
Show answer
Using f⁻¹(x) = (x − 3)/2, we get f⁻¹(11) = (11 − 3)/2 = 4. Check: f(4) = 2(4) + 3 = 11.