Sometimes the letter you want appears in two places, like A = P + Prt, where P shows up twice. No single opposite can undo both at once: take one away and the other is still there. The fix is a new move. Gather every term with the letter on one side, pull the letter out as a common factor, and divide by what's left in the bracket. It's the move behind transposing precision and the F1 score, two numbers in every classifier's report.
What you'll be able to do
Recognize when the letter you want appears more than once, so peeling layers can't free it
Gather every term with the letter on one side, factor the letter out, and divide by the bracket
Rearrange formulas like A = P + Prt and y = (x + 2)/(x − 3), stating the value the bracket can't take
Spot an unfinished rearrangement: one that still has the letter on both sides
Rearrange precision and the F1 score to find the true positives or the recall a target needs
Quick check
Have a guess before you start. In A = P + Prt, can you make P the subject by undoing one layer at a time?
Yes: take Prt from both sides, and P is alone
Yes: take P from both sides, then divide by rt
Yes: divide both sides by rt, then by P
No: the two P terms must be combined first
Show the answer
No: the two P terms must be combined first
Any move that frees one P leaves the other behind. Combine them first: A = P(1 + rt), with P once. Then divide: P = A/(1 + rt).
The math: gather, factor, divide
Purpose. A method for formulas where the letter you want appears more than once.
Clear any fractions (the last lesson's move).
Expand any brackets that hide the letter.
Move every term with the letter to one side and every other term to the other.
Factor the letter out: ax − cx = x(a − c). The letter now appears once.
Divide by the bracket. The condition: the bracket can't be 0.
In ax + b = cx + d, take cx and b from both sides: ax − cx = d − b. Factor: x(a − c) = d − b. Divide: x = (d − b)/(a − c), as long as a ≠ c. If a = c, the x terms cancel completely, and there's either no answer or every number works.
Quick check
From an earlier lesson: pull the common factor out of 6x + 9xy.
3x(2 + 9y)
x(6 + 9)
3x(2 + 3y)
3(2x + 3y)
Show the answer
3x(2 + 3y)
Both terms have a 3 and an x: 6x = 3x × 2 and 9xy = 3x × 3y. Multiply back to check: 3x(2 + 3y) = 6x + 9xy.
When the letters cancel. Gathering can make the letter vanish. In 2x + 3 = 2x + 5, taking 2x from both sides leaves 3 = 5, which is never true, so no x works: two parallel lines that never meet. In 2x + 3 = 2x + 3, it leaves 3 = 3, which is always true, so every x works. That's what the condition a ≠ c guards: the bracket (a − c) is 0 exactly when the letter cancels.
Watch the balance gather the x terms of 5x + 2 = 3x + 12, the formula ax + b = cx + d with numbers in. Taking 3x from both pans is the "gather" step; the rest is the method you know.
Try it now
Change the numbers: whenever the x's differ on the two pans, gathering them leaves one x term.
Symbols and formula. A fraction with the letter on top and bottom needs all three moves: clear, gather, factor.
In plain words: get every copy of the letter onto one side, pull it out as a factor, then divide by whatever is left with it.
The condition y ≠ 1 isn't just a rule: no x ever makes (x + 2)/(x − 3) equal 1, because the top is always 5 more than the bottom. The rearranged formula tells you that by refusing to divide by 0.
Quick check
Make x the subject of y = (x + 1)/(x − 2).
x = (x + 1)/(y − 2)
x = (y + 1)/(y − 2)
x = (2y − 1)/(y + 1)
x = (2y + 1)/(y − 1)
Show the answer
x = (2y + 1)/(y − 1)
y(x − 2) = x + 1, so xy − 2y = x + 1. Gather: xy − x = 2y + 1. Factor: x(y − 1) = 2y + 1. Divide: x = (2y + 1)/(y − 1). Check with x = 5: y = 6/3 = 2, and (4 + 1)/(2 − 1) = 5.
In your own words
Why can't you finish a rearrangement while the letter still appears on both sides?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: test what you can't see at a glance
Purpose. A rearrangement with gathering has more places to slip, so the round trip earns its keep.
defy_of(x):
return (x + 2) / (x - 3)
defx_of(y):
if y == 1:
returnNonereturn (3 * y + 2) / (y - 1)
for x in [4, 8, 13, 0.5]:
print(x, round(y_of(x), 4), round(x_of(y_of(x)), 9))
print(x_of(1))
It runs right here in your browser, with nothing to install.
Every input comes back. Rounding to 9 decimal places hides the tiny rounding errors of decimals, which are about a millionth of a millionth here. The last line shows the condition at work: y = 1 is the one value no x can produce.
Worked example
Worked example: the break-even point
A stall sells walking maps at $12 each. Each map costs $4 to print, and the stall costs $200 to run. Revenue 12x equals cost 4x + 200 at the break-even point: x appears on both sides.
Gather: 12x − 4x = 200, factor: x(12 − 4) = 200, divide: x = 200/8 = 25 maps. The last line checks it: 25 maps bring in $300 and cost $300. If a map sells for exactly what it costs to print, every sale adds as much cost as revenue and the $200 is never covered, so the function returns None. That's the condition price ≠ unit cost.
x_of(6) = 20 ÷ 5 = 4.0 and x_of(2) = 8 ÷ 1 = 8.0. Dividing gives floats, so both print with .0.
Why not just guess? You could find the break-even point by trying 10 maps, then 20, then 30, as in the guess, check and adjust method from solving equations. It works, but each new price or running cost means guessing all over again. The rearranged formula answers every version in one line, and the check on the last line of the example shows it's right. Gathering and factoring once saves all the guessing that would follow.
Quick check
Which line shows a rearrangement that isn't finished?
x = (y + 3x)/2
x = 2y − 7
x = (y − 5)/4
x = (3y + 2)/(y − 1)
Show the answer
x = (y + 3x)/2
x appears on the right as well, so you'd need x to work out x. Gather: 2x = y + 3x gives −x = y, so x = −y.
Drill
Fix the break-even point
break_even(price, unit_cost, fixed) should solve price × x = unit_cost × x + fixed for x, and return None when price equals unit_cost. Fix it.
In the lesson, your code is checked as you write it.
In your own words
The break-even formula refuses price = unit cost. What does that mean for the stall, in plain words?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: precision and F1, turned around
Purpose. A classifier's report shows precision, recall and F1. Planning questions usually ask for a number that appears twice in them.
True positives from precision. Precision is P = TP/(TP + FP). If you know the precision and the false alarms, TP appears on top and inside the bottom. Clear: P(TP + FP) = TP. Gather: P·FP = TP − P·TP. Factor: P·FP = TP(1 − P). Divide: TP = P·FP/(1 − P).
The recall a target needs. F1 combines precision P and recall R (the share of real yeses the model finds): F1 = 2PR/(P + R). To find the recall needed, clear: F1(P + R) = 2PR, gather the R terms: F1·P = 2PR − F1·R = R(2P − F1), divide: R = F1·P/(2P − F1). The conditions: 2P must be more than F1, and the answer must be at most 1, since no model finds more than all the yeses.
deftrue_pos(precision, fp):
return precision * fp / (1 - precision)
defrecall_needed(f1, precision):
if2 * precision <= f1:
returnNone
r = f1 * precision / (2 * precision - f1)
if r > 1:
returnNonereturn r
deff1(p, r):
return2 * p * r / (p + r)
print(round(true_pos(0.9, 12), 6), round(recall_needed(0.72, 0.6), 6))
print(round(f1(0.6, recall_needed(0.72, 0.6)), 6), recall_needed(0.9, 0.4), recall_needed(0.8, 0.6))
Quick check
A model has precision 0.75 and raised 30 false alarms. How many of its flags were right?
A steady average. Training code often keeps a running average of the gradient (the direction each weight should move): m = βm + (1 − β)g, with β around 0.9. If g stays the same for a long time, what does m settle at? The letter m appears twice. Gather: m − βm = (1 − β)g. Factor: m(1 − β) = (1 − β)g. Divide: m = g. The average settles exactly on the steady value, as it should.
Quick check
What does this print?
deff1(p, r):
return2 * p * r / (p + r)
print(round(f1(0.5, 1.0), 4), round(f1(0.8, 0.8), 4))
0.6667 0.64
1.0 1.6
0.75 0.8
0.6667 0.8
Show the answer
0.6667 0.8
2 × 0.5 × 1 ÷ 1.5 = 0.6667: F1 sits closer to the lower of the two. With both at 0.8, F1 is 0.8.
Drill
Targets for a classifier
Write true_pos(precision, fp) and recall_needed(f1, precision), returning None when 2 × precision isn't more than f1, or when the recall needed is more than 1.
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