Mathematical Foundations

I.4 · Mathematical Language

What does it mean to do the same thing to both sides?

An equation is a question about an unknown number, and every step of solving it must leave the set of answers unchanged. We keep a balance, learn the one rule that turns an inequality around, and meet the two steps that quietly change the answers: squaring and dividing by something that can be zero.

A balanced statement: L(x) □ R(x)L(x)\ \square\ R(x)

Start from a statement

A balance beam, drawn schematically. At x equal to 0, the left pan holds 2 and the right pan holds 10. The right pan is lower. The tilt shows which side is larger, not by how much.

A number line from minus 11 to 11 with the trial value x at 0. The statement 3 x plus 2 equals x plus 10 is true when x equals 4; those values are marked on the line.

trial value of x
0
left side at this x
2
right side at this x
10
left minus right (what tilts the beam)
−8
is "3x + 2 = x + 10" true at this x?
false
solution set of this statement
{4}

The beam tilts toward the larger side. Left minus right is 0 exactly at a solution of an equation.

At x = 0 the left side is 2 and the right side is 10: the right pan is lower, so x = 0 is not a solution. The statement is true exactly when x = 4.

3x+2=x+10  ⟺   x=4(S={4})\begin{aligned}3x+2&=x+10\\&\iff\ x=4\quad\bigl(S=\{4\}\bigr)\end{aligned}
left=3⋅0+2=2right=0+10=10left−right=−8\begin{aligned}\text{left}&=3\cdot 0+2=2\\\text{right}&=0+10=10\\\text{left}-\text{right}&=-8\end{aligned}

The history: every step done to both sides

3x+2=x+103x+2=x+10

The starting statement. It is true exactly when x = 4. Each operation is applied to both sides, and the set of solutions never changes.

Do the same to both sides

from −12 to 12, up to 2 decimal places

from −10 to 10, up to 2 decimal places

Each operation is applied to the latest statement. Selecting an earlier step in the history only shows it; nothing is undone.

A statement with an unknown x, and the history of what you do to both sides. Pick a statement, then choose an operation and press the button: the new statement is added to the history. The beam shows both sides at the trial value of x, and the line below it marks the solutions. Try multiplying by 0, and try dividing 4 − 2x < x + 10 by a negative number.

Try it before reading on. Start with 3x+2=x+103x+2=x+10 and subtract xx from both sides, then subtract 22, then divide by 22. After each step, drag the trial value of xx to 44 and watch the beam: it is level at the same value every time. Then pick the last statement, 4−2x<x+104-2x<x+10, divide by −3-3 and watch the relation turn around. Finally try to multiply by 00 and read why the stage refuses. By the end of the chapter you will know what a solution set is, why a balanced step keeps it, the one operation that changes an inequality, and the two familiar steps that can change an equation’s answers without warning.

Intuition: a question and a scale

A written equation looks like a fact. It is really a question. When you read 3x+2=x+103x+2=x+10 you are asked: for which numbers xx are the two sides equal? For x=4x=4 both sides are 1414. For x=0x=0 they are 22 and 1010. The unknown is a placeholder, and the equation picks out the numbers that fit.

A pair of kitchen scales is the right picture. The two pans hold the two sides. If they balance, the sides are equal, and whatever you do to one pan you must do to the other if you want them to keep balancing: take a kilogram off both, double both, halve both. The solving procedure is nothing else. Each step produces a new statement with the same answers, only simpler, until the unknown stands alone and the answer can be read off.

Two things can go wrong, and both are the subject of this chapter. Some steps do not keep the answers: multiplying by 00 destroys them, and squaring or dividing by an expression that can be 00 changes them. And the picture has to be extended to inequalities, where “the pan that is lower” is a statement about order and one operation, multiplying by a negative number, turns the order around.

This chapter prepares the next one, Functions as input, transformation and output, where the same letter xx stops being an unknown to be found and becomes an input that varies. It uses the squares and roots of the first chapter and the number line of the second.

Definition: expressions, equations, inequalities and their solutions

Expression. An expression is a recipe for a number, built from numbers, letters and the operations of arithmetic: 3x+23x+2, x2−1x^{2}-1, y4\frac{y}{4}. It has no "==" and is not true or false. It has a value once its letters have values.

Variable, parameter, unknown. A letter stands for a number, and the word used says what the letter is doing. An unknown is a letter whose value we are trying to find (xx in 3x+2=x+103x+2=x+10). A variable is a letter that is allowed to take many values, and we study what happens as it does (xx in f(x)=3x+2f(x)=3x+2). A parameter is a letter that is fixed for a given problem but could have been another number (aa in ax+bax+b). The same symbol can be any of them, depending on the question.

Equation. An equation states that two expressions have the same value: L(x)=R(x)L(x)=R(x). It is a condition on xx. It is true for some values of xx, false for others.

Inequality. Replace "==" by <<, ≤\le, >> or ≥\ge. The statement is again a condition on xx.

Solution set. The solution set of a statement is the set of all numbers xx that make it true. Two statements are equivalent when they have the same solution set. Solving means replacing a statement by equivalent ones until the answer is visible.

Identity. An equation that is true for every xx, such as 2(x+1)=2x+22(x+1)=2x+2, is an identity. An equation that is true for no xx, such as 2(x+1)=2x+12(x+1)=2x+1, is a contradiction. Everything else is a conditional equation: it is true for some xx and false for others.

Balanced operation. An operation applied to both sides of a statement is balanced when the new statement is equivalent to the old. The balanced operations for a linear statement a1x+b1 □ a2x+b2a_1x+b_1\ \square\ a_2x+b_2 are:

  • add the same number, or the same multiple of xx, to both sides (subtracting is adding a negative number);
  • multiply both sides by a number k≠0k\neq 0, or divide both sides by k≠0k\neq 0.

Each is reversible: adding cc is undone by adding −c-c, and multiplying by kk is undone by dividing by kk. That is exactly why it preserves the solution set. If a step turns SS into S′S' and the inverse step turns S′S' back into SS, then every solution of SS is a solution of S′S' and vice versa. Multiplying by 00 has no inverse, and the stage refuses it.

Keeping the balance

