Mathematical Foundations

I.2 · Mathematical Language

How do two numbers pin down a place?

A coordinate is a signed distance read from an agreed zero. We build the number line, then the plane from two of them, and learn what a point's coordinates, quadrant, displacement and distance each say, and where screens and arrays quietly turn the picture upside down.

A point and its coordinates: P=(x, y)P=(x,\,y)

Try a point

A coordinate plane from minus 7 to 7 on both axes. The point P is at minus 3 comma 2, quadrant II (highlighted). Dashed segments run from the point straight to each axis: the vertical one has length |y| and ends on the x-axis, the horizontal one has length |x| and ends on the y-axis. The distance from P to the origin is the square root of 13.

x-coordinate (signed)
−3
y-coordinate (signed)
2
quadrant
II
distance to the y-axis
3
distance to the x-axis
2
distance to the origin
√133.605551…

= exact · ≈ rounded to 6 decimals · the distance to the x-axis is |y|, to the y-axis |x|

(−3, 2) is in quadrant II: x < 0 and y > 0. The distance to the x-axis is |y| = 2 and to the y-axis is |x| = 3: the coordinate you read along one axis is the distance to the OTHER axis.

P=(x, y)=(−3, 2)x<0, y>0 ⇒ quadrant II\begin{aligned}P&=(x,\,y)=\left(-3,\ 2\right)\\&x<0,\ y>0\ \Rightarrow\ \text{quadrant II}\end{aligned}
∣x∣=3,∣y∣=2∣OP∣=x2+y2=(−3)2+22=9+4=13=13≈3.605551…\begin{aligned}\lvert x\rvert&=3,\qquad \lvert y\rvert=2\\\lvert OP\rvert&=\sqrt{x^{2}+y^{2}}=\sqrt{(-3)^{2}+2^{2}}\\&=\sqrt{9+4}=\sqrt{13}\\&=\sqrt{13}\\&\approx 3.605551\ldots\end{aligned}

from −7 to 7, up to 2 decimal places

from −7 to 7, up to 2 decimal places

Snap to

A point P in the plane. Drag it, use the arrow keys on it, or type its two coordinates. The shaded region is its quadrant; the dashed legs are its distances to the two axes; the solid segment is its distance to the origin. Move it onto an axis and watch the quadrant disappear.

Try it before reading on. Drag PP into each of the four corners and watch the signs of xx and yy. Then drag it onto an axis, and read what the stage says about its quadrant. Then compare the two distances to the axes with the two coordinates: which coordinate is the distance to which axis? By the end of the chapter you will know how a pair of numbers fixes a place, why a point on an axis is in no quadrant, what the distance between two points is and why Pythagoras supplies it, and why the row of a pixel in an image is not its yy-coordinate.

Intuition: reading a place from two rulers

Finding a house on a street needs one number: how far along the street. A street is a line, and one signed number fixes a place on a line. Finding a seat in a theatre needs two: which row and which seat in the row. A plane has two independent directions, so a place in a plane needs two numbers, one for each.

A coordinate system is the agreement that makes those numbers mean something: where zero is, which way is positive, and what one step is. Change the agreement and the same place gets different numbers, so the numbers are never the place itself. They are a reading of the place against rulers that somebody chose. Most of the mistakes in this chapter come from forgetting one of the choices, usually which way an axis points.

The quantities of the previous chapter are what the coordinates measure: each coordinate is a signed length, and its unit travels with it. The squares and roots of that chapter return here in one place only, the distance between two points.

Definition: number line, plane and point

Number line. Choose a point OO on a straight line, a direction to call positive, and a unit of length. The coordinate of a point XX on the line is the number xx whose absolute value is the distance from OO to XX, with the sign ++ if XX is on the positive side of OO and −- if it is on the other. Each point has exactly one coordinate and each real number is the coordinate of exactly one point. The point OO has coordinate 00.

Displacement and distance on the line. For two points with coordinates aa and bb, the displacement from aa to bb is the signed number

b−a,b-a,

positive when bb is to the right of aa, negative when it is to the left, and zero when they are the same point. The distance is its absolute value, ∣b−a∣\lvert b-a\rvert. The displacement has a direction and the distance does not: a−b=−(b−a)a-b=-(b-a), while ∣a−b∣=∣b−a∣\lvert a-b\rvert=\lvert b-a\rvert.

