EVERYTHING AIAI engineering, made visual
0/23 complete
LESSON 00 · MATHEMATICS × AI · FOUNDATION

Nothing here is hard.
It is the vocabulary.

Powers and logs, functions that chain, sums that grow, and the row-times-column move at the heart of every neural network — with a number line to check each step yourself.

90 MIN · 6 CHAPTERSPREREQ · SCHOOL ARITHMETIC
FIG. 00 / FOUR MOVES, ONE AT A TIME
ARITHMETIC 2 + 3 × 4 = 14 the move in focus what it combines with
LESSON 00TYPE · FOUNDATION~90 MINPREREQ · SCHOOL ARITHMETICORIGINAL LESSON ↗
THE 60-SECOND VERSIONThen show me the symbols ↓
01 / ORDER OF OPERATIONS

× and ÷ run before + and −.

The same three numbers can make two different answers; brackets decide which. Computers follow the contract exactly, so the order is a rule to write down, not a preference to argue about. And a minus sign in front of a power applies after it: −3² = −9.

2 + 3 × 4 = 14 · (2 + 3) × 4 = 20
02 / A FUNCTION IS A MACHINE

Input → rule → output, the same every time.

f(x) = x² turns 3 into 9 and never changes its mind. Chaining machines is composition — run g first, then feed its output to f — and swapping the order changes the answer. A neural network is just this, chained very deep.

f(g(2)) = 49 ≠ g(f(2)) = 13
03 / ROW × COLUMN

Every matrix entry is one dot product.

Lay one row of the left matrix on one column of the right: multiply the pairs, add them up. The inner dimensions must agree — (m × n) @ (n × p) = (m × p) — and the outer two survive. Nothing else in matrix multiplication is happening.

(2 × 3) @ (3 × 2) = (2 × 2)
MENTAL MODEL IN ONE SENTENCE

Every lesson ahead is built from four small moves: apply a rule, raise to a power, add things up, and lay a row on a column — nothing else is allowed to stay mysterious.

By the end you will read every symbol in this notebook aloud, move between powers and logs, expand a Σ into its terms, and compute a 2 × 2 matrix product by hand — the arithmetic every later lesson assumes.

NUMBERS & SYMBOLS

Math is compression.
Learn to read it out loud.

Each symbol replaces a sentence. Once you can say them aloud, formulas stop looking like a wall — and underneath every one of them is arithmetic you already know.

Start with the rule that trips up more people than anything else: operations have an order. Multiplication and division run before addition and subtraction, and brackets run before everything. 2 + 3 × 4 is 2 + 12 = 14, not (2 + 3) × 4 = 20. If you want 20, say so with brackets.

The other small trap is the minus sign. −3² means −(3²) = −9: the power happens first and the sign applies after. To square a negative, wrap it: (−3)² = 9. It is worth knowing this once, because a missing bracket in a loss function produces a wrong number, not an error message.

Brackets first (2 + 3) × 4 = 5 × 4 = 20 × and ÷ next 2 + 3 × 4 = 2 + 12 = 14 + and − last 10 − 4 ÷ 2 = 10 − 2 = 8 Powers before signs −3² = −(3·3) = −9 (−3)² = (−3)·(−3) = 9

The same expression, two orders

Compute the expression the maths way and the strict left-to-right way. Watch the two markers land in different places.

2 + 3 × 4 maths order: 1. 3 × 4 = 12 2. 2 + 12 = 14 answer: 14 strict left to right: 1. 2 + 3 = 5 2. 5 × 4 = 20 answer: 20 step through both orders and watch where they diverge

× and ÷ bind tighter than + and −, so they run first. Brackets run before everything. The slider lets you walk one operation at a time.

