Work with functionsLesson 19 of 48 · 7 min

Compose functions in the right order

Use Lambda to represent a rule and distinguish two orders of composition.

Maths refresher: Function notation

f(x) means the output of the rule f at input x. Composition feeds an output into another rule.

f(x)=x+1,g(x)=x2,g(f(x))=(x+1)2f(x)=x+1,\quad g(x)=x^2,\quad g(f(x))=(x+1)^2
Worked example

At x=2x=2, g(f(2))=9g(f(2))=9 while f(g(2))=5f(g(2))=5. Function order changes the result.

Maths prerequisite map ↗

A function maps permitted inputs to outputs. Composing functions means applying one rule to the result of another. The order matters: f(g(x))f(g(x)) usually differs from g(f(x))g(f(x)).

See the mathematics
-2-0.750.51.753-13.5812.517xy
-2-0.750.51.753-13.5812.517xy
f(g(x))=x2+1,g(f(x))=(x+1)2f(g(x))=x^2+1,\quad g(f(x))=(x+1)^2
f(g(x))=x2+1g(f(x))=(x+1)2\begin{gathered}f(g(x))=x^2+1\\g(f(x))=(x+1)^2\end{gathered}
  • x2+1x^{2} + 1
  • (x+1)2\left(x + 1\right)^{2}
Starter reference. Green: square then add one. Amber: add one then square. The order changes the rule.

SymPy Lambda represents a symbolic rule with explicit arguments. Calling it with an expression substitutes that expression into its argument position. This makes composition inspectable without creating a numerical Python function yet.

The example compares adding one then squaring with squaring then adding one. Evaluate both at the same input and compare their formulas. Keep a function’s domain in mind: a formal composition may require an output of the inner function to lie inside the outer function’s permitted inputs.

Do “add one, then square” and “square, then add one” define the same rule?

Write a prediction (optional)

Exact symbolic investigation

Select Run Python with the code unchanged.

Tools
About Python

Python and the course packages can load while you read. Each run starts with fresh user variables; imported modules stay loaded. Stop replaces the worker. Runtime details ↗

Ctrl+Enter or ⌘+Enter runs Python.

Compare with the worked example

Executed from the published Python during the site build. Your own run appears above. Build environment ↗

f after g
x2+1x^{2} + 1
g after f
(x+1)2\left(x + 1\right)^{2}
f(g(2))
55
g(f(2))
99
Worked text output
f after g: x**2 + 1
g after f: (x + 1)**2
f(g(2)): 5
g(f(2)): 9

Read the starter result

Compare f after g with g after f. The inner call happens first. The entries f(g(2)) and g(f(2)) show the effect of changing order at a shared input.

Edit and compare

Replace f = sp.Lambda(x,x+1) with f = sp.Lambda(x,2*x). Select Run Python. Compare f(g(2)) with g(f(2)). Which argument is squared in each order?

Return to the editor

Scope. This example uses polynomial rules. Compositions involving roots or logarithms need explicit domain checks.

Try a fresh case

In h(k(x))h(k(x)), which rule receives x first?

Choose an answer
Need a hint?
Hint 1

Start at the innermost parentheses.

Hint 2

Track the output passed to the next rule.

Your notesNotes save in this browser when storage is available.