Cartesian plane. Take two number lines in the plane that cross at their zero points at a right angle. The first, the xx-axis, is drawn horizontally with positive to the right. The second, the yy-axis, is drawn vertically with positive upward. The crossing point is the origin O=(0,0)O=(0,0). A point PP has the coordinates (x,y)(x,y) when xx is the coordinate on the xx-axis of the foot of the perpendicular from PP to that axis, and yy is the same for the yy-axis. The pair is ordered: (2,5)(2,5) and (5,2)(5,2) are different points.

Quadrants. The axes cut the plane into four open regions, numbered counter-clockwise from the top right:

QuadrantSign of xxSign of yy
I++++
II−-++
III−-−-
IV++−-

The word open matters. A point is in a quadrant only when both coordinates are non-zero and have the signs in the table. A point with x=0x=0 or y=0y=0 lies on an axis. It is a border between two quadrants and belongs to neither. The origin is on both axes and belongs to no quadrant.

Distance in the plane. The distance between A=(a1,a2)A=(a_1,a_2) and B=(b1,b2)B=(b_1,b_2) is

∣AB∣=(b1−a1)2+(b2−a2)2.\lvert AB\rvert=\sqrt{(b_1-a_1)^{2}+(b_2-a_2)^{2}}.

The next sections explain where this comes from. For the origin it reads ∣OP∣=x2+y2\lvert OP\rvert=\sqrt{x^{2}+y^{2}}.

The number line: one number, a direction, a distance

Everything in the plane is built from the line, so begin there. The stage shows two points aa and bb on one line. The step from aa to bb is drawn below the line as a signed arrow, and the distance is drawn above it as a measurement.

Two points on a line: b−ab-a and ∣b−a∣\lvert b-a\rvert

Try a pair (a → b)

A number line from minus 8 to 8. Point a is at minus 3 and point b is at 5. Point a is to the left of point b. The signed step from a to b is 8, and the distance between them is 8.

point a
−3
point b
5
signed step from a to b
8
signed step from b to a
−8
distance (no direction)
8
midpoint
1
distance through 0
8

b − a has a sign (a direction); |b − a| does not. |a| + |b| is a distance only through 0.

b is to the right of a, so b − a = 8 (the sign says which way) while a − b = −8: the same two points, the opposite direction. The distance |b − a| = 8 has no direction, so it is the same both ways. |a| + |b| = 8 happens to equal the distance, because 0 is between the points (or is one of them).

b−a=5−(−3)=8a−b=(−3)−5=−8=−(b−a)\begin{aligned}b-a&=5-(-3)=8\\a-b&=(-3)-5=-8=-(b-a)\end{aligned}
∣b−a∣=∣8∣=8=∣a−b∣a+b2=−3+52=1\begin{aligned}\lvert b-a\rvert&=\lvert 8\rvert=8=\lvert a-b\rvert\\\tfrac{a+b}{2}&=\tfrac{-3+5}{2}=1\end{aligned}

from −7 to 7, up to 2 decimal places

from −7 to 7, up to 2 decimal places

Snap to

Two points on a number line. Drag a and b, use the arrow keys, or type them. The arrow below the line is the signed step b − a; the measurement above it is the distance |b − a|. When a and b coincide there is no step and the distance is 0.

Swap the two points and watch what changes. The arrow flips direction and the signed step changes sign. The measurement above does not change at all. That is the whole difference between a displacement and a distance, and it is worth saying again in words: a displacement answers “how do I get from here to there?”, and so depends on which is which. A distance answers “how far apart are they?”, and does not.

The stage also shows the midpoint a+b2\tfrac{a+b}{2}. It is the point exactly half-way, since it is aa plus half the displacement: a+12(b−a)=a+b2a+\tfrac12(b-a)=\tfrac{a+b}{2}. And it shows a tempting wrong answer. If aa and bb are on opposite sides of 00, then ∣a∣+∣b∣\lvert a\rvert+\lvert b\rvert is the distance, because the path passes through 00. If they are on the same side, it is not: ∣3∣+∣5∣=8\lvert 3\rvert+\lvert 5\rvert=8, but the points 33 and 55 are 22 apart. The sum of absolute values is a distance via zero, and the stage marks it as such.

The cleanest rule for the distance on a line is the one in the definition, ∣b−a∣\lvert b-a\rvert, because it never needs to ask which side of 00 anyone is on.

The plane: two lines at right angles

Now put a second line through OO, perpendicular to the first. The hero stage is that picture. A point P=(x,y)P=(x,y) is reached from the origin by walking xx along the first axis, then yy in the perpendicular direction. The two dashed legs on the stage are those two walks, read backwards: from PP straight down (or up) to the xx-axis, and straight across to the yy-axis.