SymbolRead it aloud asExample
Σ (sigma)“add up all of these”Σᵢ₌₁³ aᵢ = a₁ + a₂ + a₃
Π (capital pi)“multiply all of these”Πᵢ pᵢ = p₁ · p₂ · p₃
aᵢ (subscript)“the i-th entry of a”a = [4, 7, 9] gives a₂ = 7
x², xⁿ“x squared”, “x to the n”2³ = 2·2·2 = 8
|a| or ‖a‖“the length of a”|[3, 4]| = 5
a · b“a dot b”a dot product (Lesson 1)
A @ B“A matmul B”matrix multiply (chapter 05)
Aᵀ“A transpose”rows and columns swapped
A⁻¹“A inverse”the matrix that undoes A
“approximately equals”π ≈ 3.14
“maps to” or “approaches”f: ℝⁿ → ℝᵐ takes n in, gives m out
argmax“the input that gives the biggest output”argmax of [0.1, 0.7, 0.2] is index 1
P(A|B)“probability of A given B”the bar means “given that B happened”
E[X]“expected value of X”the long-run average (Lesson 6)
e“Euler's number”, ≈ 2.71828exp(x) means eˣ
ln x, log x“natural log of x”the power you raise e to, in order to get x
Worked check: reading Σᵢ₌₁³ aᵢbᵢ out loud

The symbol says: let i walk from 1 to 3, and for each i multiply aᵢ by bᵢ, then add the results. With a = [4, 7, 9] and b = [2, 1, 3]:

Σᵢ₌₁³ aᵢbᵢ = a₁b₁ + a₂b₂ + a₃b₃ = 4·2 + 7·1 + 9·3 = 8 + 7 + 27 = 42 second example, so the pattern sticks: c = [1, 2, 2] and d = [3, 1, 4] Σᵢ₌₁³ cᵢdᵢ = 1·3 + 2·1 + 2·4 = 3 + 2 + 8 = 13

Notice there is no cleverness here: expand the symbol into its terms, compute each little product, add them up. That move — multiply pairs, then sum — is the dot product of Lesson 1 and the engine inside matrix multiplication in chapter 05.

Quick check

What does 2 + 3 × 4 equal?

FUNCTIONS AS MACHINES

A function is a machine:
input → rule → output.

Number in, number out, the same output every time for the same input. That is the whole definition — everything else in this notebook is machines feeding machines.

f(x) = x² turns 3 into 9. The letter f is the machine’s name, x is the slot you feed, and is the rule the machine applies. A machine can take several inputs at once: f(x, y) = x² + 3xy + y² turns the pair (1, 2) into 1 + 6 + 4 = 11.

Composition chains two machines: f(g(x)) means “run g first, then feed whatever comes out into f.” If g(x) = 3x + 1 and f(u) = u², then g(2) = 7 and f(7) = 49, so f(g(2)) = 49. Swap the order and you get a different machine: g(f(2)) = g(4) = 13. Order is part of the meaning, exactly as it will be for matrix multiplication in Lesson 3.

f(x) = x² f(3) = 3² = 9 f(x, y) = x² + 3xy + y² f(1, 2) = 1 + 6 + 4 = 11 g(x) = 3x + 1 f(u) = u² f(g(2)) = f(3·2 + 1) = f(7) = 7² = 49 ← g runs first g(f(2)) = g(2²) = g(4) = 3·4 + 1 = 13 ← f runs first same two machines, different order, different answer.

The function machine

Pick a rule, drag the input, and watch the output move along the curve. Tick “chain” to feed the output back in as the next input.

x = 1.5 f(x) = 3·1.5 + 1 = 5.5 chain is off: one pass through the machine

A function is a machine: one input, one rule, one output, the same output every time. Chaining two machines is composition — and the order you chain them in changes the result.

Worked check: one chain, two orders, with gentler numbers

A second example, small enough to do entirely in your head. Take g(x) = 2x + 3 and f(u) = u − 1, and feed in 4:

f(g(4)) = f(2·4 + 3) = f(11) = 11 − 1 = 10 g(f(4)) = g(4 − 1) = g(3) = 2·3 + 3 = 9 10 ≠ 9 — the order you chain machines in changes the result.

The inner machine always runs first. When in doubt, evaluate the brackets from the inside out, one number at a time, and write each intermediate down.

Quick check

With g(x) = 3x + 1 and f(u) = u², what is f(g(2))?

EXPONENTS & LOGS

Powers pack small numbers.
Logs unpack them.

An exponent counts repeated multiplication; a logarithm asks the reverse question. These two appear in every lesson from 4 onward, so they are worth getting right once.

means 2·2·2 = 8: the little number counts how many copies of the base are multiplied together. A logarithm runs the question backwards: “2 to what power gives 8?” The answer is 3, written log₂ 8 = 3.

