An inverse function runs a function backward: give it an output, and it returns the input. Peeling steps in reverse finds simple ones. This lesson uses a method that also works where peeling can't: write y = f(x), make x the subject, then rename. It covers x in two places, the one-to-one rule every inverse needs, and every form of a straight line. In AI and ML, the same moves unscale a model's predictions and turn a classifier's weights into a line you can draw.
What you'll be able to do
Find an inverse by writing y = f(x), making x the subject and renaming, even when x appears twice
Say when an inverse exists (f must be one-to-one), restrict a domain to make one, and swap the domain and range
Check an inverse both ways, f(f⁻¹(x)) = x and f⁻¹(f(x)) = x, by algebra and in Python
Rearrange every form of a line: general, point-slope, the slope formula and the intercept from a point
Unscale min-max and standardized predictions, and turn a classifier's weights into the boundary line you'd draw
Quick check
Have a guess before you start. f(x) = (2x + 3)/5 sends 6 to 3. Which input does it send to 7?
3.4
2.5
16
32
Show the answer
16
Undo f's steps in reverse: times 5 (35), take 3 away (32), halve (16). Check: (2 × 16 + 3)/5 = 35/5 = 7. That backward rule is the inverse, f⁻¹(x) = (5x − 3)/2.
The math: make x the subject of y = f(x)
Purpose. Find a function's inverse, and know when it has one.
lesson 20 wrote f⁻¹, read "f inverse", for the function that undoes f, so f⁻¹(f(x)) = x. The −1 means "undo", not "one over". That lesson peeled steps in reverse. Transposing y = f(x) for x also works where peeling can't:
Write y = f(x).
Make x the subject, with any move from this subcourse.
Rename. The result takes y in and gives x out, so swap the letters and write f⁻¹(x).
State the conditions, and check that f(f⁻¹(x)) = x.
Renaming changes no numbers: an input's letter is only a name. With x as its input, f⁻¹ can share a graph with f. Each point (a, b) of f becomes (b, a), so f⁻¹ is f's mirror image in the line y = x.
Quick check
From an earlier lesson: h(x) = x/4 − 2. What is h⁻¹(1)?
4
6
12
−1.75
Show the answer
12
h divides by 4, then takes 2 away. Undo in reverse: add 2 (3), then multiply by 4 (12). Check: 12/4 − 2 = 1.
Watch f(x) = (2x + 3)/5 build y from x = 6, then peel it back. The bottom row is step 2: x made the subject, one opposite at a time.
Try it now
Change the input and press Use these numbers: whatever goes in, the bottom row brings it back.
Symbols and formula. The four steps for f(x) = (2x + 3)/5:
y=52x+3⇒5y=2x+3⇒x=25y−3⇒f−1(x)=25x−3
In plain words: call the output y, free x, then rename the letters so the inverse takes x in, like any function.
Check it both ways. f⁻¹ undoes f, and f undoes f⁻¹. Put f⁻¹(x) into f: (2 × (5x − 3)/2 + 3)/5 = (5x − 3 + 3)/5, which is x. The other way, (5 × (2x + 3)/5 − 3)/2 = (2x + 3 − 3)/2, also x. Everything cancels, so it holds for every x, not just one lucky number.
Named mistakes. Writing f⁻¹(x) = 1/f(x), which is 5/(2x + 3), a different function. And renaming only some letters, as in f⁻¹(x) = (5y − 3)/2: a finished inverse has one letter, its input.
Quick check
f(x) = 4 − 3x. What is f⁻¹(x)?
f⁻¹(x) = 4 + 3x
f⁻¹(x) = (x − 4)/3
f⁻¹(x) = (4 − x)/3
f⁻¹(x) = 1/(4 − 3x)
Show the answer
f⁻¹(x) = (4 − x)/3
y = 4 − 3x, so 3x = 4 − y and x = (4 − y)/3. Rename: f⁻¹(x) = (4 − x)/3. Check: f(2) = −2, and (4 − (−2))/3 = 2.
When x appears twice. In f(x) = x/(x + 1), x is on top and in the bottom, so no single opposite frees it. Use the gather-and-factor move from lesson 32:
y=x+1x⇒xy+y=x⇒y=x−xy=x(1−y)⇒f−1(x)=1−xx
The condition x ≠ 1 tells you something about f. Its top is always 1 less than its bottom, so f never gives 1. So f takes any input but −1 and gives any output but 1, while f⁻¹ takes any input but 1 and gives any output but −1. The domain and range swap: f⁻¹ accepts exactly what f can give.
A second route: the top is (x + 1) − 1, so f(x) = 1 − 1/(x + 1), with x in one place. Then peeling works again.
One-to-one, or no inverse. An inverse sends each output back to one input, so f must be one-to-one: no two inputs share an output. On a graph, no horizontal line crosses it twice. x² fails, since 3 and −3 both give 9. Restrict the domain and it passes: on x ≥ 0 only, f⁻¹(x) = √x.
The restriction also picks the sign of the ± root from lesson 33. For f(x) = (x − 3)² on x ≥ 3, x − 3 is never negative, so y = (x − 3)² gives x − 3 = +√y, and f⁻¹(x) = 3 + √x. On x ≤ 3 instead, it's 3 − √x.
A line y = mx + b is one-to-one when m ≠ 0, with f⁻¹(x) = (x − b)/m. A flat line sends every input to b, so it has no inverse.
Quick check
f(x) = (x + 2)² is restricted to x ≥ −2. What is f⁻¹(x)?
−2 − √x
√x − 2
±√x − 2
√x + 2
Show the answer
√x − 2
On x ≥ −2, x + 2 is never negative, so y = (x + 2)² gives x + 2 = √y and x = √y − 2. Check: f(1) = 9, and √9 − 2 = 1.
Every form of a line turns around.lesson 21 wrote a line as y = mx + b, with slope m and intercept b. Its other forms rearrange with the same moves, and each condition is a divisor that can't be 0.
Form
Made the subject
Condition
ax + by + c = 0 (general)
y = −(ax + c)/b
b ≠ 0
ax + by + c = 0 (general)
x = −(by + c)/a
a ≠ 0
y − y₁ = m(x − x₁) (point-slope)
x = x₁ + (y − y₁)/m
m ≠ 0
m = (y₂ − y₁)/(x₂ − x₁) (slope)
y₂ = y₁ + m(x₂ − x₁)
none
m = (y₂ − y₁)/(x₂ − x₁) (slope)
x₂ = x₁ + (y₂ − y₁)/m
m ≠ 0
y = mx + b through (x₁, y₁)
b = y₁ − mx₁
none
The point-slope form names a line by its slope m and one point (x₁, y₁) on it. In the general form, b is just the number in front of y, not the intercept. If b = 0, the line has no y in it at all: it's the vertical line x = −c/a, with no y = … form.
In your own words
Why are the domain and range of f⁻¹ the range and domain of f?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: both round trips, conditions first
Purpose. Write f and f⁻¹ as functions, with each condition as the first line, and test the round trip both ways.
deff(x):
if x == -1:
returnNonereturn x / (x + 1)
deff_inv(y):
if y == 1:
returnNonereturn y / (1 - y)
for x in [3, 0.5, -3, 9]:
print(x, round(f_inv(f(x)), 9), round(f(f_inv(x)), 9))
print(f(-1), f_inv(1))
It runs right here in your browser, with nothing to install.
Every row comes back both ways. Rounding to 9 places hides the tiny errors decimals carry. The two orders test different numbers: f_inv(f(x)) only meets values that f produced, while f(f_inv(x)) feeds f_inv values you chose. The last line shows each condition at work.
Worked example
Worked example: writing the restriction into code
x² has an inverse only on x ≥ 0. Here f refuses negative inputs, and f_inv refuses what f can never give.
import math
deff(x):
if x < 0:
returnNonereturn x ** 2deff_inv(y):
if y < 0:
returnNonereturn math.sqrt(y)
print(math.sqrt((-4) ** 2), f_inv(f(3)), f(f_inv(9)))
print(f(-4), f_inv(-9))
In the lesson you can change this and run it.
Without the restriction, squaring −4 and taking the root gives 4.0, not −4: that round trip fails. With f limited to x ≥ 0, both orders work: 3 comes back as 3.0 and 9 as 9.0. The last line shows both conditions. −4 is outside f's domain, and −9 is outside the domain of f_inv, which is the range of f.
Quick check
What does this print?
defy_on_line(a, b, c, x):
if b == 0:
returnNonereturn -(a * x + c) / b
print(y_on_line(3, 4, -12, 0), y_on_line(3, 4, -12, 8), y_on_line(1, 0, -5, 2))
3.0 -3.0 None
3.0 -3.0 5.0
-3.0 3.0 None
3.0 3.0 None
Show the answer
3.0 -3.0 None
At x = 0: −(−12) ÷ 4 = 3.0, the intercept. At x = 8: −(24 − 12) ÷ 4 = −3.0. The last line is x − 5 = 0, with b = 0: no y in it, so no y = … form, and the function returns None.
Quick check
A line through (x1, y1) has slope m. Which line gives its height y2 at x2?
y2 = m * (x2 - x1)
y2 = y1 - m * x1
y2 = y1 + (x2 - x1) / m
y2 = y1 + m * (x2 - x1)
Show the answer
y2 = y1 + m * (x2 - x1)
m = (y₂ − y₁)/(x₂ − x₁). Multiply both sides by (x₂ − x₁), then add y₁. Nothing is divided by m, so a flat line is fine: it gives y₂ = y₁.
Drill
Fix the line formulas
x_at(y, x1, y1, m) should undo the point-slope form y − y₁ = m(x − x₁), returning None for a flat line. intercept(m, x1, y1) should give b in y = mx + b for a line through (x₁, y₁). Each has a slip. Fix both.
In the lesson, your code is checked as you write it.
In your own words
Why does f_inv(f(-4)) give 4.0 when f squares and f_inv takes the square root, and how does a restriction fix it?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: unscaling, and the line a classifier draws
Purpose. Models train and predict on scaled numbers, and classifiers split data along a line. Reading either one in real units is a rearrangement.
Min-max scaling squeezes a column into 0 to 1: x′ = (x − min)/(max − min), with min and max from the training data, and max ≠ min. Make x the subject to unscale: x = min + x′(max − min). Standardizing, the scaling met in lesson 30, uses the mean μ (mu) and the standard deviation σ (sigma, a typical spread, defined fully in lesson 97): z = (x − μ)/σ, so x = μ + zσ, with σ ≠ 0. Libraries such as scikit-learn call this undo step inverse_transform.
The tent shop's daily rentals ran from 12 to 60. max gives a list's biggest value, as in lesson 19, and its partner min gives the smallest:
defunminmax(s, lo, hi):
return lo + s * (hi - lo)
defunstandard(z, mu, sigma):
return mu + z * sigma
rentals = [12, 30, 41, 60, 25]
lo = min(rentals)
hi = max(rentals)
print(lo, hi, unminmax(0.75, lo, hi), unminmax(1.25, lo, hi))
print(unstandard(1.5, 36, 12), unstandard(-0.5, 36, 12))
A scaled 0.75 means 48 tents. A scaled 1.25 means 72, more than any day in the data: the model is reaching past what it has seen.
Quick check
The trail club's walk times were standardized with mean μ = 62 minutes and σ = 8 minutes. A model predicts z = −1.5. What time is that?
The line a classifier draws. A linear classifier scores a point with two features (inputs), x₁ and x₂: s = w₁x₁ + w₂x₂ + b, with weights w₁ and w₂ and a bias b. It says yes when s > 0. Its decision boundary is where s = 0, the points it can't decide. That's the general form of a line, so make x₂ the subject:
w1x1+w2x2+b=0⇒x2=−w2w1x1+b,w2=0
In plain words: for each x₁ along the bottom of a chart, this gives the height x₂ where the boundary sits.
If w₂ = 0, the score ignores x₂, and the boundary is the vertical line x₁ = −b/w₁: solve for x₁ instead.
The trail club labels a walk hard from its length x₁ in km and its climb x₂ in hundreds of meters, with w₁ = 0.5, w₂ = 2 and b = −10:
defboundary_x2(w1, w2, b, x1):
if w2 == 0:
returnNonereturn -(w1 * x1 + b) / w2
for x1 in [0, 8, 12, 16]:
print(x1, boundary_x2(0.5, 2, -10, x1))
print(boundary_x2(0.5, 0, -10, 8))
w₂ is positive, so points above the line score above 0: an 8 km walk is hard once it climbs more than 300 meters. With a negative w₂, solving s > 0 for x₂ flips the sign, as in lesson 36.
−(1 × 6 + 4) ÷ (−2) = −10 ÷ −2 = 5.0. With w₂ = 0 the score has no x₂ in it, so there's no x₂ = … form: the boundary is the vertical line x₁ = 6/3 = 2.
Drill
Unscale, then draw the boundary
Write unminmax(s, lo, hi), the real value of a min-max scaled s, and boundary_x2(w1, w2, b, x1), the height of the decision boundary at x1, returning None when w2 is 0.
31Formulas with fractions: clear them firstFreeClearing denominators, cross-multiplying, a letter in the bottom, and reciprocal formulas like 1/R = 1/R₁ + 1/R₂.35 min
37Inverse functions, lines and scalingFreeUndoing a function by making x the subject, domains that allow it, line forms, and unscaling predictions.40 min
38Sequences and sums: find n, a, d and rFreeEvery letter of the arithmetic and geometric formulas as the subject, and an average's window from its decay.40 min
40Review: rearrange anything (algebra)FreeBroken rearrangements to fix, mixed practice from the whole algebra block, a strategy chart, and its formula sheet.35 min
41Coordinates and distanceFreePoints as (x, y), Pythagoras, distance in any number of coordinates, and nearest neighbors.42 min
42Angles, degrees and radiansFreeMeasuring a turn, converting units, arcs, and the angle between two arrows.36 min
43Sine and cosineFreeAcross and up: right triangles, the unit circle, and directions and rotations.42 min
44Waves: height, speed and shiftFreeAmplitude, period and phase, sampling a wave, and position codes in transformers.37 min
68Vector equations: rearranging with arrowsFreeSolving for a vector or a scale, recovering a gradient from two weight snapshots, and an angle from a dot product.40 min
87Counting: choices, orders and groupsFreeThe counting principle, factorials, ordered picks and groups, and why attention's cost grows with the square of the length.43 min
88Probability: from counts to chancesFreeEqually likely outcomes, the complement, distributions, seeded simulation, and a next-word model.43 min
89Combining events: and, or, independentFreeThe or rule, independence, probability trees, drawing with and without replacement, and chains of steps.42 min
100Maximum likelihoodFreeLikelihoods as products, log-likelihoods as sums, grid search, underflow, and the negative log-likelihood loss.45 min
101Linear regression: the best lineFreeResiduals, least squares by formula, three roads to one line, the matrix form, and train versus test error.45 min
109One neuron, start to finishFreeWeighted sum, activation, loss, slopes and update, by hand and in code, and what one straight line can and can't split.45 min
111Training a small networkFreeThe backward pass in matrix form, gradient checks, a full training loop, and reading loss curves.45 min
112The same network in PyTorchFreenn.Sequential with the NumPy weights loaded, the same loss and slopes, then SGD and mini-batches.45 min
113Attention as matrix mathFreeScaled dot-product scores, the causal mask, a row-by-row softmax, and blending value vectors.45 min
114Residual connections and layer normFreeAdding the input back and why its slope keeps learning alive, layer norm by hand, and add and norm.40 min
115A transformer block, start to finishFreeA sentence through embeddings, attention, add and norm, a small network and next-word scores, every shape named.45 min
117Final test: transpose any formulaFreeSlips to fix from every topic, mixed practice of every kind of rearranging, the strategy chart and the full sheet.45 min
Learn the math behind AI from arithmetic up: numbers, algebra, vectors and matrices, calculus, probability and statistics, each shown as numbers moving and checked in Python, ending with a network you build and train from the formulas.
Learn to make any letter of any formula the subject: layers undone in reverse order, fractions, a letter that appears twice, powers, roots, quadratics, logs, inequalities, trigonometry, vectors, matrices, calculus, probability, statistics and the formulas inside AI models, each checked in Python.
See the course →
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write