A linear equation a1x+b1=a2x+b2a_1x+b_1=a_2x+b_2 has a solution set of exactly one of three kinds. Move the xx terms to the left and the constants to the right, which are two balanced steps. What remains is

(a1−a2) x=b2−b1.(a_1-a_2)\,x=b_2-b_1 .

Let m=a1−a2m=a_1-a_2 and c=b2−b1c=b_2-b_1. Then:

value of mmthe equation mx=cmx=csolution set
m≠0m\neq 0divide by mm: x=c/mx=c/mexactly one number
m=0m=0, c≠0c\neq 00=c0=c, which is falseno solution
m=0m=0, c=0c=00=00=0, which is trueevery xx

The stage follows this: the beam, the history and the inspector are all computed from the same statement. The beam shows L(x)L(x) in the left pan and R(x)R(x) in the right pan at the trial value of xx. The pan with the larger value is lower, and the beam is exactly level only when the two sides are equal, that is, at a solution of an equation. The amount of tilt is schematic (it grows with the difference but is capped so that the picture stays on the page); the inspector shows the true numbers L(x)L(x), R(x)R(x) and L(x)−R(x)L(x)-R(x).

Look at the presets. For 2x+3=2x+72x+3=2x+7 the xx terms cancel and the history ends with the false statement 0=40=4; the solution set is empty, and the verdict is marked as a caution. For 2x+3=2x+32x+3=2x+3 they cancel to the true 0=00=0 and every xx works.

The history is a record. Selecting an earlier step with the stepper or the slider shows that statement; it never undoes anything, and the solution set stays the same at every step. You can remove the last step explicitly with its own button, which is a different action.

Why multiplying by 00 is refused

Start from x=3x=3 and multiply both sides by 00. The result is 0=00=0, which is true for every xx. The original had one solution and the new statement has all of them. Since no number turns 00 back into 33, the step cannot be undone. A step that changes the solution set is not balanced, and the stage shows its reason instead of accepting it. Dividing by 00 is refused for the plain reason that division by 00 is undefined.

Inequalities and the order-reversing rule

An inequality such as 5x−1≥2x+85x-1\ge 2x+8 is solved by the same balanced steps, with its relation carried along. Adding a number to both sides keeps the relation. Multiplying by a positive kk keeps it. The one new fact is this.

If p<qp<q and k<0k<0, then kp>kqkp>kq.

Proof. Since p<qp<q, the number q−pq-p is positive. A negative number times a positive number is negative, so k(q−p)<0k(q-p)<0, that is kq−kp<0kq-kp<0, that is kq<kpkq<kp. □\square

So multiplying or dividing by a negative number reverses the relation: << becomes >>, and ≤\le becomes ≥\ge. Geometrically, multiplying by −1-1 reflects the number line through 00, which swaps left and right, so what was to the left is now to the right; multiplying by −3-3 reflects and then stretches by 33.

Multiply an inequality by kk: keep the order or reverse it

Try a pair and a multiplier

Two number lines. On the upper line p is at 2 and q is at 5, so 2 is less than 5. On the lower line, after multiplying by minus 2, kp is at minus 4 and kq is at minus 10, so minus 4 is greater than minus 10.

The products on a wider line. kp is at minus 4 and kq is at minus 10.

first number
2
second number
5
multiplier
−2
first number times k
−4
second number times k
−10
the order before
2 < 5
the true order after
−4 > −10
does the old relation still hold?
no

Multiplying by a negative k reflects the line through 0, so the numbers swap sides.

k = −2 is negative, so the order is REVERSED: "2 < 5" becomes "−4 > −10". Counterexample: if you kept the old sign you would write "−4 < −10", and that is false for these numbers. Multiplying by a negative number reflects the line through 0 (and stretches it by |k|), which swaps left and right.

kp=(−2)⋅2=−4kq=(−2)⋅5=−10\begin{aligned}kp&=(-2)\cdot 2=-4\\kq&=(-2)\cdot 5=-10\end{aligned}
2<5 ⟹ −4>−10k is negative: the order is reversed\begin{aligned}2<5&\ \Longrightarrow\ -4>-10\\&\text{k is negative: the order is reversed}\end{aligned}

from −6 to 6, up to 2 decimal places

from −6 to 6, up to 2 decimal places

from −3 to 3, up to 2 decimal places

Two numbers p and q on a number line, and their products with k below. Drag p and q or type them, and change k. For k greater than 0 the arrow keeps its direction; for k less than 0 it reverses. With k equal to 0 both products collapse to 0 and the step is rejected.

The stage states the counterexample in numbers. With p=2p=2, q=5q=5 and k=−2k=-2, the true order is 2<52<5, and the products are kp=−4kp=-4 and kq=−10kq=-10. If you forgot to reverse and wrote −4<−10-4<-10, you would have written a false statement. The true one is −4>−10-4>-10. The multiplier k=0k=0 is rejected for a different reason: 0⋅p0\cdot p and 0⋅q0\cdot q are both 00, so the strict inequality p<qp<q becomes the false 0<00<0, or loses its meaning altogether, and nothing can be divided back.

The solution set of an inequality is an interval, with an exact endpoint and an end that is open or closed. For 5x−1≥2x+85x-1\ge 2x+8 the steps give 3x≥93x\ge 9, then x≥3x\ge 3: the set [3,∞)[3,\infty), and 33 belongs to it because the relation allows equality. For 4−2x<x+104-2x<x+10 the steps give −3x<6-3x<6, then dividing by −3-3 (reversing) x>−2x>-2: the set (−2,∞)(-2,\infty), with −2-2 excluded. A square bracket or a filled dot means included; a round bracket or an open dot means excluded.

Squaring is not a balanced step

Squaring both sides is tempting: it removes a square root. But the step a=b ⇒ a2=b2a=b\ \Rightarrow\ a^{2}=b^{2} only goes one way. The squared equation a2=b2a^{2}=b^{2} is also true when a=−ba=-b, so the new statement can have more solutions than the old. The extra ones are called extraneous roots.

Take the family x+p=x+q\sqrt{x+p}=x+q with whole numbers p,qp,q. The square root sign means the non-negative root, so a solution must make the right side non-negative as well. Write t=x+qt=x+q for the right side. Then x+p=t−q+px+p=t-q+p, and squaring x+p=(x+q)2x+p=(x+q)^{2} gives