In machine learning the base is almost always e ≈ 2.71828 — Euler’s number — and that logarithm is written ln or simply log. So when a paper says “log probability”, read “natural log”. The reason for e is calculus: is the one function whose slope equals its own value at every point (Lesson 4), which keeps derivatives clean.

2³ = 2·2·2 = 8 log₂ 8 = 3 “2 to what power gives 8?” 2⁻² = 1/2² = 0.25 log₂ 0.25 = −2 e ≈ 2.71828 ln e = 1 ln 1 = 0 ln and exp undo each other: ln(eˣ) = x e^(ln x) = x
Derivation: the log rules, and why they hold
  1. Exponents add when you multiply: eᵃ · eᵇ = eᵃ⁺ᵇ. Reason: eᵃ is a copies of e multiplied; eᵇ is b copies; together that is a + b copies.
  2. Now let x = eᵃ and y = eᵇ, so by definition ln x = a and ln y = b. Then x·y = eᵃ⁺ᵇ, so ln(x·y) = a + b = ln x + ln y. A product inside the log becomes a sum outside — this one rule is why Lessons 6 and 7 work in “log space”.
  3. Apply it n times: ln(xⁿ) = n · ln x.
  4. Division: x / y = x · y⁻¹, so ln(x / y) = ln x − ln y.
  5. Since e⁰ = 1, ln 1 = 0. Numbers between 0 and 1 need a negative power of e, so their logs are negative: ln 0.5 ≈ −0.69 and ln 0.01 ≈ −4.6. As x → 0, ln x → −∞, which is why “log of a probability” is always ≤ 0 and why a probability of exactly 0 is a disaster in code.
  6. ln and exp undo each other: ln(eˣ) = x and e^(ln x) = x.
Rules Numbers to convince yourself ln(xy) = ln x + ln y ln(2·4) = ln 8 = 2.079 ; ln 2 + ln 4 = 0.693 + 1.386 = 2.079 ✓ ln(xⁿ) = n·ln x ln(2³) = ln 8 = 2.079 ; 3·ln 2 = 3 · 0.693 = 2.079 ✓ eᵃ·eᵇ = eᵃ⁺ᵇ e¹·e² = 2.718 · 7.389 = 20.09 = e³ ✓ ln 1 = 0, ln e = 1 e⁰ = 1

2ˣ and its undo button

Move the exponent and watch the same pair of numbers appear on both sides: 2ˣ builds the value, log₂ takes it apart again.

a = 3 2^a = 2^3 = 8 log2(8) = 3 round trip: 2^(log2(8)) = 8 anchors: 2^0 = 1 log2(1) = 0 2^3 = 8 log2(8) = 3

A logarithm asks the reverse question: “2 to what power gives this number?” In machine learning the base is usually e ≈ 2.718, but the idea is exactly this one.

Second worked example: a three-word sentence. A model assigns probabilities 0.9, 0.8 and 0.7 to three words in a row. The probability of the whole sequence is the product 0.9 · 0.8 · 0.7 = 0.504. In log space: ln 0.9 ≈ −0.105, ln 0.8 ≈ −0.223, ln 0.7 ≈ −0.357, and the sum is −0.105 − 0.223 − 0.357 = −0.685. Check: e^(−0.685) ≈ 0.504 ✓. Same number, but a long sentence adds up instead of multiplying down towards zero — the sum never underflows.

Quick check

Given ln 2 ≈ 0.693, what is ln 8?

SUMS, LISTS & INDEXES

Σ says: add them all.
i says: which one.

A list of numbers is the most common object in all of machine learning. Two moves cover almost everything: operate entry by entry, and add up a sequence of entries.

An array (or list, or vector) is an ordered stack of numbers: a = [4, 7, 9]. The little letter below a symbol selects one entry — aᵢ is “the i-th entry of a”, so a₂ = 7. One warning worth internalising now: maths usually counts from 1, code always counts from 0. In the same list, maths calls the last entry a₃ = 9 while Python calls it a[2] = 9. When a sum runs off the end of a list in code, this is almost always why.

Element-wise operations move entry by entry: add matching positions, or stretch every entry by the same number. The length of a vector is Pythagoras run across its entries: |[3, 4]| = √(9 + 16) = 5. Dividing a vector by its length leaves a unit vector pointing the same way: [3, 4] / 5 = [0.6, 0.8]. Lesson 1 uses that constantly.

