Skip to main content

Chaining and undoing: composition and inverse

Put two function machines in a row and the output of one becomes the input of the next: composition, where the order matters. Run a machine backwards to undo it: the inverse, which exists only when no two inputs share an output, and whose graph is the mirror image in the line y = x.
M01.3.4L0 Foundation25 minIn review

Before this lesson

M01.3.1 A function is a machine M01.3.3 Parabolas and the quadratic formula

After this lesson you can

  • Work out a composition such as g(f(x)) by applying f first, and show with numbers that the order usually matters.
  • Decide with the horizontal-line test whether a function has an inverse.
  • Find an inverse by solving y = f(x) for x, and check it by composing the two.
  • Explain why the graph of an inverse is the mirror image of the graph in the line y = x.
  • Limit the inputs of squaring to x ≥ 0 so that the square root undoes it, and say why the limit is needed.

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.

The question

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?

Show answer

\({(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}\)?

Show answer

One. The discriminant is \({36 - 36 = 0}\), and \({x^2 - 6x + 9} = {(x - 3)^2}\) is 0 only at \({x = 3}\).

Intuition

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 rows of function machines. Top row, titled first f, then g: the composition g after f: a blue disc 1 goes into a machine f with rule 2 x plus 3, out comes a dashed disc 5, which goes into a machine g with rule x squared, and out comes a vermillion disc 25, labelled g of f of 1 equals 25. Bottom row, titled first g, then f: the composition f after g: the blue disc 1 goes into g, out comes 1, which goes into f, and out comes a vermillion disc 5, labelled f of g of 1 equals 5. A sentence underneath reads: the same two machines in the other order give a different output.
M01.3.4-F1

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.

Build it up

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.

Two machines drawn as long boxes, each holding two smaller step boxes. Top, titled Forwards: double, then add 3: a blue disc 1 goes into machine f, through a step times 2, then the value 2, then a step plus 3, and out to a vermillion disc 5. Bottom, titled Backwards: undo the last step first: the vermillion disc 5 enters machine f inverse from the right, goes through a step minus 3, then the value 2, then a step divided by 2, and green arrows carry it out to the blue disc 1 on the left.
M01.3.4-F2

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}\).

The formal statement

NoteDefinition: composition

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.

NoteDefinition: one-to-one, inverse

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.

A coordinate plane with a dashed black mirror line labelled mirror: y equals x. A vermillion line, labelled y equals f of x, passes through the points (minus 1, 1), (0, 3) and (1, 5). A sky-blue line, labelled y equals f inverse of x, passes through their mirror images (1, minus 1), (3, 0) and (5, 1). Green dotted segments join each point to its mirror image, crossing the mirror line at right angles. Beside the picture: f of x equals 2 x plus 3, f inverse of x equals (x minus 3) over 2, swap the two numbers of each point: (1, 5) to (5, 1), (0, 3) to (3, 0), (minus 1, 1) to (1, minus 1); each pair is mirrored in the line y equals x.
M01.3.4-F3

The graph of \({f^{-1}}\) is the graph of \({f}\) reflected in the line \({y = x}\): each point \({(a, b)}\) becomes \({(b, a)}\).

Go deeper: undoing a chain: the last machine first

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} \]

Worked example

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.

Two rows of machines. Top row, titled There: first f, then g: a blue disc 1 goes into machine f with rule 2 x plus 3, out comes a dashed disc 5, into machine g with rule x squared, and out comes a vermillion disc 25, labelled g of f of 1. Bottom row, titled And back: undo g first, then f: green arrows run from right to left. The vermillion disc 25, labelled start, goes into machine g inverse with rule square root of x, out comes 5, into machine f inverse with rule (x minus 3) over 2, and out comes the blue disc 1. A sentence underneath reads: the square root undoes squaring here because 5 is not negative.
M01.3.4-F4

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}\).

Code

Two machines and the two ways to chain them. Each chain is a function that calls the others.

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

The inverse undoes the last step first. A loop checks that it returns every input it is given.

def f_inverse(y):
    # take 3 away, then halve
    return (y - 3) / 2

for x in [-3, -1, 0, 1, 2.5, 10]:
    print(x, f(x), f_inverse(f(x)))
-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.

import math

for x in [3, 0, -3]:
    print(x, x * x, math.sqrt(x * x))

# the round trip: 1 to 25 to 1
y = g(f(1))
back = f_inverse(math.sqrt(y))
print(y, back)
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.

Explore

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?

Pitfalls

Two panels. Left, marked with a cross and titled All of x squared: no inverse: the vermillion parabola y equals x squared and a dashed horizontal line y equals 4 meeting it at two reddish-purple points above minus 2 and 2, with the question 4 came from 2 or from minus 2? Right, marked with a tick and titled Only x at least 0: inverse square root of x: the right half of the parabola in vermillion, labelled y equals x squared, x at least 0, its mirror image the sky-blue curve y equals the square root of x, and the dashed mirror line y equals x. A green dotted segment joins the point (2, 4) on the parabola to its mirror image (4, 2) on the square-root curve.
M01.3.4-F5

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.

Check yourself

Recall

1. With \({f(x) = 2x + 3}\) and \({g(x) = x^2}\), find \({g(f(2))}\) and \({f(g(2))}\).

Show answer

\({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?

Show answer

\({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}\)?

Show answer

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}\)?

Show answer

\({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?

Show answer

Each is the mirror image of the other in the line \({y = x}\): the point \({(a, b)}\) becomes \({(b, a)}\).

Exercises

1. ★ Find the inverse of \({k(x) = 5x - 2}\), and check it by composing in both orders.

Show solution

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.

Show solution

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.

Show solution

\({(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}\).

Summary card

Chain machines with composition; undo one with its inverse, last step first.

The anchor figure again: 1 through f then g gives 25, while 1 through g then f gives 5.
M01.3.4-F1

Same machines, other order, other output.

  • Composition. \({(g \circ f)(x) = g(f(x))}\), with \({f}\) first; \({g(f(1)) = 25}\) but \({f(g(1)) = 5}\).
  • Inverse. \({f^{-1}(f(x)) = x}\); it exists only if no two inputs share an output.
  • Finding it. Solve \({y = f(x)}\) for \({x}\): \({f(x) = 2x + 3}\) gives \({f^{-1}(x) = \frac{x - 3}{2}}\).
  • Graph. Reflect in \({y = x}\). The square root undoes squaring only for \({x \geq 0}\).

Sources and next steps

Sources. Each was opened and checked against this lesson on 12 November 2026.

  • Jay Abramson and others, Algebra and Trigonometry 2e, OpenStax. Section 3.4: the notation \({(f \circ g)(x) = f(g(x))}\) with the inner function applied first, and the warning that \({f \circ g}\) and \({g \circ f}\) are in general different functions. Section 3.7: inverses of one-to-one functions, \({f^{-1}(f(x)) = x}\), \({f^{-1}}\) is not the reciprocal, the quadratic fails the horizontal-line test, the graph of \({f^{-1}}\) is the graph of \({f}\) reflected about \({y = x}\), and \({x^2}\) limited to \({[0, \infty)}\) has the square root as its inverse.
  • Tengyu Ma and Andrew Ng, CS229 Lecture Notes, Stanford University. Section 7.3, equation (7.39), writes a multi-layer network as a composition of matrix-multiplication and activation modules.
  • Python Software Foundation, 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

Your progress is saved in this browser only. No account, no tracking.
Back to top