t2=t+(p−q),t=1±D2,D=4p−4q+1.t^{2}=t+(p-q),\qquad t=\frac{1\pm\sqrt{D}}{2},\qquad D=4p-4q+1 .

So x=t−q=1−2q±D2x=t-q=\frac{1-2q\pm\sqrt{D}}{2}, which is the quadratic x2+(2q−1)x+(q2−p)=0x^{2}+(2q-1)x+(q^{2}-p)=0 with discriminant DD. The number DD is always odd.

A candidate x0x_0 solves the original equation exactly when its right side x0+q=tx_0+q=t is not negative. If t<0t<0, the number x0x_0 solves −x+p=x+q-\sqrt{x+p}=x+q instead: the other sign of the root. Because the two values of tt add up to 11, the larger one is at least 12\tfrac12, so at least one candidate is always valid whenever there are candidates. Whether the second is valid depends on the sign of its tt.

Squaring can invent roots: x+p=x+q\sqrt{x+p}=x+q

Try an equation

A coordinate plane. The solid curve is y equals the square root of x plus 2, the dashed curve is y equals minus that square root, and the straight line is y equals x. Root 1 is at x equal to minus 1, on the lower, dashed branch: extraneous. Root 2 is at x equal to 2, on the upper, solid branch: valid.

shift under the root
2
shift of the line
0
discriminant 4p − 4q + 1 of the squared equation
9
root 1 of the squared equation
−1
root 1 in the ORIGINAL equation
extraneous
root 2 of the squared equation
2
root 2 in the ORIGINAL equation
valid

= exact · ≈ rounded · a root is valid only if the right-hand side x + q is not negative

Root 1, x = −1: the right-hand side x equals −1, which is negative, and a square root is never negative. This x solves −√(x + 2) = x instead. Extraneous. Root 2, x = 2: the right-hand side x equals 2, which is not negative, and the radicand x + 2 equals 4 = 2², so √4 = 2. Valid. Squaring produced 2 candidates but only 1 solves the original equation.

x+2=xx+2=x2(squaring is not an equivalence)⇒ x2−x−2=0D=4p−4q+1=9\begin{aligned}\sqrt{x+2}&=x\\x+2&=x^{2}\\&\text{(squaring is not an equivalence)}\\&\Rightarrow\ x^{2}-x-2=0\\&D=4p-4q+1=9\end{aligned}
x1=−1,right side=−1 <0 (extraneous)x2=2,right side=2 ≥0 (valid)\begin{aligned}x_{1}&=-1,\quad \text{right side}=-1\ <0\ \text{(extraneous)}\\x_{2}&=2,\quad \text{right side}=2\ \ge 0\ \text{(valid)}\end{aligned}

from −6 to 6, whole numbers only

from −6 to 6, whole numbers only

The curve y = √(x + p) (solid), the same curve with the opposite sign (dashed) and the line y = x + q. Each root of the squared equation sits on the line: a filled marker on the solid curve means it solves the original equation, an open-looking marker on the dashed curve means it solves the other sign and is extraneous. Change p and q, or use the presets.

The preset p=2p=2, q=0q=0 is x+2=x\sqrt{x+2}=x. Squaring gives x+2=x2x+2=x^{2}, so x2−x−2=0x^{2}-x-2=0 and (x−2)(x+1)=0(x-2)(x+1)=0. The candidates are 22 and −1-1. Substitute into the original equation: for x=2x=2, 4=2\sqrt{4}=2 and the right side is 22, so it is valid. For x=−1x=-1, 1=1\sqrt{1}=1 but the right side is −1-1: extraneous. It is a solution of −x+2=x-\sqrt{x+2}=x instead.

The preset p=1p=1, q=0q=0 has the irrational candidates 1±52\frac{1\pm\sqrt5}{2}. The sign of 1−52\frac{1-\sqrt5}{2} is decided exactly (it is negative because 5>1\sqrt5>1), not by looking at its decimal −0.618034…-0.618034\ldots. The stage also shows p=0p=0, q=1q=1 where D=−3<0D=-3<0: there is no real candidate and squaring cannot create any.

The only reliable test of a candidate is to put it back into the original equation.

The honest scope of this family

Squaring is one of several steps that can add roots. Multiplying both sides by an expression that can be 00 does it too. This chapter treats one family exactly, so that the claim “a candidate is valid exactly when x+q≥0x+q\ge0” can be checked on every case. Other equations with radicals, and equations with fractions, need the same discipline: find the candidates by whatever steps you like, then check them in the original.

Dividing by something that can be zero

The opposite danger is losing a root. Consider x(x−2)=3(x−2)x(x-2)=3(x-2). A tempting step is to divide both sides by (x−2)(x-2), which leaves x=3x=3. But the division is allowed only where x−2≠0x-2\neq0, that is for x≠2x\neq2. At x=2x=2 both sides equal 00, so x=2x=2 is a solution, and the division threw it away.

The safe way is to move everything to one side and factor:

x(x−2)−3(x−2)=0  ⟺  (x−2)(x−3)=0  ⟺  x=2 or x=3.x(x-2)-3(x-2)=0\iff(x-2)(x-3)=0\iff x=2\ \text{or}\ x=3 .

A product is 00 exactly when one of its factors is 00, so both roots appear. In general x(x−c)=m(x−c)x(x-c)=m(x-c) is equivalent to (x−c)(x−m)=0(x-c)(x-m)=0.

Dividing by a factor that can be zero: x(x−c)=m(x−c)x(x-c)=m(x-c)

Try an equation

A coordinate plane with a parabola, the left side, and a straight line, the right side. They cross where the equation is true, at x equal to 2 and 3. The crossing at x equal to 2 is the one that dividing by the factor throws away.

the zero of the factor
2
the number on the right
3
all solutions
{2, 3}
what the division concludes
x = 3
the solution the division loses
x = 2
left side at x = c
0
right side at x = c
0

x(x − 2) = 3(x − 2): both sides are equal at every solution