Add: [1, 2] + [3, 4] = [1+3, 2+4] = [4, 6] Scale: 2 · [1, 2] = [2·1, 2·2] = [2, 4] Combine: 2·[1, 0] + 3·[0, 1] = [2, 0] + [0, 3] = [2, 3] Length: |[3, 4]| = √(3² + 4²) = √25 = 5 |[1, 2, 2]| = √(1 + 4 + 4) = 3 Σᵢ₌₁⁴ i = 1 + 2 + 3 + 4 = 10 Σᵢ₌₁³ i² = 1 + 4 + 9 = 14 Πᵢ₌₁³ pᵢ = p₁ · p₂ · p₃ (multiply instead of add)

Σ, one term at a time

The index i walks 1, 2, 3 … and the running total grows by one bar per step. Press play or drag the stop-at slider.

Σᵢ₌₁ⁿ i (i walks 1 → 5) terms: 1 + 2 + 3 + 4 + 5 summed so far: nothing yet — the total starts at 0

The index is the counter inside the Σ. The note under it says where the counter starts; the letter on top says where it stops. In machine-learning code, sums like this run over examples.

Worked check: a dot product, read slowly

The dot product is the most-used sum in the notebook. It multiplies matching entries and adds the results. Read it twice with real numbers so the symbol has no mystery left:

a = [4, 7, 9] b = [2, 1, 3] a · b = Σᵢ aᵢbᵢ = 4·2 + 7·1 + 9·3 = 8 + 7 + 27 = 42 length of a: |a| = √(4² + 7² + 9²) = √(16 + 49 + 81) = √146 ≈ 12.08 unit vector: a / |a| ≈ [0.331, 0.579, 0.745]

The code version of the same sum is one line: sum(ai*bi for ai, bi in zip(a, b)). The Σ is the same instruction; the loop is just written down in symbols.

Python you will readPlain EnglishExample
x = [4, 7, 9]a list, in order. Python counts positions from 0: x[0] = 4, x[2] = 9len(x) = 3
[f(x) for x in xs]a new list made by applying f to each item[x*2 for x in [1,2,3]] → [2, 4, 6]
zip(a, b)walk both lists side by side, in pairslist(zip([1,2],[3,4])) → [(1,3), (2,4)]
sum(a*b for a,b in zip(x,y))multiply the pairs, then add: a dot productx=[1,2], y=[3,4] → 1·3 + 2·4 = 11
A @ B vs A * Bmatrix multiply vs element-wise multiply in NumPythe silent bug of Lesson 2

Π is the multiplication sibling of Σ. It shows up when events do not influence each other: the probability of two heads in a row is ½ · ½ = ¼, a Π over two coin flips. Lesson 6 builds everything from that one fact.

Quick check

What is Σᵢ₌₁³ i²?

MULTIPLY MATRICES

Rows meet columns.
Multiply pairs, then add.

A matrix is a grid of numbers. Multiplying two of them sounds intimidating and is not: every entry of the answer is one row laid on one column, exactly the multiply-and-add you already do.

A matrix is a rectangular grid of numbers. Its shape is written rows × columns, always rows first: a 2 × 3 matrix has 2 rows and 3 columns. The entry in row i, column j is written A[i][j] or aᵢⱼ. A vector with n entries is a matrix with one column: n × 1.

The rule. To compute entry (i, j) of A @ B, take row i of A and column j of B, multiply the pairs position by position, and add. For that to work, the row and the column must be the same length — which is exactly the shape rule (m × n) @ (n × p) = (m × p). The inner dimensions must match and are consumed; the outer dimensions survive.

A = | 1 2 3 | shape 2 × 3 A[0][2] = 3 (row 0, column 2) | 4 5 6 | Worked 2 × 2 example: A = | 1 2 | B = | 5 6 | | 3 4 | | 7 8 | C[0][0] = row 0 of A · col 0 of B = 1·5 + 2·7 = 5 + 14 = 19 C[0][1] = row 0 of A · col 1 of B = 1·6 + 2·8 = 6 + 16 = 22 C[1][0] = row 1 of A · col 0 of B = 3·5 + 4·7 = 15 + 28 = 43 C[1][1] = row 1 of A · col 1 of B = 3·6 + 4·8 = 18 + 32 = 50 A @ B = | 19 22 | shapes: (2×2) @ (2×2) = (2×2) | 43 50 |