Look at what the legs measure. The vertical leg ends on the xx-axis and has length ∣y∣\lvert y\rvert. The horizontal leg ends on the yy-axis and has length ∣x∣\lvert x\rvert. So the distance from PP to the xx-axis is ∣y∣\lvert y\rvert and the distance to the yy-axis is ∣x∣\lvert x\rvert. Each distance uses the other coordinate. This is the single most common slip in this topic, and the stage’s verdict line repeats it deliberately.

Quadrants and the axes

The quadrant of a point is decided by the two signs and nothing else. The point (−3,2)(-3,2) has x<0x<0 and y>0y>0, so it is in quadrant II. The point (−0.1, 100)(-0.1,\,100) is in quadrant II too: the size of a coordinate has no say in the quadrant, only its sign.

An axis point needs separate care, because “not positive” and “negative” are different things. For (0,−4)(0,-4), xx is neither positive nor negative. It is zero, the border. The point is on the yy-axis and is in neither quadrant III nor quadrant IV. A program that writes if x > 0 and y > 0: ... elif ... else: quadrant 4 assigns (0,−4)(0,-4) and even (0,0)(0,0) to quadrant IV. The reference code below runs that mistake next to the correct version.

Use the presets on the stage to visit the three kinds of position: a quadrant, a half-axis, and the origin. In each case the verdict line says which, and which quadrants an axis separates.

The distance formula is Pythagoras

Take two points A=(a1,a2)A=(a_1,a_2) and B=(b1,b2)B=(b_1,b_2). Draw a horizontal line through AA and a vertical line through BB; they meet at the corner C=(b1,a2)C=(b_1,a_2). Then ACAC is horizontal with length ∣b1−a1∣\lvert b_1-a_1\rvert, CBCB is vertical with length ∣b2−a2∣\lvert b_2-a_2\rvert, and the angle at CC is a right angle, because the axes were chosen perpendicular. In the right triangle ACBACB the Pythagorean theorem gives

∣AB∣2=∣AC∣2+∣CB∣2=(b1−a1)2+(b2−a2)2.\lvert AB\rvert^{2}=\lvert AC\rvert^{2}+\lvert CB\rvert^{2}=(b_1-a_1)^{2}+(b_2-a_2)^{2}.

Squares make the absolute values unnecessary, since ∣t∣2=t2\lvert t\rvert^{2}=t^{2}. Taking the non-negative root, which is what   \sqrt{\ \ } means, gives the distance formula above.

This is a proof only because the theorem is taken as known. In coordinate geometry it is also used as the definition of distance: when a plane is defined as the set of pairs of numbers, distance is declared to be this formula, and the theorem about right triangles becomes a statement that the formula agrees with the geometry. For the hero point and the origin the same triangle has legs ∣x∣\lvert x\rvert and ∣y∣\lvert y\rvert.

The stage below carries the pieces: the displacement (Δx,Δy)=(b1−a1, b2−a2)(\Delta x,\Delta y)=(b_1-a_1,\,b_2-a_2), the right-angled corner, the exact squared distance, and the distance itself in its simplest exact form.

Two points: displacement, distance, midpoint

Try a pair (A → B)

A coordinate plane from minus 7 to 7 on both axes. Point A is at minus 3 comma 1 and point B is at 3 comma minus 3. The displacement from A to B is 6 comma minus 4. Dashed legs run horizontally and then vertically between the points and form a right angle at the corner. The distance from A to B is 2 times the square root of 13. The midpoint is 0 comma minus 1.

point A
(−3, 1)
point B
(3, −3)
displacement from A to B
(6, −4)
displacement from B to A
(−6, 4)
distance (no direction)
2√137.211103…
midpoint
(0, −1)
taxicab distance
10

The displacement B − A has a direction; the distance |AB| does not. Taxicab = |dx| + |dy|.

The displacement from A to B is (6, −4); from B to A it is (−6, 4): reversing the direction flips both signs. The distance 2√13 ≈ 7.211103 is the same either way. The taxicab distance 10 is larger because the path along the axes is not straight (it is equal only when dx or dy is 0).