x(x − 2) = 3(x − 2) has two solutions, {2, 3}. Dividing both sides by (x − 2) is allowed only where x − 2 is not 0, that is for x other than 2. It leaves x = 3 and silently throws away x = 2, where both sides equal 0 (0 = 0). Move everything to one side and factor instead.

x(x−2)=3(x−2)  ⟺   (x−2)(x−3)=0  ⟺   x=2 or x=3\begin{aligned}x\left(x-2\right)=3\left(x-2\right)&\\\iff\ \left(x-2\right)\left(x-3\right)&=0\\\iff\ x=2\ \text{or}\ x=3&\end{aligned}
divide by (x−2): x=3but at x=2: x−2=0so the division was by 0 and x=2 was lost\begin{aligned}\text{divide by }(x-2):\ x&=3\\\text{but at }x=2:\ x-2&=0\\\text{so the division was by }0\text{ and }x=2\text{ was lost}&\end{aligned}

from −6 to 6, whole numbers only

from −6 to 6, whole numbers only

The left side x(x − c) is a parabola and the right side m(x − c) is a line. They cross where the equation is true. The marker labelled lost is the solution that dividing by (x − c) throws away; when c equals m there is a single touching point and nothing is lost.

When c=mc=m the equation is (x−c)2=0(x-c)^{2}=0 with the single root cc. Dividing gives x=m=cx=m=c: nothing is lost as a value, but the division was still not justified. The stage keeps both cases apart.

The same lesson applies to simplifying a fraction such as x2−4x−2=x+2\frac{x^{2}-4}{x-2}=x+2: the two sides agree for every x≠2x\neq2, but the left side is undefined at 22. A simplification that is valid only where a divisor is not 00 carries that restriction with it. The restriction on xx is called a domain restriction.

Expanding brackets: identities and slips

The distributive law says that multiplication spreads over addition: a(bx+c)=abx+aca(bx+c)=abx+ac for every a,b,c,xa,b,c,x. It is an identity. Written as an equation a(bx+c)=abx+aca(bx+c)=abx+ac, its solution set is every xx.

The most common slip multiplies only the first term: a(bx+c)=abx+ca(bx+c)=abx+c. The stage compares the two. Write the equation as a pair of lines y=a(bx+c)y=a(bx+c) and y=abx+cy=abx+c. They have the same slope abab, so they are parallel. They coincide when the constants agree, ac=cac=c, which happens exactly when c(a−1)=0c(a-1)=0, that is a=1a=1 or c=0c=0. Otherwise they are a constant distance c(a−1)c(a-1) apart, and the slip’s equation is a contradiction: true for no xx. A single numerical check at one xx already catches it.

Expression, equation, identity: a(bx+c)a(bx+c)

Try a bracket

A coordinate plane with two straight lines: y equals 2x + 6, the bracket as written, and y equals 2x + 6, its expansion. The two lines coincide: they are the same function of x. At x equal to 2 the heights are 10 and 10.

outer factor
2
coefficient of x inside
1
constant inside
3
value of x
2
the bracket at this x
5
the product, bracket first
10
every term multiplied
10
product minus expansion
0
what kind of statement is the equation?
identity

The solid line is the bracket as written, the dashed line is the expansion. The equation is an identity when the lines coincide.

At x = 2 the bracket gives 10 and the expansion gives 10. They agree, and not by luck: both simplify to 2x + 6, so the equation 2x + 6 = 2x + 6 is true for every x. It is an identity: the distributive law.

a(bx+c)=2(1⋅2+3)=2⋅5=10\begin{aligned}a(bx+c)&=2\left(1\cdot 2+3\right)=2\cdot 5=10\end{aligned}
abx+ac=2⋅1⋅2+2⋅3=4+6=10\begin{aligned}abx+ac&=2\cdot 1\cdot 2+2\cdot 3=4+6=10\end{aligned}

from −3 to 3, whole numbers only

from −3 to 3, whole numbers only

from −5 to 5, whole numbers only

from −6 to 6, up to 2 decimal places

Expand by

The bracket a(bx + c) as a solid line and its expansion as a dashed line. Pick whether the expansion multiplies every term or only the first. At the chosen x the inspector shows both values and their gap. The lines coincide for an identity and are parallel for the slip.

This is the difference between the words in the definition. The bracket is an expression. Setting it equal to its correct expansion makes an identity, true for every xx. Setting it equal to the slip makes a contradiction, true for no xx (except in the harmless cases a=1a=1 or c=0c=0, where the slip happens to be right). An equation like 2(x+1)=x+52(x+1)=x+5 is conditional: true only at x=3x=3.

Reading f(x)=ax+bf(x)=ax+b is a first look at the next chapter: the same expression ax+bax+b, but now xx is an input, and aa and bb are parameters that define the rule.

Worked examples

Solving a linear equation is three moves in a fixed order, and it pays to see them as steps:

  1. Collect the xx terms on one side by adding the opposite of the unwanted xx term to both sides.
  2. Collect the constants on the other side the same way.
  3. Divide by the number in front of xx (reversing an inequality if it is negative) and then check in the original statement.

Step through five cases. The first is 3x+2=x+103x+2=x+10. The second has a solution that is not a whole number. The third is the inequality 4−2x<x+104-2x<x+10, where the last step reverses the relation. The fourth is x+2=x\sqrt{x+2}=x with its extraneous root. The last is the division that loses a root.

Worked examples: solve, then check

Choose a problem

3x+2=x+103x+2=x+10

The starting statement. It is true exactly when x = 4. Each operation is applied to both sides, and the set of solutions never changes.

Choose a problem and step through the work. Going back selects an earlier step; nothing is undone.

Executable reference code

Run the Python examples below to follow the calculations step by step. First the solver: a function that returns one of exactly three answers, the inequality version with its reversal, and the case of multiplying by 00.

code/python/algebra_solve.pyexact solving with fractions, the three cases, the reversal
from fractions import Fraction


def solve_linear(a1, b1, a2, b2):
    """Solve a1*x + b1 = a2*x + b2 exactly. The answer is one of exactly three things."""
    m = a1 - a2  # what is left in front of x after moving the x-terms to the left
    c = b2 - b1  # what is left on the right after moving the constants
    if m == 0:  # the equation reads 0 = c and does not involve x at all
        return ("all",) if c == 0 else ("none",)
    return ("point", Fraction(c) / m)  # Fraction keeps integers and decimals-from-strings exact