Matrix multiply trainer

Fill in all four entries of A @ B. Click a cell to light up the row of A and column of B it comes from, then reveal the arithmetic.

A =
2
1
3
1
4
0
@B =
1
2
3
1
0
4
=C =

Shapes: (2 × 3) @ (3 × 2) = (2 × 2). The shared 3 is consumed by the multiply-and-add; the outer 2 and 2 survive.

result[0][0] uses row 1 of A = [2, 1, 3] and column 1 of B = [1, 3, 0] fill the cell, then reveal the working when you want to see the answer 0 / 4 cells correct

Every entry is one row laid on one column: multiply the pairs, then add. That is the whole rule — and it is exactly what a neural network layer does, with bigger numbers.

Why this rule — and the second view of the same product

Why not multiply matching cells? Because a matrix is meant to act on a vector, and the action is a weighted sum. Look at A @ [x, y]: row 0 gives 1·x + 2·y and row 1 gives 3·x + 4·y. Each output is a weighted sum of the inputs, with the weights coming from one row. That is precisely what a neuron computes in Lesson 2 — so this is the multiplication that makes matrices useful. Multiplying matching cells is a different, also useful, operation with a different symbol.

Second view: columns combine. The same product reads as a mix of A’s columns, with the input saying how much of each: A @ [x, y] = x · [1, 3] + y · [2, 4]. Numeric check with x = 2, y = 1:

row view: A @ [2, 1] = [1·2 + 2·1, 3·2 + 4·1] = [4, 10] column view: 2·[1, 3] + 1·[2, 4] = [2, 6] + [2, 4] = [4, 10] ✓ same vector, different picture — Lesson 3 lives in this view.

Second worked example, a non-square product. Multiply a 2 × 3 by a 3 × 2:

A = | 2 1 3 | B = | 1 2 | | 1 4 0 | | 3 1 | | 0 4 | C[0][0] = 2·1 + 1·3 + 3·0 = 2 + 3 + 0 = 5 C[0][1] = 2·2 + 1·1 + 3·4 = 4 + 1 + 12 = 17 C[1][0] = 1·1 + 4·3 + 0·0 = 1 + 12 + 0 = 13 C[1][1] = 1·2 + 4·1 + 0·4 = 2 + 4 + 0 = 6 A @ B = | 5 17 | shapes: (2×3) @ (3×2) = (2×2) | 13 6 | the inner 3 was used up by the multiply-and-add element-wise is a different operation (identical shapes required): | 1 2 | * | 5 6 | = | 5 12 | matching cells only | 3 4 | | 7 8 | | 21 32 |
Quick check

For A = [[1, 2], [3, 4]] and B = [[5, 6], [7, 8]], what is the top-left entry of A @ B?

CHECK YOURSELF

Five questions.
Then the terms worth keeping.

Answer before you look. These test the exact moves the rest of the notebook assumes — order of operations, logs, composition, sums and one matrix entry.

0 / 5 answered · 0 correct

01What is 2 + 3 × 4?

02Given ln 2 ≈ 0.693, how else can you write ln 8?

03With g(x) = 3x + 1 and f(u) = u², what is f(g(2))?

04What is Σᵢ₌₁³ i²?

05For A = [[1, 2], [3, 4]] and B = [[5, 6], [7, 8]], what is the top-left entry of A @ B?

Key terms, demystified

Click a card to swap the lazy description for what it actually means.

Exercises from the lesson

