def f(x):
return 2 * x + 3
def g(x):
return x * x
# first f, then g
def g_after_f(x):
return g(f(x))
# first g, then f
def f_after_g(x):
return f(g(x))
print(g_after_f(1), f_after_g(1))25 5
Before this lesson
M01.3.1 A function is a machine M01.3.3 Parabolas and the quadratic formula
After this lesson you can
In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.
A neural network is built from simple machines in a row: the output of each layer is the input of the next. Does the order of the machines matter? And if a machine turns 1 into 5, can it always be run backwards, to turn 5 back into 1?
From the last lesson
1. What is the vertex of \({y = (x - 3)^2 + 2}\), and is it the lowest or the highest point?
\({(3, 2)}\), the lowest point: the square is never negative and is 0 only at \({x = 3}\).
2. How many real zeros has \({x^2 - 6x + 9}\)?
One. The discriminant is \({36 - 36 = 0}\), and \({x^2 - 6x + 9} = {(x - 3)^2}\) is 0 only at \({x = 3}\).
Take the two machines of this module: \({f}\) doubles its input and adds 3, and \({g}\) squares it. Put 1 into \({f}\) and out comes 5. Feed that 5 into \({g}\) and out comes 25. Now use the same two machines the other way round. Put 1 into \({g}\) and out comes 1; feed that into \({f}\) and out comes 5.
Two machines in a row. The output of the first is the input of the second. The same two machines in the other order give a different output.
Running one machine and then another is called composition. Machine learning is full of it. A neural network is a chain of layers, and the network’s output is the last layer applied to the output of the one before, and so on back to the input. Order matters there too.
The second question runs a machine backwards. \({f}\) doubles, then adds 3. To get back from 5 to 1, take 3 away and then halve: \({5 - 3 = 2}\) and \({2 \div 2 = 1}\). That backwards machine is the inverse of \({f}\). Not every machine has one: squaring turns both \({-2}\) and \({2}\) into 4, and from 4 alone there is no telling which it was.
Step 1. Name the chain. Running \({f}\) first and then \({g}\) gives the output \({g(f(x))}\). The machine that does both is written \({g \circ f}\) and read “\({g}\) after \({f}\)”. Read the brackets from the inside out: \({f}\), the one nearest to \({x}\), acts first.
Step 2. Find its formula. Replace the input of \({g}\) by the whole of \({f(x)}\), then expand with the square of a sum from lesson M01.1.2:
\[ \begin{aligned} g(f(x)) &= (2x + 3)^2 \\ &= 4x^2 + 12x + 9 . \end{aligned} \]
The other order puts \({g(x)}\) into \({f}\): \({f(g(x)) = 2x^2 + 3}\). At \({x = 1}\) the first gives 25 and the second 5. They are different functions.
Step 3. Run the machine backwards. \({f}\) does two things: double, then add 3. To undo them, undo the last one first. Take 3 away, then halve.
The inverse undoes the steps in the opposite order, like taking off shoes before socks.
So \({f^{-1}(y) = \frac{y - 3}{2}}\), written with a raised \({-1}\) and read “f inverse”. Check it on any input:
\[ \begin{aligned} f^{-1}(f(x)) &= \frac{(2x + 3) - 3}{2} \\ &= \frac{2x}{2} = x . \end{aligned} \]
The backwards machine returns every input it is given.
Step 4. When backwards fails. A machine can be run backwards only if each output came from just one input. Squaring breaks this: \({g(-2) = g(2) = 4}\). On the graph, the horizontal line \({y = 4}\) meets the parabola twice.
Step 5. Limit the inputs. Allow only inputs \({x \geq 0}\). Now each output comes from one input, and the square root undoes the square: \({\sqrt{x^2} = x}\) for every \({x \geq 0}\).
If the outputs of \({f}\) are allowed inputs of \({g}\), the composition \({g \circ f}\) is the function
\[ (g \circ f)(x) = g(f(x)) . \]
\({f}\) is applied first. In general \({g \circ f}\) and \({f \circ g}\) are different functions.
A function is one-to-one if no two different inputs give the same output. A one-to-one function \({f}\) has an inverse \({f^{-1}}\), which sends each output \({y}\) back to the one input \({x}\) with \({f(x) = y}\). Its domain is the range of \({f}\), and
\[ \begin{aligned} f^{-1}(f(x)) &= x \ \text{ for every input } x , \\ f(f^{-1}(y)) &= y \ \text{ for every output } y . \end{aligned} \]
Why one-to-one is needed. Suppose two different inputs \({a}\) and \({b}\) had the same output \({y}\). Then \({f^{-1}(y)}\) would have to be \({a}\) and also \({b}\): two outputs for one input, which no function allows.
The horizontal-line test. A function is one-to-one exactly when no horizontal line meets its graph more than once. It is the vertical-line test of lesson M01.3.1 with across and up swapped: the line \({y = c}\) meets the graph at every input whose output is \({c}\).
Finding an inverse. Write \({y = f(x)}\) and solve for \({x}\). For \({f}\): from \({y = 2x + 3}\), take 3 from both sides, \({y - 3 = 2x}\), then halve, \({x = \frac{y - 3}{2}}\). So \({f^{-1}(y) = \frac{y - 3}{2}}\), or, with the input called \({x}\) as usual, \({f^{-1}(x) = \frac{x - 3}{2}}\).
The graph of an inverse is a mirror image. A point \({(a, b)}\) is on the graph of \({f}\) when \({f(a) = b}\). That is the same as \({f^{-1}(b) = a}\), which says \({(b, a)}\) is on the graph of \({f^{-1}}\). So the inverse’s graph is the graph of \({f}\) with the two numbers of every point swapped.
Swapping the numbers is reflecting in the line \({y = x}\). The midpoint of \({(a, b)}\) and \({(b, a)}\) is \({\big(\frac{a+b}{2}, \frac{a+b}{2}\big)}\), which lies on the line. And every point \({(t, t)}\) of the line is equally far from the two, because by the distance formula of lesson M01.2.3 the squared distances are
\[ \begin{aligned} &(t - a)^2 + (t - b)^2 \ \text{ and} \\ &(t - b)^2 + (t - a)^2 , \end{aligned} \]
the same two terms in the other order. Two different points are mirror images in a line exactly when every point of the line is equally far from both, so \({(b, a)}\) is the mirror image of \({(a, b)}\). When \({a = b}\) the point lies on the mirror and is its own image.
The graph of \({f^{-1}}\) is the graph of \({f}\) reflected in the line \({y = x}\): each point \({(a, b)}\) becomes \({(b, a)}\).
If \({f}\) and \({g}\) both have inverses, the inverse of \({g \circ f}\) is \({f^{-1} \circ g^{-1}}\): undo \({g}\) first, because \({g}\) acted last. Check that it returns every input:
\[ \begin{aligned} &f^{-1}\big(g^{-1}(g(f(x)))\big) \\ &\quad= f^{-1}(f(x)) = x . \end{aligned} \]
The inner \({g^{-1}}\) cancels the \({g}\), and then \({f^{-1}}\) cancels the \({f}\). The other way round uses the same two cancellations:
\[ \begin{aligned} &g\big(f(f^{-1}(g^{-1}(y)))\big) \\ &\quad= g(g^{-1}(y)) = y . \end{aligned} \]
Send 1 through \({f}\) and then \({g}\), and bring the answer back.
There. \({f(1) = 2 \times 1 + 3 = 5}\), then \({g(5) = 5^2 = 25}\). So \({g(f(1)) = 25}\).
And back. Undo \({g}\) first, because it acted last. Squaring has no inverse on all numbers, but the number that went into \({g}\) was \({f(1) = 5}\), which is not negative, so the square root undoes it: \({\sqrt{25} = 5}\). Then undo \({f}\): \({f^{-1}(5) = \frac{5 - 3}{2} = 1}\). We are back at the start.
The round trip. Going back, the machines are undone in the opposite order.
The whole chain backwards, as a formula. The chain is \({y = (2x + 3)^2}\). For inputs with \({2x + 3 \geq 0}\), that is \({x \geq -1.5}\), take the square root of both sides, then solve the line:
\[ \begin{aligned} \sqrt{y} &= 2x + 3 \\ x &= \frac{\sqrt{y} - 3}{2} . \end{aligned} \]
At \({y = 25}\) this gives \({\frac{5 - 3}{2} = 1}\), as before.
Why the limit matters. Try the input \({-4}\): \({f(-4) = -5}\) and \({g(-5) = 25}\). The chain sends both 1 and \({-4}\) to 25, so on all inputs it has no inverse. Run 25 back and you reach 1, never \({-4}\), because the square root gives \({5}\) and not \({-5}\).
Two machines and the two ways to chain them. Each chain is a function that calls the others.
25 5
The inverse undoes the last step first. A loop checks that it returns every input it is given.
-3 -3 -3.0
-1 1 -1.0
0 3 0.0
1 5 1.0
2.5 8.0 2.5
10 23 10.0
math.sqrt undoes a square, but only for inputs that were not negative.
3 9 3.0
0 0 0.0
-3 9 3.0
25 1.0
The last row of the loop is the pitfall in one line: \({-3}\) goes in, 9 comes out, and the square root returns 3, not \({-3}\). The CS229 lecture notes write a whole neural network the same way as g_after_f, one layer inside the next, and in module M01.4 the logarithm is defined as the inverse of a power.
The function machine of this module is in lesson M01.3.1. Press Chain a second machine, choose \({2x + 3}\) for \({f}\) and \({x^2}\) for \({g}\), and set the input to 1: the read-out shows \({g(f(1)) = 25}\). Press Swap the order and it shows \({f(g(1)) = 5}\). Then try \({\abs{x}}\) with \({x^2}\) in both orders. The two curves agree at every input: can you say why?
Wrong beside right. All of \({x^2}\) fails the horizontal-line test. Its right half passes, and its mirror image is the square root.
“The square root undoes squaring.”
Only for inputs that are 0 or more. \({\sqrt{(-3)^2} = \sqrt{9} = 3}\), not \({-3}\). Squaring sends \({3}\) and \({-3}\) to the same 9, so no machine can tell them apart afterwards. Limit the inputs to \({x \geq 0}\) first, and then the square root is the inverse.
“\({f^{-1}(x)}\) means \({\frac{1}{f(x)}}\).”
The raised \({-1}\) means “run backwards”, not “one over”. For \({f(x) = 2x + 3}\), \({f^{-1}(5) = 1}\), while \({\frac{1}{f(5)} = \frac{1}{13}}\).
“\({g \circ f}\) means \({g}\) first.”
It means \({f}\) first, as \({g(f(x))}\) shows: the inner bracket is worked out first. Get the order wrong and \({25}\) becomes \({5}\). Swapping two machines usually changes the result, and so does swapping two layers of a network.
1. With \({f(x) = 2x + 3}\) and \({g(x) = x^2}\), find \({g(f(2))}\) and \({f(g(2))}\).
\({f(2) = 7}\) and \({g(7) = 49}\), so \({g(f(2)) = 49}\). \({g(2) = 4}\) and \({f(4) = 11}\), so \({f(g(2)) = 11}\).
2. In \({g \circ f}\), which machine runs first?
\({f}\). The composition is \({g(f(x))}\), worked from the inside out.
3. What does the horizontal-line test check, and what does it say about \({x^2}\)?
Whether a function is one-to-one, so has an inverse. \({x^2}\) fails: \({y = 4}\) meets it twice.
4. What is the inverse of \({h(x) = x - 4}\)?
\({h^{-1}(x) = x + 4}\): add back the 4 that \({h}\) took away.
5. How are the graphs of a function and its inverse related?
Each is the mirror image of the other in the line \({y = x}\): the point \({(a, b)}\) becomes \({(b, a)}\).
1. ★ Find the inverse of \({k(x) = 5x - 2}\), and check it by composing in both orders.
From \({y = 5x - 2}\): add 2 to both sides, \({y + 2 = 5x}\), then divide by 5, \({x = \frac{y + 2}{5}}\). So \({k^{-1}(x) = \frac{x + 2}{5}}\). Check: \({k^{-1}(k(x))} = {\frac{(5x - 2) + 2}{5}} = {x}\), and \({k(k^{-1}(x))} = {5 \times \frac{x + 2}{5} - 2} = {x}\).
2. ★★ For \({f(x) = 2x + 3}\) and \({g(x) = x^2}\), find every input \({x}\) at which \({g(f(x))}\) and \({f(g(x))}\) agree.
Set the two formulas equal, take \({2x^2 + 3}\) from both sides, then halve:
\[ \begin{aligned} 4x^2 + 12x + 9 &= 2x^2 + 3 \\ 2x^2 + 12x + 6 &= 0 \\ x^2 + 6x + 3 &= 0 . \end{aligned} \]
By the quadratic formula of lesson M01.3.3, with discriminant \({36 - 12 = 24}\) and \({\sqrt{24} = 2\sqrt{6}}\),
\[ x = \frac{-6 \pm \sqrt{24}}{2} = -3 \pm \sqrt{6} , \]
that is \({x \approx -0.5505}\) and \({x \approx -5.4495}\). At every other input the two orders differ.
3. ★★★ Let \({h(x) = x - 4}\). Find the inverse of \({h \circ f}\), where \({f(x) = 2x + 3}\), in two ways: by solving, and as \({f^{-1} \circ h^{-1}}\). Then show that the other order, \({h^{-1} \circ f^{-1}}\), is wrong.
\({(h \circ f)(x)} = {(2x + 3) - 4} = {2x - 1}\). Solving \({y = 2x - 1}\) gives \({x = \frac{y + 1}{2}}\), so the inverse is \({\frac{x + 1}{2}}\). As a chain of inverses:
\[ \begin{aligned} f^{-1}(h^{-1}(x)) &= f^{-1}(x + 4) \\ &= \frac{(x + 4) - 3}{2} \\ &= \frac{x + 1}{2} . \end{aligned} \]
The same. The other order gives \({h^{-1}(f^{-1}(x))} = {\frac{x - 3}{2} + 4} = {\frac{x + 5}{2}}\). Test it: \({(h \circ f)(1) = 1}\), and the true inverse sends 1 back to \({\frac{1 + 1}{2} = 1}\), but the wrong one gives \({\frac{1 + 5}{2} = 3}\).
Chain machines with composition; undo one with its inverse, last step first.
Same machines, other order, other output.
Sources. Each was opened and checked against this lesson on 12 November 2026.
math.sqrt, Python 3.12 documentation: returns the square root of its argument.Next. Module M01.4, Exponentials and logarithms, starts from powers. Its lesson M01.4.3 meets the inverse again: the logarithm is the machine that undoes a power.
This lesson unlocks
M01.4 Exponentials and logarithms ML01.1.3 A model is a function with knobs