def solve_inequality(a1, b1, rel, a2, b2):
    """Solve a1*x + b1 (rel) a2*x + b2 for rel in '<', '<=', '>', '>='. Dividing by a negative m reverses the relation."""
    flipped = {"<": ">", "<=": ">=", ">": "<", ">=": "<="}
    m, c = a1 - a2, b2 - b1
    if m == 0:  # 0 (rel) c: true for every x or for none
        holds = {"<": 0 < c, "<=": 0 <= c, ">": 0 > c, ">=": 0 >= c}[rel]
        return ("all",) if holds else ("none",)
    if m < 0:  # dividing both sides by a negative number reverses the order
        rel = flipped[rel]
    return ("x", rel, Fraction(c) / m)


# 3x + 2 = x + 10  ->  2x = 8  ->  x = 4
print(solve_linear(3, 2, 1, 10))  # ('point', Fraction(4, 1))
print(solve_linear(2, 3, 2, 7))  # ('none',)   : 0 = 4
print(solve_linear(2, 3, 2, 3))  # ('all',)    : 0 = 0

# the same operation on both sides keeps the solution: subtract x, subtract 2, divide by 2 (each step is reversible)
step1 = (3 - 1, 2, 0, 10)  # 2x + 2 = 10
step2 = (2, 0, 0, 8)  # 2x = 8
step3 = (1, 0, 0, 4)  # x = 4
history_agrees = all(solve_linear(*s) == solve_linear(3, 2, 1, 10) for s in (step1, step2, step3))
print(history_agrees)  # True

# multiplying both sides by 0 is the one operation that is NOT allowed: 0 = 0 holds for every x, but x = 4 was the only solution
times_zero = solve_linear(0 * 3, 0 * 2, 0 * 1, 0 * 10)  # ('all',)
print(times_zero, times_zero == solve_linear(3, 2, 1, 10))  # ('all',) False

# 4 - 2x < x + 10  ->  -3x < 6  ->  dividing by -3 reverses the order:  x > -2
print(solve_inequality(-2, 4, "<", 1, 10))  # ('x', '>', Fraction(-2, 1))
wrong_if_not_reversed = ("x", "<", Fraction(-2))  # what forgetting the reversal would claim
test_point = Fraction(-5)  # satisfies the WRONG answer (-5 < -2) ...
original_holds_at_test_point = (-2 * test_point + 4) < (1 * test_point + 10)  # 14 < 5 is False
print(original_holds_at_test_point)  # False: ... but not the original inequality

# exactness: the same equation 0.1x + 0.2 = 0.3 solved with floats and with Fractions built from strings
float_x = (0.3 - 0.2) / 0.1  # 0.9999999999999998
exact_x = solve_linear(Fraction("0.1"), Fraction("0.2"), Fraction(0), Fraction("0.3"))[1]  # Fraction(1, 1)
print(float_x, exact_x, float_x == 1, exact_x == 1)  # 0.9999999999999998 1 False True

# a coefficient that should cancel to exactly 0 may not, in floats: 0.1 + 0.2 - 0.3 is 2**-54, not 0
m_float = (0.1 + 0.2) - 0.3
print(m_float, m_float == 2.0**-54, (2 - 1) / m_float)  # 5.551115123125783e-17 True 1.8014398509481984e+16 : "no solution" became a huge x
m_exact = (Fraction("0.1") + Fraction("0.2")) - Fraction("0.3")
print(m_exact, solve_linear(Fraction("0.1") + Fraction("0.2"), 1, Fraction("0.3"), 2))  # 0 ('none',)

Then the extraneous-root check, with the substitution test, an exact test with Fraction, and the division fallacy.

code/python/algebra_extraneous.pysquare, solve, then substitute into the original
import math
from fractions import Fraction


def squared_roots(p, q):
    """Roots of the SQUARED equation x + p = (x + q)**2, i.e. x**2 + (2q - 1)x + (q**2 - p) = 0, as floats (None when there are none)."""
    discriminant = 4 * p - 4 * q + 1
    if discriminant < 0:
        return None
    root = math.sqrt(discriminant)
    return sorted([(1 - 2 * q - root) / 2, (1 - 2 * q + root) / 2])


def classify(p, q, x):
    """Substitute a candidate into the ORIGINAL equation sqrt(x + p) = x + q. Squaring cannot be trusted; substitution can."""
    if x + p < 0:
        return "outside the domain"  # sqrt(x + p) is not defined for x < -p
    return "valid" if math.isclose(math.sqrt(x + p), x + q, abs_tol=1e-12) else "extraneous"


def exact_check(p, q, x):
    """The same test in exact arithmetic for a Fraction x: sqrt(x + p) is a Fraction only if numerator and denominator are perfect squares."""
    r = x + p
    if r < 0:
        return False
    n, d = math.isqrt(r.numerator), math.isqrt(r.denominator)
    return n * n == r.numerator and d * d == r.denominator and Fraction(n, d) == x + q


# p = 2, q = 0:  sqrt(x + 2) = x  ->  x + 2 = x**2  ->  x = -1 or x = 2
roots = squared_roots(2, 0)  # [-1.0, 2.0]
verdicts = [classify(2, 0, x) for x in roots]  # ['extraneous', 'valid']
print(roots, verdicts)
print(math.sqrt(-1 + 2), "!=", -1)  # 1.0 != -1 : the root -1 satisfies -sqrt(x + 2) = x, the other sign
print([exact_check(2, 0, Fraction(x)) for x in (-1, 2)])  # [False, True]

# p = 1, q = 0:  sqrt(x + 1) = x  has the irrational candidates (1 +- sqrt(5))/2
golden, conjugate = squared_roots(1, 0)[1], squared_roots(1, 0)[0]
print(classify(1, 0, golden), classify(1, 0, conjugate))  # valid extraneous
print(golden, conjugate)  # 1.618033988749895 -0.6180339887498949
print(math.sqrt(conjugate + 1), -conjugate)  # both are 0.618... (equal up to rounding): the conjugate solves -sqrt(x + 1) = x
print(squared_roots(0, 1))  # None : sqrt(x) = x + 1 has no candidates at all (the discriminant is -3)