Four short problems, all doable on paper. Try first; a worked answer is one click away.

  1. Compute 2 + 3 × 4, (2 + 3) × 4, and 10 − 4 ÷ 2 without a calculator.
    Show one worked answer

    Multiplication and division run before addition and subtraction. 2 + 3 × 4: 3 × 4 = 12, then 2 + 12 = 14. (2 + 3) × 4: brackets first, 2 + 3 = 5, then 5 × 4 = 20 — same three numbers, different order, different answer. 10 − 4 ÷ 2: 4 ÷ 2 = 2, then 10 − 2 = 8. Reading left to right would have given (10 − 4) ÷ 2 = 3, which is why the order is a rule and not a suggestion.

  2. Using ln 2 ≈ 0.693, compute ln 8 and ln 0.25 by hand. Check each answer by estimating the power of e.
    Show one worked answer

    ln 8 = ln(2³) = 3 · ln 2 ≈ 3 × 0.693 = 2.079, and e²·⁰⁷⁹ ≈ 8 ✓. For 0.25: 0.25 = 1/4 = 2⁻², so ln 0.25 = −2 · ln 2 ≈ −1.386, and e⁻¹·³⁸⁶ ≈ 0.25 ✓. Numbers below 1 always have negative logs, which is why a probability of exactly 0 gives log(0) = −∞ and breaks code.

  3. Compute Σᵢ₌₁⁴ (2i + 1) and Σᵢ₌₁⁴ i² by expanding the terms.
    Show one worked answer

    For 2i + 1 the index walks 1, 2, 3, 4: terms 2·1+1 = 3, 2·2+1 = 5, 2·3+1 = 7, 2·4+1 = 9, so the sum is 3 + 5 + 7 + 9 = 24. For i²: 1² + 2² + 3² + 4² = 1 + 4 + 9 + 16 = 30. Expand first, add second — the Σ is only shorthand for that instruction.

  4. Compute A @ B and B @ A for A = [[1, 2], [3, 4]] and B = [[5, 6], [7, 8]]. Are they equal?
    Show one worked answer

    A @ B: top-left 1·5 + 2·7 = 19; top-right 1·6 + 2·8 = 22; bottom-left 3·5 + 4·7 = 43; bottom-right 3·6 + 4·8 = 50. So A @ B = [[19, 22], [43, 50]]. B @ A: 5·1 + 6·3 = 23; 5·2 + 6·4 = 34; 7·1 + 8·3 = 31; 7·2 + 8·4 = 46, giving [[23, 34], [31, 46]]. Not equal: matrix multiplication is not commutative, so the order you multiply in is part of the meaning.

Terms this lesson borrows from later lessons (or outside)

You do not need to master these here. Each one gets a proper treatment in its own lesson; the one-line meaning is enough to keep reading. Orange dotted underlines in the prose point back to this list.

  • chain ruleHow to differentiate a function of a function: multiply the rate of change of each link. If L depends on y and y depends on x, then dL/dx = (dL/dy)·(dy/dx). (Lesson 4)
  • derivativeThe slope of a function at a point: how much the output changes per tiny change of the input. (Lesson 4)
  • partial derivativeThe derivative with respect to one input while every other input is held fixed. (Lesson 4)
  • weightsThe adjustable numbers inside a model. In a dense layer they are the entries of the matrix W (and the bias b); training changes them. (Lesson 2)
  • neural networkA model built from layers. Each layer multiplies its input by a matrix, adds a bias, and applies a simple non-linear function such as ReLU. (Lesson 2)
  • layerOne stage of a neural network: output = activation(W @ x + b). (Lesson 2)
  • neuronOne unit of a layer: a weighted sum of its inputs plus a bias, passed through an activation function. (Lesson 5)
  • expected valueThe probability-weighted average of a random quantity; the long-run mean. (Lesson 6)
  • prior / posteriorBayesian inference: start with a belief (the prior), see evidence, and compute an updated belief (the posterior). (Lesson 7)
  • eigenAn eigenvector is a direction a matrix only stretches, never turns; the eigenvalue is the stretch factor. (Lesson 3)
  • PyTorchA deep learning framework from Meta: arrays (tensors) with automatic differentiation built in.
  • NumPyNumerical Python: the standard array library, fast because its loops run in compiled C.
  • GPUGraphics Processing Unit: a chip with thousands of small cores that multiply matrices in parallel.
KEEP GOING

A picture is a start.
Practice is the rest.

This lesson is a port of an open course. Everything here traces back to it — and the next step is running the code yourself.

Lesson text adapted from the Math Foundations Notebook's Ground Zero section. The five quiz questions, the worked exercise answers, the second worked examples, and the number-line, function-machine, exponent-log, summation and matrix-trainer labs are original to this page. Every lab runs in your browser.