B−A=(xB−xA, yB−yA)=(3−(−3), −3−1)=(6, −4)A−B=(−6, 4)=−(B−A)\begin{aligned}B-A&=\left(x_B-x_A,\ y_B-y_A\right)\\&=\left(3-(-3),\ -3-1\right)=\left(6,\ -4\right)\\A-B&=\left(-6,\ 4\right)=-(B-A)\end{aligned}
∣AB∣=dx2+dy2=62+(−4)2=36+16=52=213≈7.211103…\begin{aligned}\lvert AB\rvert&=\sqrt{dx^{2}+dy^{2}}=\sqrt{6^{2}+(-4)^{2}}\\&=\sqrt{36+16}=\sqrt{52}\\&=2\sqrt{13}\\&\approx 7.211103\ldots\end{aligned}
M=(xA+xB2, yA+yB2)=(0, −1)∣dx∣+∣dy∣=10 > ∣AB∣\begin{aligned}M&=\left(\tfrac{x_A+x_B}{2},\ \tfrac{y_A+y_B}{2}\right)=\left(0,\ -1\right)\\\lvert dx\rvert+\lvert dy\rvert&=10\ >\ \lvert AB\rvert\end{aligned}

from −7 to 7, up to 2 decimal places

from −7 to 7, up to 2 decimal places

from −7 to 7, up to 2 decimal places

from −7 to 7, up to 2 decimal places

Snap to

Two points A and B. Drag either one, use the arrow keys, or type the four coordinates. The legs show the components of the displacement, the corner marks the right angle, and the measurement along AB is the distance. M is the midpoint.

Four things to try on it.

  • Reverse the direction. Swap AA and BB (or drag one through the other). The displacement B−AB-A turns into A−BA-B and both components change sign. The distance is unchanged, since each component is squared.
  • Make a leg vanish. Put BB directly above AA. The segment is vertical, Δx=0\Delta x=0, and the formula collapses to Δy2=∣Δy∣\sqrt{\Delta y^{2}}=\lvert\Delta y\rvert, which is the number-line distance again. A horizontal segment does the same with Δx\Delta x. The plane distance contains the line distance as the special case.
  • Make the points coincide. Then both legs are 00, the distance is 00 and there is no direction. The stage reports this case in its own words: a zero displacement has no direction.
  • Read the exact form. Take A=(−3,1)A=(-3,1) and B=(3,−3)B=(3,-3). Then Δ=(6,−4)\Delta=(6,-4), Δx2+Δy2=36+16=52\Delta x^{2}+\Delta y^{2}=36+16=52, and 52=213≈7.211103\sqrt{52}=2\sqrt{13}\approx 7.211103. The exact form is 2132\sqrt{13}. The decimal is a rounding, and the book writes ≈\approx for it, as in the previous chapter. Only when the squared distance is a perfect square, as in the 33-44-55 triangle, is the distance a whole number.

Midpoint, and a second kind of distance

The midpoint of the segment ABAB averages each coordinate separately:

M=(a1+b12, a2+b22)=A+12 (B−A).M=\Bigl(\tfrac{a_1+b_1}{2},\ \tfrac{a_2+b_2}{2}\Bigr)=A+\tfrac12\,(B-A).

The second form says: start at AA and take half of the displacement.

Walking from AA to BB along the grid lines (horizontally, then vertically) covers ∣Δx∣+∣Δy∣\lvert\Delta x\rvert+\lvert\Delta y\rvert. This is the taxicab distance, after a driver who must keep to the streets. It is never shorter than the straight line, and it is equal to it exactly when the segment is horizontal, vertical, or has zero length. The reason is one line: (∣Δx∣+∣Δy∣)2=Δx2+Δy2+2∣Δx∣∣Δy∣(\lvert\Delta x\rvert+\lvert\Delta y\rvert)^{2}=\Delta x^{2}+\Delta y^{2}+2\lvert\Delta x\rvert\lvert\Delta y\rvert, and the extra term is zero only if a leg is zero. The stage shows both numbers, and which one is the straight-line distance.

Screen rows are not the yy-axis

A mathematical plane has yy growing upward. A picture stored in a computer does not. In an image array, img[row, col], row 00 is the top row and rows are counted downward; screen coordinates in nearly every graphics system do the same. A point (x,y)(x,y) in the mathematical picture therefore sits in the array at

col=x,row=H−1−y,\text{col}=x,\qquad \text{row}=H-1-y,

where HH is the number of rows. The pair is swapped (row first), the vertical direction is flipped, and the −1-1 appears because the first row is numbered 00 and not 11. The formula undoes itself: y=H−1−rowy=H-1-\text{row}.