# The division fallacy: x(x - 2) = 3(x - 2).  Dividing by (x - 2) gives x = 3 and loses x = 2, where both sides are 0.
def both_sides(x, c=2, m=3):
    return x * (x - c), m * (x - c)


solutions = [x for x in range(-10, 11) if both_sides(x)[0] == both_sides(x)[1]]  # a scan, nothing divided
print(solutions)  # [2, 3]
print(both_sides(2))  # (0, 0) : dividing by (x - 2) is dividing by zero at x = 2

Finally the two machine-learning situations of this chapter: undoing a standardisation, and a learning rate as a factor in an inequality.

code/python/algebra_affine.pyinverting an affine map; 0 < eta < 1
from fractions import Fraction


# Standardising a feature is an affine map z = (x - mu) / sigma. Undoing it is solving z = (x - mu) / sigma for x:
# multiply both sides by sigma (not 0), then add mu: both are balanced, reversible operations.
def standardise(x, mu, sigma):
    return (x - mu) / sigma


def unstandardise(z, mu, sigma):
    return z * sigma + mu


mu, sigma = Fraction(50), Fraction(8)
values = [Fraction(v) for v in (42, 50, 66, 53)]
z = [standardise(v, mu, sigma) for v in values]  # [-1, 0, 2, 3/8]
back = [unstandardise(t, mu, sigma) for t in z]
print([str(t) for t in z])  # ['-1', '0', '2', '3/8']
print(back == values)  # True: the two maps undo each other exactly (with floats this can fail in the last digit)

# sigma = 0 is the case "multiply by 0" in disguise: a constant feature cannot be standardised, and the inverse does not exist
constant_feature = [Fraction(7)] * 4
sigma_zero = Fraction(0)
try:
    standardise(constant_feature[0], Fraction(7), sigma_zero)
except ZeroDivisionError as error:
    print("sigma = 0:", type(error).__name__)  # ZeroDivisionError: nothing can be divided by 0

# A learning rate is a scale factor in an inequality. Gradient descent on f(w) = (w - c)**2 steps  w <- w - eta * 2(w - c),
# so the distance to the minimiser is multiplied by (1 - 2*eta) at every step.
def step(w, c, eta):
    return w - eta * 2 * (w - c)


def contracts(eta):
    """|1 - 2*eta| < 1  <=>  -1 < 1 - 2*eta < 1  <=>  (subtract 1)  -2 < -2*eta < 0  <=>  (divide by -2 and REVERSE)  1 > eta > 0."""
    return 0 < eta < 1


c = Fraction(3)
for eta in (Fraction(1, 4), Fraction(1, 2), Fraction(3, 4), Fraction(-1, 10), Fraction(11, 10)):
    w = Fraction(0)
    for _ in range(3):
        w = step(w, c, eta)
    print(str(eta), str(abs(w - c)), contracts(eta))
# eta = 1/4 -> 3/8   (|1 - 1/2|**3 * 3), contracts
# eta = 1/2 -> 0     (a step of 1/2 lands on the minimiser), contracts
# eta = 3/4 -> 3/8   (|1 - 3/2|**3 * 3 = 3/8), contracts
# eta = -1/10 -> 648/125 = 5.184 : the sign of eta is wrong, every step moves AWAY from the minimiser (factor 6/5)
# eta = 11/10 -> 648/125 = 5.184 : too large, each step overshoots by more than it gained (factor -6/5)

Two observations about the first file. The inputs are turned into Fractions so that the answer is exact; Fraction("0.1") is one tenth, while the float 0.1 is a binary approximation of it. And the function returns a tuple whose first entry is one of "point", "none" or "all": the three kinds of solution set, made explicit instead of hidden in a float.

Explore: change the parameters

These questions have definite answers; check them against the stages.

  • On the balance stage, choose 5x−1≥2x+85x-1\ge2x+8. Which trial values of xx make the beam tilt toward the left pan, and does the verdict say the statement is true there?
  • On the balance stage, start from 3x+2=x+103x+2=x+10 and multiply both sides by −2-2 before doing anything else. Is the beam level at the same xx? Which trial value is it level at in the new statement?
  • On the balance stage, try to multiply by 00. What does the stage say, and where is that fact in the definition?
  • On the reversal stage, find all pairs p<qp<q with p,q∈{−2,−1,0,1,2}p,q\in\{-2,-1,0,1,2\} and k=−1k=-1 for which the products kpkp and kqkq are both positive.
  • On the extraneous stage, find every pp for which q=0q=0 gives an extraneous root. (Hint: it depends on whether DD is greater than 11.)
  • On the extraneous stage, find a pair (p,q)(p,q) with p<qp<q that has no real candidate, and one with p=qp=q where both roots are valid.
  • On the division stage, which (c,m)(c,m) make the line tangent to the parabola?

Common mistakes

Numerical considerations

The mathematics above is exact. A program that solves equations with doubles is not, and two problems appear even for tiny, harmless-looking equations.

Equality of the answer. Solve 0.1x+0.2=0.30.1x+0.2=0.3 by hand: subtract 0.20.2, divide by 0.10.1, and the answer is exactly 11. In doubles, (0.3 - 0.2) / 0.1 is 0.9999999999999998, and == 1 is False. The table solves several equations exactly with the model’s rational arithmetic, next to the double a program holds. A row marked “no” is a case where the program’s answer is a different number from the true one.

equationPythonexact xdouble (what the program holds)agree?
3*x + 2 = 1*x + 10(10 - 2) / (3 - 1)44yes
0.1*x + 0.2 = 0.3(0.3 - 0.2) / (0.1 - 0)10.9999999999999998⚠ no
0.1*x + 0.7 = 0.8(0.8 - 0.7) / (0.1 - 0)11.0000000000000009⚠ no
0.3*x + 0.1 = 0.7(0.7 - 0.1) / (0.3 - 0)22yes
0.1*x = 0.3(0.3 - 0) / (0.1 - 0)32.9999999999999996⚠ no
0.7*x + 0.1 = 0.8(0.8 - 0.1) / (0.7 - 0)11.0000000000000002⚠ no
1.1*x = 3.3(3.3 - 0) / (1.1 - 0)32.9999999999999996⚠ no

