Sub topic 2
Composing Functions: Feeding One Output into Another
Assume you can use function notation such as \(f(x)\), identify a function’s domain and range, and simplify algebraic expressions. This chapter develops a new function by connecting two existing functions.
The idea of composition
A composite function uses the output of one function as the input of another. The notation
\[ (f\circ g)(x) \]
is read “\(f\) composed with \(g\).” It means
\[ (f\circ g)(x)=f(g(x)). \]
The function on the right, \(g\), acts first. Its output then becomes the input to \(f\).
For example, let
\[ f(x)=2x+3 \quad\text{and}\quad g(x)=x^2. \]
To find \((f\circ g)(x)\), substitute \(g(x)\) wherever \(f\) has \(x\):
\[ (f\circ g)(x)=f(g(x))=f(x^2)=2x^2+3. \]
The order is part of the meaning. You do not multiply \(f\) and \(g\).
Evaluating a composite
To evaluate \((f\circ g)(4)\), work from the inside out:
- Find \(g(4)\):
\[ g(4)=4^2=16. \]
- Use that output as the input to \(f\):
\[ f(16)=2(16)+3=35. \]
Therefore,
\[ (f\circ g)(4)=35. \]
A function diagram shows the same process:
The expression \(f(g(x))\) is often called the composite expression. The inner function is \(g\), and the outer function is \(f\).
Composition is usually not commutative
Addition satisfies \(a+b=b+a\), but function composition usually does not. Reversing the order changes which function acts first.
Using the same functions:
\[ (g\circ f)(x)=g(f(x))=g(2x+3)=(2x+3)^2. \]
At \(x=4\),
\[ (f\circ g)(4)=35, \qquad (g\circ f)(4)=121. \]
| Composition | First output | Final value at \(x=4\) |
|---|---|---|
| \(f\circ g\) | \(g(4)=16\) | \(f(16)=35\) |
| \(g\circ f\) | \(f(4)=11\) | \(g(11)=121\) |
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)
g_after_f = compose(g, f)
print(f_after_g(4)) # 35
print(g_after_f(4)) # 121The helper function compose takes the outer function first and the inner function second. Its returned function applies inner before outer.
A reliable procedure
For \((f\circ g)(x)\):
- Write \(f(g(x))\).
- Substitute the complete expression \(g(x)\) into \(f\).
- Use parentheses around the substitution.
- Simplify only after the substitution is correct.
Forgetting the parentheses can change the result. If \(f(x)=x^2\) and \(g(x)=x+1\), then
\[ f(g(x))=(x+1)^2, \]
not \(x^2+1\).
Exercise
Let f(x)=3x-2 and g(x)=x^2+1. Find (f∘g)(2) and (g∘f)(2).
Show answer
(f∘g)(2)=f(5)=13, while (g∘f)(2)=g(4)=17.