This stage is limited, on purpose, to integer lattice points: the pixel (x,y)(x,y) is the cell whose indices are the integers xx and yy, with the point (0,0)(0,0) at the bottom left. Other conventions exist (pixel centres at half-integers, the origin at the top left) and change the formula by a constant or a sign. The stage models one convention and states it.

The same pixel, two coordinate systems

Try a pixel

The pixel grid of an image 5 wide and 4 tall drawn as a coordinate plane with y up. The selected pixel is at x = 3, y = 1, which is column 3 and row 2 counted from the top. The bottom row is y = 0.

The same image as an array: img[row, col], row 0 at the top
row, columncol 0col 1col 2col 3col 4
row 0(0,3)(1,3)(2,3)(3,3)(4,3)
row 1(0,2)(1,2)(2,2)(3,2)(4,2)
row 2(0,1)(1,1)(2,1)(3,1)(4,1)
row 3(0,0)(1,0)(2,0)(3,0)(4,0)
Cells show
x (column from the left)
3
y (from the bottom)
1
row = H − 1 − y
2
column = x
3
flat index = row · W + col
13
slip: row = H − y
3a legal row number, but the wrong pixel (one row too low)
slip: img[x, y]
row 3, col 1a legal index, and a different pixel (a silent bug)

row = H − 1 − y · col = x · index = row · W + col · the two slips are what NOT to write

The image is 5 pixels wide and 4 tall. The point (x, y) = (3, 1) is in column 3, and row 2 counted from the TOP, so it is img[2, 3], flat index 2 · 5 + 3 = 13. The slip H − y = 3 would read the pixel one row lower.

col=x=3row=H−1−y=4−1−1=2\begin{aligned}\mathrm{col}&=x=3\\\mathrm{row}&=H-1-y=4-1-1=2\end{aligned}
index=row⋅W+col=2⋅5+3=13y=H−1−row=4−1−2=1\begin{aligned}\mathrm{index}&=\mathrm{row}\cdot W+\mathrm{col}=2\cdot 5+3=13\\y&=H-1-\mathrm{row}=4-1-2=1\end{aligned}

from 2 to 10, whole numbers only

from 2 to 10, whole numbers only

from 0 to 4, whole numbers only

from 0 to 3, whole numbers only

A grid of pixels, drawn the mathematical way (y up) and stored the array way (row 0 on top). Choose the size, then pick a cell by dragging the point or typing x and y. The table highlights the same cell, and the two slip rows in the inspector show what the wrong formulas would have returned.

The two classic slips are both shown. Using H - y as the row is off by one: for y=0y=0 it points at row HH, which does not exist, and Python raises an IndexError. Writing img[x, y] in place of img[row, col] is worse when the image is square or the indices happen to be in range, because it raises no error and returns a different pixel. A bug that crashes is a gift; a bug that returns the wrong pixel is not.

Worked examples

A distance calculation is five steps, and it pays to see them as steps rather than a trick:

  1. Subtract each pair of coordinates to get the displacement (Δx,Δy)(\Delta x,\Delta y), in the direction from AA to BB.
  2. Square each component. The sign disappears here.
  3. Add the two squares. This is the squared distance, and it is exact whenever the coordinates are.
  4. Root it, using the exact method of the previous chapter: factor out square factors to reach the simplest radical form.
  5. Check by squaring the answer, and by reversing the direction: the displacement changes sign, the distance does not.

Step through four cases. The first is the 33-44-55 triangle. The second is the 2132\sqrt{13} case from above. The third has coordinates with halves, so the arithmetic is on fractions and the root of a fraction is needed. The last starts in quadrant III and ends in quadrant I: the first point has negative coordinates, yet both components of the displacement are positive, because the displacement is a difference.

Worked examples: the distance between two points

Choose a pair of points (A → B)

A=(1, 2)B=(4, 6)\begin{aligned}A&=\left(1,\ 2\right)\\B&=\left(4,\ 6\right)\end{aligned}

We want the distance between the two points A and B, a length. It should not matter which point we call first.

Choose a pair of points 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 distance work: displacement, squared distance, math.dist, the midpoint, the taxicab distance, and an exact equality that the doubles get wrong.

code/python/coordinates_distance.pydisplacement, distance, midpoint
import math
from fractions import Fraction


def displacement(a, b):
    """The displacement from point a to point b: b - a, component by component."""
    return (b[0] - a[0], b[1] - a[1])


def squared_distance(a, b):
    dx, dy = displacement(a, b)
    return dx * dx + dy * dy