A coefficient that should vanish. The three-way answer depends on whether m=a1−a2m=a_1-a_2 is exactly 00. In doubles, (0.1 + 0.2) - 0.3 is not 00 but 2−54≈5.55⋅10−172^{-54}\approx5.55\cdot10^{-17}. The equation (0.1+0.2)x+1=0.3x+2(0.1+0.2)x+1=0.3x+2 has no solution (the xx terms cancel to 00 and leave 0=10=1), but computed in floats the program divides 11 by 2−542^{-54} and reports x≈1.8⋅1016x\approx1.8\cdot10^{16} with full confidence. The reference file above shows both calculations: the float one and the exact one that returns none.

Substitution with a tolerance. The extraneous-root test in the reference file compares math.sqrt(x + p) with x + q using math.isclose. That is adequate for the whole-number parameters of this family, where a valid root has a right side of at least 00 and an invalid one has a negative right side of at least about 0.60.6 in size. For other equations the tolerance decides the verdict, and a tolerance that is too wide accepts a false root while one that is too narrow rejects a true one. The exact test (a Fraction whose square root is itself a Fraction) cannot be fooled but works only when the root is rational.

Connections to engineering and machine learning

  • Undoing a transformation. Standardising a feature, z=x−μσz=\frac{x-\mu}{\sigma}, is an affine map, and getting back to the original scale is solving that equation for xx: multiply by σ\sigma (not 00) and add μ\mu. A feature with σ=0\sigma=0 is the “multiply by 00” case in disguise: the map cannot be undone, and a constant column cannot be standardised.
  • Learning rates are factors in an inequality. Gradient descent on f(w)=(w−c)2f(w)=(w-c)^{2} updates w←w−η⋅2(w−c)w\leftarrow w-\eta\cdot2(w-c), so the distance to the minimiser is multiplied by 1−2η1-2\eta at every step. The distance shrinks when ∣1−2η∣<1\lvert1-2\eta\rvert<1. Written as −1<1−2η<1-1<1-2\eta<1 and solved with the steps of this chapter, subtracting 11 gives −2<−2η<0-2<-2\eta<0, and dividing by −2-2 reverses both inequalities: 0<η<10<\eta<1. The condition η>0\eta>0 matters: a negative rate moves away from the minimum. The reference file runs three steps for several rates.
  • One-dimensional least squares. Fitting a single weight to xw=yxw=y gives w=y/xw=y/x, valid only for x≠0x\neq0. When x=0x=0 the equation says 0=y0=y: no solution if y≠0y\neq0, every ww if y=0y=0. The three cases of this chapter are the three behaviours of the linear system in one unknown, and they return in higher dimensions.
  • Thresholds on a loss. A stopping rule such as “stop when the loss is below ε\varepsilon” is an inequality in the parameters. Rearranging it by hand needs the same reversal rule whenever a negative coefficient is involved.
  • Domain guards in code. The division fallacy is the source of many runtime errors: a formula simplified by hand is fine for x≠2x\neq2 and a ZeroDivisionError at 22. Write the restriction down with the formula.
  • Looking ahead. In Functions the expression ax+bax+b becomes a rule with a domain, and an equation f(x)=yf(x)=y asks for the inputs that produce a given output.

Exercises

Answer each question and press Check. A wrong answer tells you which mistake it looks like. After one attempt you may open a worked solution.

calculation

Solve a linear equation

Solve 3x + 2 = x + 10. Enter the value of x. Check it by putting it back into BOTH sides.

prediction

What happens to the x?

Predict the solution set of 2x + 3 = 2x + 7 before you work on it.

Choose one answer

prediction

Dividing by a negative number

From −3x < 6, divide both sides by −3. What is the result?

Choose one answer

explanation

Why not multiply both sides by 0?

Why does the balance refuse "multiply both sides by 0"?

Choose one answer

calculation

The boundary of an inequality

The solutions of 5x − 1 ≥ 2x + 8 are all x ≥ b. Enter b.

calculation

Square, solve, then check

Solve √(x + 6) = x − 6. Squaring gives two candidates; enter the one that solves the ORIGINAL equation.

construction

Build an equation with an extraneous root

Choose whole numbers p and q, each from −6 to 6, so that √(x + p) = x + q has two candidates after squaring and exactly ONE of them is extraneous.

calculation

Do not divide by (x − 2)

Find ALL solutions of x(x − 2) = 3(x − 2). Enter the smaller solution and the larger one.

prediction

Expand 4(x − 3)

Which expression equals 4(x − 3) for every x?

Choose one answer

prediction

Which one is an identity?

An identity is an equation that is true for every x. Which of these is one?

Choose one answer

explanation

Which learning rates converge?

Gradient descent on f(w) = (w − c)² multiplies the distance to the minimiser by |1 − 2η| at every step. For which η does the distance shrink, |1 − 2η| < 1?

Choose one answer

Summary and checkpoints

  • An equation or inequality is a condition on an unknown; its solution set is the set of numbers that satisfy it. A linear equation has exactly one solution, none, or every number.
  • A balanced step (add the same thing to both sides, multiply or divide both sides by a non-zero number) is reversible, so it keeps the solution set. Multiplying by 00 is not reversible and is refused.
  • Multiplying or dividing by a negative number reverses an inequality, because it reflects the line through 00. Test a value to catch a missed reversal.
  • Squaring is not a balanced step: it can add roots (extraneous roots). Substitute each candidate into the original equation.
  • Dividing by an expression that can be 00 can lose a root. Move everything to one side and factor instead.
  • a(bx+c)=abx+aca(bx+c)=abx+ac is an identity; the slip abx+cabx+c gives a contradiction unless a=1a=1 or c=0c=0.
  • In code: keep coefficients exact, never compare a float answer with ==, and test whether a coefficient that should cancel is exactly 00.

