Sub topic 2
Composing Functions: Feeding One Output into Another
This chapter assumes you are comfortable with function notation such as f(x), with domain and range, with solving simple equations, and with reading graphs of lines, parabolas and square roots. Here you will learn what a composite function is, how to evaluate one at a number, and how to build one as a formula. You will also see the mistakes that most often go wrong.
The idea: a pipeline of two functions
A function takes an input and returns one output. Composition connects two functions in a chain. The output of the first function becomes the input of the second.
Suppose g doubles a number and f adds 3. Start with 5. Applying g gives 10. Feeding 10 into f gives 13. The pair "double, then add 3" is itself a function, and it maps 5 to 13. That combined function is a composite function.
Notation and reading direction
The composite of f and g is written two ways:
- (f∘g)(x), read "f composed with g of x" or "f of g of x".
- f(g(x)), the same thing with the nesting written out.
The definition is:
(f∘g)(x) = f(g(x))
The function nearest to x, here g, is the inner function. It runs first. The function on the outside, here f, is the outer function. It runs second. This is the opposite of left-to-right reading, so it takes practice. A useful habit is to say "f of g of x" aloud and work from the innermost parentheses outward.
The symbol ∘ is not multiplication. (f∘g)(x) does not mean f(x) · g(x). It also does not mean f(x) + g(x).
Evaluating a composite at a number
To compute (f∘g)(a), take two steps:
- Compute g(a). Call the result b.
- Compute f(b).
Let f(x) = 2x + 3 and g(x) = x². Find (f∘g)(4).
- g(4) = 4² = 16.
- f(16) = 2(16) + 3 = 35.
So (f∘g)(4) = 35. Writing the intermediate value 16 explicitly is worth the extra line. Most evaluation mistakes come from skipping it.
Building the composite as a formula
Often you want a formula for f(g(x)) rather than a single value. Take the outer function's rule and replace every x in it with the whole inner expression.
With f(x) = 2x + 3 and g(x) = x²:
- f says "double the input and add 3".
- The input is now x², so (f∘g)(x) = 2(x²) + 3 = 2x² + 3.
Check the formula against the value above. At x = 4, 2(16) + 3 = 35, which matches.
Here is a second example where the inner function has more than one term. Let f(x) = x² and g(x) = x − 1. The outer rule is "square the input". The input is x − 1, so:
(f∘g)(x) = (x − 1)² = x² − 2x + 1
The parentheses are essential. Writing x − 1² or x² − 1 describes a different function.
A third example uses a square root as the outer function. Let h(x) = √x and k(x) = x − 5. Then (h∘k)(x) = √(x − 5). Substituting the inner expression is the same process whatever the outer function looks like.
| Outer f(x) | Inner g(x) | Composite f(g(x)) |
|---|---|---|
| 2x + 3 | x² | 2x² + 3 |
| x² | x − 1 | (x − 1)² |
| √x | x − 5 | √(x − 5) |
| 1/x | x + 2 | 1/(x + 2) |
Composition in code
A program expresses the same idea directly. This function takes two functions and returns their composite:
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
print(g(4)) # 16, the inner step
print(f(g(4))) # 35, the outer step applied by hand
print(f_after_g(4)) # 35, the same result from the compositeThe argument names follow the math: outer runs last and inner runs first. The first argument of compose is the function written on the left of ∘.
Composing from tables and graphs
You do not need formulas to compose. If functions are given as tables, look up the inner output, then use it as the input to the outer table. Here g is given as a table, and f(x) = 2x + 3:
| x | g(x) | f(g(x)) |
|---|---|---|
| 1 | 3 | 9 |
| 2 | 0 | 3 |
| 3 | −1 | 1 |
The same method works on graphs. To find f(g(2)) from graphs, find 2 on the horizontal axis of g and read up to the curve. That height is g(2). Then locate that value on the horizontal axis of f and read up again.
Lookup tables show an important restriction. This code composes two tables stored as dictionaries:
g_table = {1: 3, 2: 0, 3: -1}
f_table = {3: 9, 0: 3, -1: 1}
def lookup_compose(outer, inner, x):
return outer[inner[x]]
for x in [1, 2, 3]:
print(x, lookup_compose(f_table, g_table, x))
# Output:
# 1 9
# 2 3
# 3 1If g_table held a value such as 7 that is not a key in f_table, the lookup would raise a KeyError. A composite is only defined where the inner output is an acceptable input for the outer function.
A first look at when a composite fails
The same restriction applies to formulas. Let h(x) = √x and k(x) = x − 5, so (h∘k)(x) = √(x − 5).
- At x = 9: k(9) = 4, and h(4) = 2. This works.
- At x = 1: k(1) = −4, and √(−4) is not a real number. The composite is undefined at 1.
The rule is that x must be in the domain of the inner function, and the inner output g(x) must be in the domain of the outer function. A later chapter works through this in detail. For now, remember that evaluating step by step will expose the problem.
Common errors
- Reversing the roles. Writing f(g(x)) when asked for g(f(x)), or substituting f into g because f is written first. Identify the inner function as the one next to x.
- Treating ∘ as multiplication. (f∘g)(4) is 35 in the example above, but f(4) · g(4) = 11 · 16 = 176.
- Partial substitution. With f(x) = 2x + 3 and g(x) = x², writing f(g(x)) = 2x + 3² replaces only one occurrence of x in the wrong place. Replace every x in the outer rule, and keep the inner expression in parentheses: 2(x²) + 3.
- Dropping parentheses. If f(x) = x² and g(x) = x − 1, then f(g(x)) = (x − 1)², not x² − 1.
- Skipping the middle value. Compute g(a) first and write it down. Then compute f of that number.
- Assuming the order does not matter. Here f(g(4)) = 35. Reversing the order gives g(f(4)) = g(11) = 121, a different answer. The next chapter explains why.
Summary
- A composite function feeds the output of an inner function into an outer function: (f∘g)(x) = f(g(x)).
- To evaluate at a number, compute the inner value first, then apply the outer function.
- To build a formula, replace every x in the outer rule with the full inner expression, in parentheses.
- Tables, graphs and code all follow the same two-step process.
- The inner output must be a valid input of the outer function, or the composite is undefined at that point.
Exercise
Let f(x) = 3x − 1 and g(x) = x² + 2. Find a formula for (f∘g)(x) and evaluate (f∘g)(2). Verify your answer by computing the two steps separately.
Show answer
The outer rule is "triple the input and subtract 1". Replace x with x² + 2: (f∘g)(x) = 3(x² + 2) − 1 = 3x² + 6 − 1 = 3x² + 5. At x = 2: 3(4) + 5 = 17. Check: g(2) = 4 + 2 = 6, and f(6) = 18 − 1 = 17. Both methods agree.
Exercise
Let f(x) = √x and g(x) = x − 5. Evaluate (f∘g)(9). Then explain why (f∘g)(1) is not a real number.
Show answer
g(9) = 9 − 5 = 4, and f(4) = √4 = 2, so (f∘g)(9) = 2. For x = 1, g(1) = 1 − 5 = −4. The outer function √x needs a non-negative input, and −4 is outside its domain, so the composite is undefined at x = 1.