A, B = (1, 2), (4, 6)
step = displacement(A, B)  # (3, 4): the direction matters ...
step_back = displacement(B, A)  # (-3, -4): ... so reversing it flips both signs
distance = math.hypot(*step)  # 5.0: the length of the step
distance_from_points = math.dist(A, B)  # 5.0: math.dist takes the two points directly
same_both_ways = math.dist(B, A) == math.dist(A, B)  # True: distance has no direction
taxicab = abs(step[0]) + abs(step[1])  # 7: walking along the axes, never shorter than 5.0
midpoint = ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2)  # (2.5, 4.0)

# Comparing distances needs no square root: both sides are non-negative, so d1 < d2 exactly when d1**2 < d2**2.
origin = (0, 0)
closer = squared_distance(origin, (3, 4)) < squared_distance(origin, (4, 4))  # 25 < 32: True

# Equal distances, decided exactly. Build Fractions from STRINGS so that no binary rounding enters.
P = (Fraction("0.1"), Fraction("0.7"))
Q = (Fraction("0.5"), Fraction("0.5"))
exact_equal = squared_distance(origin, P) == squared_distance(origin, Q)  # True: both are 1/2
float_equal = (0.1**2 + 0.7**2) == (0.5**2 + 0.5**2)  # False: 0.49999999999999994 against 0.5

print(step, distance, taxicab, midpoint, exact_equal, float_equal)

Then the quadrant decision, with the common mistake and what happens to axis points and to -0.0.

code/python/coordinates_quadrant.pyquadrants and the axes
import math


def quadrant(x, y):
    """The quadrant 1..4 of the point (x, y), or None for a point on an axis or the origin: the axes belong to no quadrant."""
    if x == 0 or y == 0:
        return None
    if x > 0:
        return 1 if y > 0 else 4
    return 2 if y > 0 else 3


def lumpy_quadrant(x, y):
    """The common mistake: the last `else` swallows every point that is not strictly inside quadrants I-III."""
    if x > 0 and y > 0:
        return 1
    elif x < 0 and y > 0:
        return 2
    elif x < 0 and y < 0:
        return 3
    else:
        return 4


second = quadrant(-3, 2)  # 2
fourth = quadrant(3, -2)  # 4
on_y_axis = quadrant(0, -4)  # None: (0, -4) is on the axis, in no quadrant
at_origin = quadrant(0, 0)  # None

wrong_on_axis = lumpy_quadrant(0, -4)  # 4: wrong, the point is on an axis
wrong_at_origin = lumpy_quadrant(0, 0)  # 4: wrong, the origin is in no quadrant

# -0.0 is a distinct double that compares equal to 0, so == 0 catches it, but a "sign bit" test does not.
negative_zero = quadrant(-0.0, 3)  # None: -0.0 == 0
sign_bit_of_negative_zero = math.copysign(1, -0.0)  # -1.0: the sign bit says "negative" for a zero

print(second, fourth, on_y_axis, wrong_on_axis, negative_zero)

Finally the picture-to-array conversion, with the two slips from the pixel stage run on a real array.

code/python/coordinates_image_index.pymath coordinates to array rows (needs NumPy)
import numpy as np

H, W = 4, 5
img = np.arange(H * W).reshape(H, W)  # img[row, col]: row 0 is the TOP row, the cell number counts left to right, then down


def to_row_col(x, y, height=H):
    """Lattice point (x, y) of the math plane (y grows UP) -> array indices (row, col) (row grows DOWN)."""
    return height - 1 - y, x


x, y = 2, 0  # the bottom row of the picture
row, col = to_row_col(x, y)  # (3, 2): the last row of the array
pixel = img[row, col]  # 17
flat_index = row * W + col  # 17: the same cell in the flattened array
same_flat = img.ravel()[flat_index] == pixel  # True
back_to_y = H - 1 - row  # 0: the formula undoes itself, so the round trip returns y

# The math view of the same data: flip the rows once, then img_math[y, x] is the pixel at the point (x, y).
img_math = np.flipud(img)
same_pixel = img_math[y, x] == img[row, col]  # True

# Two classic slips.
try:
    img[H - y, x]  # off by one: row H does not exist
except IndexError:
    off_by_one_fails = True
swapped = img[x, y]  # 10: in range, no error, and a DIFFERENT pixel (row 2, column 0). A silent bug.

print(row, col, pixel, flat_index, swapped)