Checkpoints. You are ready to move on when you can, without looking:

  1. Solve 3x+2=x+103x+2=x+10 and say what the beam does at the solution.
  2. Say what the solution set of 2x+3=2x+72x+3=2x+7 is, and why.
  3. Divide −3x<6-3x<6 by −3-3 and explain, with a number, why the relation turns around.
  4. Name the extraneous root of x+2=x\sqrt{x+2}=x and the reason it fails in the original equation.
  5. Say which solution of x(x−2)=3(x−2)x(x-2)=3(x-2) is lost by dividing by x−2x-2.

The map of the book

97 of 97 chapters written · eight Parts

demo Foundation demo Try the live vector example Open as a page

Part I

Mathematical Language

The notation and mental objects everything else uses.

9 of 9 chapters written

  1. I.1 Scalars, quantities, units, powers and roots available
  2. I.2 Coordinates and points available
  3. I.3 Vectors: components, magnitude, and direction reference chapter available
  4. I.4 Algebraic expressions, equations and inequalities available
  5. I.5 Functions as input → transformation → output available
  6. I.6 Essential function families available
  7. I.7 Sets, intervals, and relations available
  8. I.8 Indexed quantities and summation available
  9. I.9 The notation map for ML available

Part II

Linear Algebra

Matrices as transformations: the strongest foundation in the book.

19 of 19 chapters written

  1. II.1 Vector addition and subtraction available
  2. II.2 Scalar multiplication and vector scaling available
  3. II.3 Norms, magnitude, and normalization available
  4. II.4 Dot product, angle, and orthogonality available
  5. II.5 Projection and distance to a direction available
  6. II.6 Linear combinations and span available
  7. II.7 Linear independence, basis, and coordinates available
  8. II.8 Matrices as linear transformations available
  9. II.9 Matrix × vector available
  10. II.10 Matrix × matrix as composition available
  11. II.11 Transpose and orthogonality available
  12. II.12 Determinant: orientation and area/volume scaling available
  13. II.13 Linear systems as intersecting constraints available
  14. II.14 Gaussian elimination and row operations available
  15. II.15 Rank and invertibility available
  16. II.16 Inverse matrices available
  17. II.17 Eigenvalues and eigenvectors available
  18. II.18 Orthogonal bases and least-squares geometry available
  19. II.19 Singular Value Decomposition available

Part III

Calculus

Change, local approximation, and sensitivity made visible.

15 of 15 chapters written

  1. III.1 Limits through approach available
  2. III.2 Continuity and local behavior available
  3. III.3 Derivative as secant → tangent available
  4. III.4 Derivative as rate of change and local linear model available
  5. III.5 Derivative rules available
  6. III.6 Chain rule available
  7. III.7 Multivariable functions and partial derivatives available
  8. III.8 Gradient and directional derivatives available
  9. III.9 Jacobian available
  10. III.10 Computational graphs and automatic differentiation available
  11. III.11 Hessian and curvature available
  12. III.12 Integrals as accumulation available
  13. III.13 Fundamental derivative/integral connection available
  14. III.14 Numerical differentiation available
  15. III.15 Numerical integration available

Part IV

Probability

Uncertainty through sample spaces, evidence, and repetition.

16 of 16 chapters written

  1. IV.1 Random experiments, outcomes, and sample spaces available
  2. IV.2 Events and probability mass available
  3. IV.3 Counting and finite probability where needed available
  4. IV.4 Conditional probability as restricted sample space available
  5. IV.5 Independence available
  6. IV.6 Bayes' theorem as probability-mass update available
  7. IV.7 Random variables available
  8. IV.8 PMF, PDF, and CDF available
  9. IV.9 Bernoulli and Binomial distributions available
  10. IV.10 Uniform and Gaussian distributions available
  11. IV.11 Expectation available
  12. IV.12 Variance and standard deviation available
  13. IV.13 Joint, marginal, and conditional distributions available
  14. IV.14 Covariance and correlation available
  15. IV.15 Law of Large Numbers available
  16. IV.16 Central Limit Theorem available

Part V

Statistics

From observed data to estimates, intervals, and likelihood.

10 of 10 chapters written

  1. V.1 Data summaries: mean, median, mode available
  2. V.2 Spread: variance, standard deviation, robust interpretation available
  3. V.3 Population, sample, and sampling variability available
  4. V.4 Estimators and estimation available
  5. V.5 Confidence intervals through repeated sampling available
  6. V.6 Covariance matrices and multivariate spread available
  7. V.7 Regression fundamentals available
  8. V.8 Likelihood available
  9. V.9 Maximum Likelihood Estimation available
  10. V.10 Maximum A Posteriori estimation available

Part VI

Optimization

Learning as repeated parameter updates governed by an objective.

10 of 10 chapters written

  1. VI.1 Objective functions and optimization landscapes available
  2. VI.2 Local vs global optima and stationary points available
  3. VI.3 Convexity available
  4. VI.4 Gradient descent flagship chapter available
  5. VI.5 Learning rate and convergence behavior available
  6. VI.6 Momentum available
  7. VI.7 Stochastic and mini-batch gradient descent available
  8. VI.8 Adam intuition available
  9. VI.9 Constraints and projected/penalized intuition available
  10. VI.10 Numerical stopping and optimization diagnostics available

Part VII

Information Theory

Logarithms, entropy, and the loss behind token prediction.

8 of 8 chapters written

  1. VII.1 Logarithms and log probabilities in computation available
  2. VII.2 Self-information available
  3. VII.3 Entropy available
  4. VII.4 Cross entropy and negative log-likelihood available
  5. VII.5 KL divergence available
  6. VII.6 Logits and softmax available
  7. VII.7 Numerical stability: log-sum-exp and stable softmax available
  8. VII.8 From token logits to language-model loss available

Part VIII

Numerical Computing

Why equivalent expressions behave differently on real machines.

10 of 10 chapters written

  1. VIII.1 Floating-point representation available
  2. VIII.2 Rounding and representation error available
  3. VIII.3 Error propagation and conditioning available
  4. VIII.4 Catastrophic cancellation available
  5. VIII.5 Overflow and underflow available
  6. VIII.6 Stable algorithms available
  7. VIII.7 Numerical linear algebra realities available
  8. VIII.8 Sparse matrices and sparse structure available
  9. VIII.9 Vectorized computation available
  10. VIII.10 Reproducibility, random seeds, and nondeterminism available