Two observations about the first file. The comparison closer uses squared distances and never takes a root. This is legitimate because both sides are non-negative and squaring is strictly increasing on non-negative numbers, so d1<d2d_1<d_2 exactly when d12<d22d_1^{2}<d_2^{2}. Comparing without roots is both faster and exact, and nearest-neighbour searches do it all the time. And the last lines build two Fractions from strings. A Fraction made from the float 0.1 would carry the binary rounding along with it.

Explore: change the parameters

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

  • On the point stage, find every point with integer coordinates in the range −7-7 to 77, both coordinates non-zero, whose distance to the origin is a whole number. (There are exactly eight, all of the 33-44-55 family.)
  • On the point stage, drag PP along a circle around the origin of radius 55. Which quantity stays fixed, and which quantities change?
  • On the point stage, set x=0.5x=0.5 and y=−1.5y=-1.5. Which quadrant? What are the distances to the two axes? Is the distance to the origin rational?
  • On the line stage, set a=2a=2 and b=6b=6, then change aa to −2-2. What happens to the signed step, the distance, and the “via zero” number?
  • On the segment stage, put A=(0,0)A=(0,0) and move BB along the line y=xy=x. What is the ratio of the taxicab distance to the straight-line distance, and does it depend on where BB is?
  • On the pixel stage, make the image 10×210\times 2, put the point at the top row, then at the bottom row. Which row index does each have?

Common mistakes

Numerical considerations

The mathematics above is exact. The doubles a program uses are not, and the distance formula meets two problems.

Equality. Whether two points are equally far from the origin is a question about whether two sums of squares are equal, and a test with == on doubles gets it wrong for perfectly ordinary decimals. The points (0.1, 0.7)(0.1,\,0.7) and (0.5, 0.5)(0.5,\,0.5) both have squared distance exactly 12\tfrac12, because 1100+49100=50100\tfrac{1}{100}+\tfrac{49}{100}=\tfrac{50}{100}. A double holds 0.10.1 and 0.70.7 only approximately, so 0.1**2 + 0.7**2 is 0.49999999999999994 while 0.5**2 + 0.5**2 is exactly 0.5. The table shows the exact value next to the double for several points. A row marked “no” is a case where the program’s answer is a different number from the true one.

pointPythonexact x² + y²double (what the program holds)agree?
(0.1, 0.7)0.1**2 + 0.7**20.50.49999999999999994⚠ no
(0.5, 0.5)0.5**2 + 0.5**20.50.5yes
(0.3, 0.4)0.3**2 + 0.4**20.250.25yes
(0.6, 0.8)0.6**2 + 0.8**211yes
(0.1, 0.2)0.1**2 + 0.2**20.050.05000000000000001⚠ no
(1.1, 2.2)1.1**2 + 2.2**26.056.050000000000001⚠ no
(2.3, 0.4)2.3**2 + 0.4**25.455.449999999999999⚠ no

Overflow and underflow. Squaring is where large and small numbers go wrong. For the point (3⋅10200, 4⋅10200)(3\cdot 10^{200},\,4\cdot 10^{200}), the true distance is 5⋅102005\cdot10^{200}, which a double holds without difficulty. But the naive math.sqrt(x*x + y*y) computes x⋅x=9⋅10400x\cdot x=9\cdot10^{400} first, which is beyond the largest double, so the intermediate result is inf and so is the answer. For tiny coordinates such as 10−20010^{-200} it is the squares that underflow to 0.0, so the naive answer is 0.0 where the true distance is about 1.414⋅10−2001.414\cdot 10^{-200}. Python’s math.hypot and math.dist scale their arguments internally and return the right size for both. Use them in code that may meet extreme values; the formula on paper is unchanged.

Negative zero. The double -0.0 is a separate value from 0.0 that compares equal to it. The test x == 0 therefore catches it, and the quadrant code above treats (−0.0, 3)(-0.0,\,3) as an axis point. But anything that reads the sign bit (math.copysign(1, x), or formatting the number) sees -0.0 as negative. A sign test written with < and > is safe; one written with the sign bit is not.

Connections to engineering and machine learning

  • Images are arrays. Every digital image is a grid of pixels addressed by row and column, with row 00 on top. Moving between a mathematical picture and an array, between a plotting library and an image library, or between two libraries with different origins is exactly the conversion in this chapter. Mistakes here produce upside-down or mirrored output, silently.
  • Bounding boxes. Object detectors describe a rectangle as a corner plus a width and height, (x, y, w, h), or as two corners, (x1, y1, x2, y2), and libraries differ on whether yy points down and which corner is the anchor. Check the convention before comparing boxes from two sources.
  • Distance between feature vectors. A data point with two features is a point in a plane, and its distance to another is the formula above. The nearest-neighbour method picks the data point at the smallest distance, and clustering methods group points by it. The distance is only meaningful if the two axes are comparable: a feature in metres and one in seconds cannot be mixed (see units), and a feature measured in thousands will dominate one measured in tenths.
  • Squared distance in practice. The squared Euclidean distance is the cost that least-squares methods and kk-means minimise. It is differentiable at zero where the distance is not, and it avoids the root altogether.
  • Looking ahead. In Vectors the displacement from PP to QQ becomes an object of its own, v=Q−Pv=Q-P, and the point is recovered as Q=P+vQ=P+v. The distance formula returns there as the length of a vector.

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.

prediction

Which quadrant?

In which quadrant is the point (−3, 2)? Predict first, then check it on the first stage.

Choose one answer

prediction

A point on an axis

Which statement is true about the point (0, −4)?

Choose one answer

prediction

How far from the x-axis?

The point P = (−3, 2). How far is P from the x-axis?

Choose one answer

calculation

Distance on a number line

What is the distance between the points −3 and 5 on a number line?

calculation

The signed step, and the distance

You walk on a number line from a = 6 to b = −2. Give the signed step b − a and the distance |b − a|.

calculation

Distance between two points

What is the distance between A = (1, 2) and B = (4, 6)?

calculation

An exact radical distance

Write the distance between A = (−1, 2) and B = (3, −4) as a·√b, where b has no square factor left. Enter the whole numbers a and b.

calculation

The midpoint

What is the midpoint of A = (−3, 4) and B = (5, −2)? Enter its two coordinates.

construction

Build a point

Give a point (x, y) in quadrant III whose distance from the origin is exactly 5. Fractions and decimals are allowed.

explanation

Along the axes, or straight?

The taxicab distance is |dx| + |dy| and the straight-line distance is √(dx² + dy²). Which statement is always true?

Choose one answer

explanation

Equal distances, decided exactly

Are the points (0.1, 0.7) and (0.5, 0.5) the same distance from the origin? Decide it without a calculator, by comparing squared distances.

Choose one answer

calculation

Row and column of a pixel

An image is 6 pixels wide and 4 tall. The point (x, y) = (4, 1) is measured with y going UP from the bottom row. Which row (counted from the top, starting at 0) and which column hold that pixel?

prediction

Index into the array

In NumPy, img has shape (H, W), with row 0 at the top of the picture. Which expression reads the pixel at the mathematical point (x, y), where y is measured up from the bottom row?

Choose one answer

Summary and checkpoints

  • A coordinate is a signed distance from an agreed zero, along an agreed positive direction. On a line, the displacement from aa to bb is b−ab-a, and the distance is ∣b−a∣\lvert b-a\rvert.
  • A point in the plane is an ordered pair (x,y)(x,y). The distance from the point to the xx-axis is ∣y∣\lvert y\rvert and to the yy-axis is ∣x∣\lvert x\rvert.
  • The quadrant is decided by the two signs. A point with a zero coordinate is on an axis and in no quadrant; the origin is in none.
  • The distance between AA and BB is Δx2+Δy2\sqrt{\Delta x^{2}+\Delta y^{2}}, which is Pythagoras on the right triangle with legs ∣Δx∣\lvert\Delta x\rvert and ∣Δy∣\lvert\Delta y\rvert. The displacement has a direction, the distance does not. The midpoint averages each coordinate.
  • Compare distances through their squares, which stay exact. The taxicab distance ∣Δx∣+∣Δy∣\lvert\Delta x\rvert+\lvert\Delta y\rvert is never shorter than the straight line.
  • In an array, row 00 is the top and row =H−1−y=H-1-y, column =x=x. Never assume a library’s origin.
  • In code: use math.dist and math.hypot for extreme values, compare with a tolerance or exactly, and write every sign test with a case for zero.

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

  1. Say which quadrant (−3,2)(-3,2) is in, and why (0,−4)(0,-4) is in none.
  2. Give the distance from (−3,2)(-3,2) to each axis, and say which coordinate each uses.
  3. Compute the displacement and the distance from (1,2)(1,2) to (4,6)(4,6), and say what changes when the points are swapped.
  4. Write 52\sqrt{52} in simplest form and say why the answer is exact.
  5. Convert the point (2,0)(2,0) in a picture 44 rows high into a row and a column of an array.

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