Skip to content
The Lumenary
Grades 9–12Ages 14–18 75 min

Vectors and coordinate geometry

Arrows you can add, stretch and multiply — and a single product that measures lengths, angles and right angles in any number of dimensions.

Formally verified · 14 LeanComputationally tested · 2 PythonAI-drafted · awaiting human reviewMathematically reviewed: Awaiting human review

Your guides: Chloe, Margaret, Gideon

MotivationH

Where does the ferry really go?

A ferry crosses the Jamuna river in Bangladesh. Its engine drives it straight across at 44 km/h relative to the water, but the river flows downstream at 33 km/h. Where does the ferry actually go, and how fast? The two motions combine: each hour the ferry moves 44 km across and 33 km downstream. Its actual velocity is an arrow of length 42+32=5\sqrt{4^2 + 3^2} = 5 km/h, pointing diagonally. To land at the jetty directly opposite, the pilot must aim upstream.

Quantities like velocity, force and displacement have a size and a direction. We call them vectors. They can be added (the ferry's velocity plus the river's) and scaled (twice as fast, or backwards). Once vectors are written in coordinates, a single operation — the dot product — gives lengths, angles, right angles and projections, and lets us prove geometric theorems with a few lines of algebra. The same ideas then work, unchanged, in three dimensions and in a thousand.

IntuitionH

Arrows that can move

Picture a vector as an arrow. What matters is its length and its direction, not where it starts: sliding an arrow without turning or stretching it gives the same vector. The displacement "3 km east, 4 km north" is the same whether you start in Dhaka or in Lima.

  • To add two vectors, place the tail of the second at the tip of the first; the sum goes from the first tail to the second tip ("tip to tail"). Doing it in the other order gives the other two sides of a parallelogram, with the same diagonal: u+v=v+uu + v = v + u.
  • To scale a vector by a number tt, stretch it by the factor ∣t∣|t|, and reverse it if t<0t \lt 0.

Put the tail at the origin of a coordinate system, and the tip has coordinates: the vector "3 east, 4 north" becomes (3,4)(3, 4). Now the geometry becomes arithmetic: adding arrows is adding coordinates, and stretching arrows is multiplying coordinates.

DefinitionsH

Definitions

Definition— vector

A vector in the plane R2\R^2 is an ordered pair v=(v1,v2)v = (v_1, v_2) of real numbers; a vector in space R3\R^3 is a triple (v1,v2,v3)(v_1, v_2, v_3); in general a vector in Rn\R^n is a list of nn real numbers, its components. The vector from point AA to point BB is AB→=B−A\overrightarrow{AB} = B - A, computed component by component.

Definition— addition and scalar multiplication

For vectors u,vu, v in Rn\R^n and a real number tt (a scalar):

u+v=(u1+v1,…,un+vn),t v=(tv1,…,tvn).u + v = (u_1 + v_1, \dots, u_n + v_n), \qquad t\,v = (t v_1, \dots, t v_n).

The zero vector is 0=(0,…,0)0 = (0, \dots, 0), and −v=(−1)v-v = (-1)v. Two vectors are parallel if one is a scalar multiple of the other.

Definition— dot product and length

The dot product of uu and vv in Rn\R^n is the number

u⋅v=u1v1+u2v2+⋯+unvn.u \cdot v = u_1 v_1 + u_2 v_2 + \cdots + u_n v_n.

The length (or norm) of vv is ∣v∣=v⋅v=v12+⋯+vn2|v| = \sqrt{v \cdot v} = \sqrt{v_1^2 + \cdots + v_n^2}. A unit vector has length 11; for v≠0v \ne 0, the vector v/∣v∣v / |v| is the unit vector in the direction of vv.

In the plane, ∣v∣=v12+v22|v| = \sqrt{v_1^2 + v_2^2} is the Pythagorean theorem; in space, ∣v∣2=v12+v22+v32|v|^2 = v_1^2 + v_2^2 + v_3^2 comes from using Pythagoras twice. The distance between points AA and BB is ∣B−A∣|B - A|.

Definition— angle and perpendicularity

For non-zero vectors u,vu, v, the angle θ\theta between them, with 0≤θ≤π0 \le \theta \le \pi, is given by

cos⁡θ=u⋅v∣u∣ ∣v∣.\cos\theta = \frac{u \cdot v}{|u|\,|v|}.

The vectors are perpendicular (orthogonal), u⊥vu \perp v, if u⋅v=0u \cdot v = 0.

In R2\R^2 and R3\R^3 we will prove that this angle is the angle you would measure between the arrows. In higher dimensions there is no picture to measure, and the formula becomes the definition; the Cauchy–Schwarz inequality below guarantees that the right-hand side lies between −1-1 and 11, so the definition makes sense.

Definition— lines and planes

The line through the point pp with direction d≠0d \ne 0 is the set {p+t d:t∈R}\{p + t\,d : t \in \R\}, in R2\R^2 or R3\R^3. Writing out components gives parametric equations, such as x=p1+td1x = p_1 + t d_1, y=p2+td2y = p_2 + t d_2.

The plane in R3\R^3 through pp with normal vector n≠0n \ne 0 is the set of points xx with n⋅(x−p)=0n \cdot (x - p) = 0. With n=(a,b,c)n = (a, b, c) this is the familiar equation ax+by+cz=n⋅pax + by + cz = n \cdot p. A plane can also be written in vector form, {p+s u+t v:s,t∈R}\{p + s\,u + t\,v : s, t \in \R\}, with uu and vv non-parallel vectors lying in it.

Worked examplesH

Worked examples

Example— Arithmetic, lengths and unit vectors

Let u=(1,2,2)u = (1, 2, 2) and v=(2,−1,2)v = (2, -1, 2). Then u+v=(3,1,4)u + v = (3, 1, 4), 3u−v=(1,7,4)3u - v = (1, 7, 4), ∣u∣=1+4+4=3|u| = \sqrt{1 + 4 + 4} = 3 and ∣v∣=3|v| = 3. The unit vector in the direction of uu is 13(1,2,2)\tfrac13(1, 2, 2).

Example— An angle in space

For the same uu and vv: u⋅v=2−2+4=4u \cdot v = 2 - 2 + 4 = 4, so cos⁡θ=43⋅3=49\cos\theta = \dfrac{4}{3 \cdot 3} = \dfrac49 and θ=arccos⁡49≈63.6∘\theta = \arccos\tfrac49 \approx 63.6^\circ. No protractor could measure this in space — but the dot product does it in one line.

Example— Perpendicular or not?

(2,3)⋅(3,−2)=6−6=0(2, 3) \cdot (3, -2) = 6 - 6 = 0, so these vectors are perpendicular. In general, (a,b)(a, b) is perpendicular to (−b,a)(-b, a): turning a vector by 90∘90^\circ swaps its components and changes one sign. On the other hand (1,1,1)⋅(1,1,−1)=1≠0(1, 1, 1) \cdot (1, 1, -1) = 1 \ne 0: those diagonals of a cube are not perpendicular.

Example— Projection and work

A child pulls a sledge along level snow with a force F=(30,40)F = (30, 40) newtons (horizontal and vertical parts). The sledge moves along the displacement d=(10,0)d = (10, 0) metres. Only the part of the force along the motion does work: the work is W=F⋅d=300W = F \cdot d = 300 joules. The projection of FF onto dd is

proj⁡dF=F⋅dd⋅d d=300100(10,0)=(30,0),\operatorname{proj}_d F = \frac{F \cdot d}{d \cdot d}\, d = \frac{300}{100}(10, 0) = (30, 0),

and the rest, F−proj⁡dF=(0,40)F - \operatorname{proj}_d F = (0, 40), is perpendicular to dd: it lifts the sledge a little but does no work along the ground.

Example— A line through two points, and where it meets a plane

The line through A=(1,0,0)A = (1, 0, 0) and B=(2,2,2)B = (2, 2, 2) has direction d=B−A=(1,2,2)d = B - A = (1, 2, 2), so it is {(1+t,2t,2t):t∈R}\{(1 + t, 2t, 2t) : t \in \R\}. Where does it meet the plane x+y+z=11x + y + z = 11? Substitute: (1+t)+2t+2t=11(1 + t) + 2t + 2t = 11, so t=2t = 2 and the point is (3,4,4)(3, 4, 4). In the plane R2\R^2, the line through (1,2)(1, 2) with direction (3,1)(3, 1) has normal (−1,3)(-1, 3) and equation −(x−1)+3(y−2)=0-(x - 1) + 3(y - 2) = 0, that is, −x+3y=5-x + 3y = 5.

Example— A non-example: lengths do not add

u=(3,0)u = (3, 0) and v=(0,4)v = (0, 4) have ∣u∣+∣v∣=7|u| + |v| = 7, but ∣u+v∣=∣(3,4)∣=5|u + v| = |(3, 4)| = 5. Walking 33 km east and then 44 km north leaves you only 55 km from home. Lengths add only when the vectors point the same way.

ProofH

The dot product: theorems

Directly from the definition, by expanding components, the dot product is symmetric, u⋅v=v⋅uu \cdot v = v \cdot u, and distributes over addition and scalars: u⋅(v+w)=u⋅v+u⋅wu \cdot (v + w) = u \cdot v + u \cdot w and (tu)⋅v=t(u⋅v)(t u) \cdot v = t (u \cdot v). Also v⋅v=∣v∣2≥0v \cdot v = |v|^2 \ge 0, with equality only for v=0v = 0. Everything below follows from these rules alone.

Theorem— Theorem (geometric meaning of the dot product)

For non-zero vectors u,vu, v in R2\R^2 or R3\R^3 that make the angle θ\theta (measured between the arrows drawn from a common tail),

u⋅v=∣u∣ ∣v∣cos⁡θ.u \cdot v = |u|\,|v| \cos\theta.

Proof

Draw uu and vv from a common point OO, ending at PP and QQ. The third side of triangle OPQOPQ is the vector u−vu - v. By the law of cosines (see trigonometry),

∣u−v∣2=∣u∣2+∣v∣2−2 ∣u∣ ∣v∣cos⁡θ.|u - v|^2 = |u|^2 + |v|^2 - 2\,|u|\,|v|\cos\theta.

On the other hand, by the rules above, ∣u−v∣2=(u−v)⋅(u−v)=u⋅u−2 u⋅v+v⋅v=∣u∣2−2 u⋅v+∣v∣2|u - v|^2 = (u - v)\cdot(u - v) = u\cdot u - 2\,u\cdot v + v \cdot v = |u|^2 - 2\,u \cdot v + |v|^2. Comparing the two expressions gives u⋅v=∣u∣ ∣v∣cos⁡θu \cdot v = |u|\,|v|\cos\theta. (If uu and vv are parallel, the "triangle" is flat, and θ\theta is 00 or π\pi; the same computation works directly.) ■\blacksquare

∎

Theorem— Corollary

Non-zero vectors uu and vv are perpendicular exactly when u⋅v=0u \cdot v = 0. Moreover, ∣u+v∣2=∣u∣2+∣v∣2|u + v|^2 = |u|^2 + |v|^2 if and only if u⋅v=0u \cdot v = 0 — Pythagoras' theorem and its converse in vector form.

Proof

cos⁡θ=0\cos\theta = 0 exactly when θ=90∘\theta = 90^\circ. For the second statement, expand: ∣u+v∣2=∣u∣2+2 u⋅v+∣v∣2|u + v|^2 = |u|^2 + 2\,u\cdot v + |v|^2. ■\blacksquare

∎

Theorem— Cauchy–Schwarz inequality

For all vectors u,vu, v in Rn\R^n:   ∣u⋅v∣≤∣u∣ ∣v∣\;|u \cdot v| \le |u|\,|v|. Equality holds exactly when uu and vv are parallel.

Proof

In R2\R^2 and R3\R^3 this follows from the last theorem, since ∣cos⁡θ∣≤1|\cos\theta| \le 1. But in Rn\R^n we want to define angles using Cauchy–Schwarz, so we need a proof that does not use angles.

If v=0v = 0 both sides are 00. Otherwise, for every real number tt,

0≤∣u−tv∣2=∣u∣2−2t (u⋅v)+t2∣v∣2.0 \le |u - t v|^2 = |u|^2 - 2t\,(u \cdot v) + t^2 |v|^2.

The right-hand side is a quadratic in tt with positive leading coefficient ∣v∣2|v|^2 that is never negative, so it has at most one real root and its discriminant is at most zero (see quadratics):

4(u⋅v)2−4∣u∣2∣v∣2≤0,that is,(u⋅v)2≤∣u∣2∣v∣2.4 (u \cdot v)^2 - 4 |u|^2 |v|^2 \le 0, \qquad\text{that is,}\qquad (u \cdot v)^2 \le |u|^2|v|^2.

Taking square roots gives the inequality. Equality means the discriminant is zero, so the quadratic has a root t0t_0, and then ∣u−t0v∣=0|u - t_0 v| = 0, i.e. u=t0vu = t_0 v. Conversely, if u=tvu = t v both sides equal ∣t∣ ∣v∣2|t|\,|v|^2. ■\blacksquare

∎

In the plane there is an even more direct proof, Lagrange's identity:

∣u∣2∣v∣2−(u⋅v)2=(u12+u22)(v12+v22)−(u1v1+u2v2)2=(u1v2−u2v1)2≥0,|u|^2|v|^2 - (u \cdot v)^2 = (u_1^2 + u_2^2)(v_1^2 + v_2^2) - (u_1 v_1 + u_2 v_2)^2 = (u_1 v_2 - u_2 v_1)^2 \ge 0,

which you can check by expanding. The gap is a perfect square, and it vanishes exactly when u1v2=u2v1u_1 v_2 = u_2 v_1, i.e. when uu and vv are parallel.

Theorem— Triangle inequality

For all vectors u,vu, v in Rn\R^n:   ∣u+v∣≤∣u∣+∣v∣\;|u + v| \le |u| + |v|, with equality exactly when one vector is a non-negative multiple of the other.

Proof

Using Cauchy–Schwarz in the middle step,

∣u+v∣2=∣u∣2+2 u⋅v+∣v∣2≤∣u∣2+2 ∣u∣ ∣v∣+∣v∣2=(∣u∣+∣v∣)2.|u + v|^2 = |u|^2 + 2\,u\cdot v + |v|^2 \le |u|^2 + 2\,|u|\,|v| + |v|^2 = (|u| + |v|)^2.

Both ∣u+v∣|u + v| and ∣u∣+∣v∣|u| + |v| are non-negative, so ∣u+v∣≤∣u∣+∣v∣|u + v| \le |u| + |v|. Equality holds exactly when u⋅v=∣u∣ ∣v∣u \cdot v = |u|\,|v|: by the equality case of Cauchy–Schwarz the vectors are parallel, and u⋅v≥0u \cdot v \ge 0 forces them to point the same way (or one to be zero). ■\blacksquare

∎

Theorem— Projection

For v≠0v \ne 0, every vector uu splits as u=p+wu = p + w with p=u⋅vv⋅v vp = \dfrac{u \cdot v}{v \cdot v}\,v parallel to vv and w=u−pw = u - p perpendicular to vv.

Proof

w⋅v=u⋅v−u⋅vv⋅v (v⋅v)=0w \cdot v = u \cdot v - \dfrac{u\cdot v}{v \cdot v}\,(v \cdot v) = 0. ■\blacksquare

∎

ProofH

Geometry proved with vectors

Vectors give short proofs of classical theorems. The trick is to name the points by their position vectors a,b,c,…a, b, c, \ldots (from a fixed origin) and to translate each geometric statement into an equation.

Theorem— The diagonals of a parallelogram bisect each other

If ABCDABCD is a parallelogram, the midpoints of the diagonals ACAC and BDBD coincide.

Proof

"ABCDABCD is a parallelogram" means that the sides ABAB and DCDC are the same vector: b−a=c−db - a = c - d. Rearranging, a+c=b+da + c = b + d. The midpoint of ACAC is 12(a+c)\tfrac12(a + c) and the midpoint of BDBD is 12(b+d)\tfrac12(b + d), and these are equal. ■\blacksquare

∎

Theorem— The medians of a triangle meet at the centroid

The three medians of a triangle ABCABC (each joining a vertex to the midpoint of the opposite side) pass through the point g=13(a+b+c)g = \tfrac13(a + b + c), which divides each median in the ratio 2:12 : 1.

Proof

The midpoint of BCBC is m=12(b+c)m = \tfrac12(b + c). The point two thirds of the way from AA to mm is

a+23 (m−a)=a+13(b+c)−23a=13(a+b+c).a + \tfrac23\,(m - a) = a + \tfrac13(b + c) - \tfrac23 a = \tfrac13(a + b + c).

This expression does not change if we permute aa, bb, cc, so the same point is two thirds of the way along each median. ■\blacksquare

∎

Theorem— The diagonals of a rhombus are perpendicular

If ABCDABCD is a parallelogram with all sides equal, its diagonals are perpendicular.

Proof

Let u=b−au = b - a and v=d−av = d - a, so the diagonals are AC→=u+v\overrightarrow{AC} = u + v and DB→=u−v\overrightarrow{DB} = u - v. Then (u+v)⋅(u−v)=∣u∣2−∣v∣2=0(u + v)\cdot(u - v) = |u|^2 - |v|^2 = 0, because ∣u∣=∣v∣|u| = |v|. ■\blacksquare

∎

Compare these with the congruent-triangle proofs in Euclidean geometry and proof: the vector proofs are shorter, but they hide the geometry inside the algebra. The rhombus argument, read backwards, is also a proof of Thales' theorem: if ∣u∣=∣v∣|u| = |v| then (u+v)⊥(u−v)(u + v) \perp (u - v).

Note

Lines in space can be skew. In the plane, two distinct lines either meet in one point or are parallel. In space there is a third possibility: lines such as {(t,0,0)}\{(t, 0, 0)\} and {(0,t,1)}\{(0, t, 1)\} are not parallel (their directions are (1,0,0)(1, 0, 0) and (0,1,0)(0, 1, 0)) and never meet (the first has z=0z = 0, the second z=1z = 1). Such lines are called skew.

Formal mathematicsF

Vectors in Lean

We start in coordinates. Here is the dot product of two vectors in the plane, Lagrange's identity, and the Cauchy–Schwarz inequality with its equality case, exactly as in the lesson:

dot2Lean 4Formally verified
/-- The dot product of two vectors in the plane, given by coordinates:
`(u₁, u₂) · (v₁, v₂) = u₁ v₁ + u₂ v₂`. -/
def dot2 (u v : ℝ × ℝ) : ℝ := u.1 * v.1 + u.2 * v.2
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor
lagrange_identityLean 4Proved here from basic factsFormally verified
/-- **Lagrange's identity** in the plane:
`(u · u)(v · v) − (u · v)² = (u₁ v₂ − u₂ v₁)²`. Both sides are polynomials in the four
coordinates, and `ring` checks that they are equal. -/
theorem lagrange_identity (u v : ℝ × ℝ) :
    dot2 u u * dot2 v v - dot2 u v ^ 2 = (u.1 * v.2 - u.2 * v.1) ^ 2 := by
  unfold dot2
  ring
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor
cauchy_schwarz_2dLean 4Proved here from basic factsFormally verified
/-- **Cauchy–Schwarz in coordinates**: `(u · v)² ≤ (u · u)(v · v)`, that is,
`|u · v| ≤ |u| |v|`. By Lagrange's identity the difference is a square, and squares are never
negative. -/
theorem cauchy_schwarz_2d (u v : ℝ × ℝ) : dot2 u v ^ 2 ≤ dot2 u u * dot2 v v := by
  have h := lagrange_identity u v
  nlinarith [sq_nonneg (u.1 * v.2 - u.2 * v.1)]
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor
cauchy_schwarz_2d_eqLean 4Proved here from basic factsFormally verified
/-- **Equality in Cauchy–Schwarz** happens exactly when `u₁ v₂ = u₂ v₁`, i.e. when `u` and `v`
are parallel (one is a multiple of the other, or one of them is zero). Again from Lagrange's
identity: the gap is `(u₁ v₂ − u₂ v₁)²`, which is zero exactly when `u₁ v₂ − u₂ v₁ = 0`. -/
theorem cauchy_schwarz_2d_eq (u v : ℝ × ℝ) :
    dot2 u v ^ 2 = dot2 u u * dot2 v v ↔ u.1 * v.2 = u.2 * v.1 := by
  have h := lagrange_identity u v
  constructor
  · intro heq
    have hsq : (u.1 * v.2 - u.2 * v.1) ^ 2 = 0 := by linarith
    have := pow_eq_zero_iff (n := 2) (by norm_num) |>.1 hsq
    linarith
  · intro hpar
    have hsq : (u.1 * v.2 - u.2 * v.1) ^ 2 = 0 := by rw [hpar, sub_self]; ring
    linarith
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor

The vector equation and the normal equation of a line in the plane describe the same set of points:

line_vector_iff_normalLean 4Proved here from basic factsFormally verified
/-- **Two equations for the same line.** The line through `p` with direction `d ≠ 0` is
`{p + t d : t ∈ ℝ}` (the vector, or parametric, equation). With the normal vector
`n = (−d₂, d₁)`, perpendicular to `d`, the same line is `{x : n · (x − p) = 0}` (the normal, or
Cartesian, equation). For the hard direction, if `n · (x − p) = 0` we take
`t = d · (x − p) / (d · d)` and check `x = p + t d` coordinate by coordinate. -/
theorem line_vector_iff_normal (p d x : ℝ × ℝ) (hd : d ≠ 0) :
    (∃ t : ℝ, x = p + t • d) ↔ dot2 (-d.2, d.1) (x - p) = 0 := by
  unfold dot2
  constructor
  · rintro ⟨t, rfl⟩
    simp only [add_sub_cancel_left, Prod.smul_fst, Prod.smul_snd, smul_eq_mul]
    ring
  · intro h
    have hdd : d.1 ^ 2 + d.2 ^ 2 ≠ 0 := by
      intro h0
      apply hd
      have h1 : d.1 = 0 := by nlinarith [sq_nonneg d.1, sq_nonneg d.2]
      have h2 : d.2 = 0 := by nlinarith [sq_nonneg d.1, sq_nonneg d.2]
      exact Prod.ext h1 h2
    refine ⟨(d.1 * (x.1 - p.1) + d.2 * (x.2 - p.2)) / (d.1 ^ 2 + d.2 ^ 2), ?_⟩
    simp only [Prod.fst_sub, Prod.snd_sub] at h
    ext
    · simp only [Prod.fst_add, Prod.smul_fst, smul_eq_mul]
      field_simp
      linear_combination (-d.2) * h
    · simp only [Prod.snd_add, Prod.smul_snd, smul_eq_mul]
      field_simp
      linear_combination d.1 * h
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor

Mathlib's general setting is a real inner product space: any collection of vectors with addition, scaling and a dot product obeying the rules at the start of the proofs section. It covers R2\R^2, R3\R^3 and Rn\R^n at once, and also spaces of functions. Mathlib writes ⟪u, v⟫ for u⋅vu \cdot v and ‖u‖ for ∣u∣|u|. Cauchy–Schwarz is in the library, and the triangle inequality is proved here from it:

cauchy_schwarzLean 4Applies a library theoremFormally verified
/-- **The Cauchy–Schwarz inequality** in any real inner product space: `|u · v| ≤ |u| |v|`.
Applies Mathlib's `abs_real_inner_le_norm`. -/
theorem cauchy_schwarz (u v : V) : |⟪u, v⟫| ≤ ‖u‖ * ‖v‖ := abs_real_inner_le_norm u v
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor
triangle_inequalityLean 4Proved here from basic factsFormally verified
/-- **The triangle inequality** `|u + v| ≤ |u| + |v|`, from Cauchy–Schwarz:
`|u + v|² = |u|² + 2 u · v + |v|² ≤ |u|² + 2 |u| |v| + |v|² = (|u| + |v|)²`, and both
`|u + v|` and `|u| + |v|` are non-negative. -/
theorem triangle_inequality (u v : V) : ‖u + v‖ ≤ ‖u‖ + ‖v‖ := by
  have hsq : ‖u + v‖ ^ 2 ≤ (‖u‖ + ‖v‖) ^ 2 := by
    rw [norm_add_sq_real]
    nlinarith [real_inner_le_norm u v]
  nlinarith [norm_nonneg (u + v), norm_nonneg u, norm_nonneg v]
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor
norm_add_le_libraryLean 4Applies a library theoremFormally verified
/-- The triangle inequality as stated in Mathlib, `norm_add_le`, which holds in every normed
space (not only inner product spaces). -/
theorem norm_add_le_library {W : Type*} [NormedAddCommGroup W] (u v : W) : ‖u + v‖ ≤ ‖u‖ + ‖v‖ :=
  norm_add_le u v
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor
pythagoras_iff_perpLean 4Proved here from basic factsFormally verified
/-- **Pythagoras with vectors, and its converse**: `|u + v|² = |u|² + |v|²` exactly when
`u · v = 0`. Expanding gives `|u + v|² = |u|² + 2 u · v + |v|²` (Mathlib's
`norm_add_sq_real`), so the two sides differ by `2 u · v`. -/
theorem pythagoras_iff_perp (u v : V) : ‖u + v‖ ^ 2 = ‖u‖ ^ 2 + ‖v‖ ^ 2 ↔ ⟪u, v⟫ = 0 := by
  rw [norm_add_sq_real]
  constructor
  · intro h; linarith
  · intro h; rw [h]; ring
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor

In Mathlib the angle between vectors is defined by the arccosine of u⋅v/(∣u∣ ∣v∣)u \cdot v / (|u|\,|v|), just as in our definition for Rn\R^n:

cos_angleLean 4Applies a library theoremFormally verified
/-- **The angle between two vectors**: `cos θ = (u · v) / (|u| |v|)`. In Mathlib this is how
the angle `InnerProductGeometry.angle u v` is *defined* (as an `arccos`), and Cauchy–Schwarz is
what guarantees the quotient lies between `−1` and `1`. Applies
`InnerProductGeometry.cos_angle`. -/
theorem cos_angle (u v : V) :
    cos (InnerProductGeometry.angle u v) = ⟪u, v⟫ / (‖u‖ * ‖v‖) :=
  InnerProductGeometry.cos_angle u v
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor
perp_iff_right_angleLean 4Applies a library theoremFormally verified
/-- **Perpendicular means dot product zero**: `u · v = 0` exactly when the angle between `u`
and `v` is `π/2` (90°). Applies `InnerProductGeometry.inner_eq_zero_iff_angle_eq_pi_div_two`.
(Mathlib's convention makes the angle with the zero vector `π/2`, matching `0 · v = 0`.) -/
theorem perp_iff_right_angle (u v : V) :
    ⟪u, v⟫ = 0 ↔ InnerProductGeometry.angle u v = π / 2 :=
  InnerProductGeometry.inner_eq_zero_iff_angle_eq_pi_div_two u v
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor
projection_perpLean 4Proved here from basic factsFormally verified
/-- **Projection.** The projection of `u` onto a non-zero `v` is `((u · v)/(v · v)) v`, and what
is left over, `u − ((u · v)/(v · v)) v`, is perpendicular to `v`:
`(u − c v) · v = u · v − c (v · v) = 0` for `c = (u · v)/(v · v)`. -/
theorem projection_perp (u v : V) (hv : v ≠ 0) :
    ⟪u - (⟪u, v⟫ / ⟪v, v⟫) • v, v⟫ = 0 := by
  have hvv : ⟪v, v⟫ ≠ 0 := inner_self_ne_zero.2 hv
  rw [inner_sub_left, real_inner_smul_left, div_mul_cancel₀ _ hvv, sub_self]
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor

Finally, the two geometry theorems. Note how short the Lean proofs are: once the statement is in vector form, the geometry has become algebra. The tactic module checks identities between linear combinations of vectors:

parallelogram_diagonals_bisectLean 4Proved here from basic factsFormally verified
/-- **The diagonals of a parallelogram bisect each other.** If `ABCD` is a parallelogram, so
that `B − A = C − D` (the sides `AB` and `DC` are the same vector), then the midpoint of the
diagonal `AC` equals the midpoint of the diagonal `BD`: `(A + C)/2 = (B + D)/2`. -/
theorem parallelogram_diagonals_bisect (a b c d : V) (h : b - a = c - d) :
    (1 / 2 : ℝ) • (a + c) = (1 / 2 : ℝ) • (b + d) := by
  have hsum : a + c = b + d := by
    rw [sub_eq_sub_iff_add_eq_add] at h
    rw [add_comm a c, ← h, add_comm]
  rw [hsum]
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor
centroidLean 4Proved here from basic factsFormally verified
/-- **The medians of a triangle meet at the centroid.** The point two thirds of the way from
the vertex `A` to the midpoint `(B + C)/2` of the opposite side is `(A + B + C)/3`. This
expression is symmetric in `A`, `B`, `C`, so the same point lies on all three medians. -/
theorem centroid (a b c : V) :
    a + (2 / 3 : ℝ) • ((1 / 2 : ℝ) • (b + c) - a) = (1 / 3 : ℝ) • (a + b + c) := by
  module
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Vectors.leanTry it in the Lean web editor
ComputationC

Exact checks and random directions

Cauchy-Schwarz and the triangle inequality, checked exactlyPythonExact computationExhaustive finite checkSymbolic computationComputationally tested
"""Cauchy-Schwarz and the triangle inequality, checked exactly.

For every pair of integer vectors u, v in three dimensions with coordinates
from -2 to 2 we check, in exact integer arithmetic, Lagrange's identity
|u|^2 |v|^2 - (u.v)^2 = |u x v|^2, hence the Cauchy-Schwarz inequality
(u.v)^2 <= |u|^2 |v|^2, and find exactly which pairs give equality. We then
test the triangle inequality and its equality cases, and finally let SymPy
verify Lagrange's identity for symbolic coordinates.
"""

import itertools
import math

import sympy

def dot(u, v):
    """The dot product u . v."""
    return sum(a * b for a, b in zip(u, v))

def cross(u, v):
    """The cross product u x v of two 3D vectors."""
    return (u[1] * v[2] - u[2] * v[1],
            u[2] * v[0] - u[0] * v[2],
            u[0] * v[1] - u[1] * v[0])

def is_parallel(u, v):
    """True when u and v are parallel (including when one is zero)."""
    return cross(u, v) == (0, 0, 0)

def same_direction(u, v):
    """True when one vector is a non-negative multiple of the other."""
    return is_parallel(u, v) and dot(u, v) >= 0

def triangle_holds(u, v):
    """Exact test of |u + v| <= |u| + |v|, with no square roots.

    |u+v| <= |u| + |v|  iff  |u+v|^2 <= |u|^2 + |v|^2 + 2|u||v|
    iff  u.v <= |u||v|, which (for u.v >= 0) is (u.v)^2 <= |u|^2 |v|^2.
    Returns (holds, equality).
    """
    uv, uu, vv = dot(u, v), dot(u, u), dot(v, v)
    holds = uv <= 0 or uv * uv <= uu * vv
    equality = uv >= 0 and uv * uv == uu * vv
    return holds, equality

def main():
    grid = list(itertools.product(range(-2, 3), repeat=3))
    pairs = cs_fail = lagrange_fail = 0
    cs_equal = parallel = tri_fail = tri_equal = same_dir = 0
    for u in grid:
        for v in grid:
            pairs += 1
            uv, uu, vv = dot(u, v), dot(u, u), dot(v, v)
            lagrange_fail += uu * vv - uv * uv != dot(cross(u, v), cross(u, v))
            cs_fail += uv * uv > uu * vv
            cs_equal += uv * uv == uu * vv
            parallel += is_parallel(u, v)
            holds, equality = triangle_holds(u, v)
            tri_fail += not holds
            tri_equal += equality
            same_dir += same_direction(u, v)
    print(f"All pairs of vectors in {{-2,...,2}}^3: {pairs} pairs (exact integers)")
    print(f"  Lagrange identity |u|^2|v|^2 - (u.v)^2 = |u x v|^2 fails: "
          f"{lagrange_fail}")
    print(f"  Cauchy-Schwarz (u.v)^2 <= |u|^2 |v|^2 fails: {cs_fail}")
    print(f"  equality (u.v)^2 = |u|^2|v|^2: {cs_equal} pairs; "
          f"parallel pairs: {parallel}")
    print(f"  triangle inequality fails: {tri_fail}")
    print(f"  equality |u+v| = |u|+|v|: {tri_equal} pairs; "
          f"same-direction pairs: {same_dir}")
    u, v = (1, 2, 2), (2, -1, 2)
    print(f"Example: u = {u}, v = {v}: u.v = {dot(u, v)}, |u| = 3, |v| = 3,")
    angle = math.degrees(math.acos(dot(u, v) / 9))
    print(f"  angle = arccos(4/9) = {angle:.2f} degrees")
    s = sympy.symbols("u1:4 v1:4")
    us, vs = s[:3], s[3:]
    gap = sympy.expand(dot(us, us) * dot(vs, vs) - dot(us, vs) ** 2
                       - dot(cross(us, vs), cross(us, vs)))
    print(f"SymPy: |u|^2|v|^2 - (u.v)^2 - |u x v|^2 expands to {gap}")
    print()
    print("Epistemic status: the grid check is exact and exhaustive, but only")
    print("for these 15,625 integer pairs. SymPy's expansion to 0 is a symbolic")
    print("verification of Lagrange's identity for all real coordinates (as")
    print("far as SymPy is trusted); the lesson's proof needs no computer.")

if __name__ == "__main__":
    main()
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

The equality cases are exactly the parallel pairs (for Cauchy–Schwarz) and the same-direction pairs (for the triangle inequality), just as the theorems say. The grid check is exact and exhaustive, but only for these 15,62515{,}625 pairs; SymPy's symbolic expansion verifies Lagrange's identity for all coordinates, and the proof explains it.

Now something surprising. What is the angle between two random directions?

Random vectors in high dimensions are nearly perpendicularPythonStatistical evidenceComputationally tested
"""In high dimensions, two random vectors are almost perpendicular.

We draw pairs of random vectors in R^n (each coordinate an independent
standard normal number, seeded) and measure the angle between them with the
dot product formula cos(theta) = u.v / (|u| |v|). In the plane the angle is
spread evenly between 0 and 180 degrees; as n grows it piles up near 90
degrees. Theory says cos(theta) has mean 0 and variance exactly 1/n, so its
standard deviation is 1/sqrt(n): we compare the sample with that prediction.
"""

import math
import random
import statistics

def random_vector(n, rng):
    """A vector of n independent standard normal coordinates."""
    return [rng.gauss(0.0, 1.0) for _ in range(n)]

def cos_angle(u, v):
    """cos of the angle between non-zero vectors u and v."""
    dot = sum(a * b for a, b in zip(u, v))
    return dot / math.sqrt(sum(a * a for a in u) * sum(b * b for b in v))

def sample_cosines(n, pairs, rng):
    """cos(theta) for `pairs` independent random pairs in R^n."""
    return [cos_angle(random_vector(n, rng), random_vector(n, rng))
            for _ in range(pairs)]

def main():
    rng = random.Random(2024)
    print("   n   mean angle   share within 10 deg of 90   sd(cos)  1/sqrt(n)")
    for n in (2, 3, 10, 100, 1000):
        cosines = sample_cosines(n, 300, rng)
        angles = [math.degrees(math.acos(c)) for c in cosines]
        near = sum(abs(a - 90) <= 10 for a in angles) / len(angles)
        sd = statistics.pstdev(cosines)
        print(f"{n:4d}   {statistics.mean(angles):7.2f} deg   "
              f"{near:24.0%}   {sd:7.4f}  {1 / math.sqrt(n):9.4f}")
    print("In the plane a random angle is equally likely anywhere in [0, 180];")
    print("in 1000 dimensions almost every pair is within a few degrees of 90.")
    print()
    print("Epistemic status: statistical evidence from 300 random pairs per")
    print("dimension, so each sample statistic has its own random error (a few")
    print("per cent for sd(cos)). The value 1/sqrt(n) is exact: cos(theta) is")
    print("distributed like one coordinate of a random unit vector, whose")
    print("squares average to 1/n because the n squares add up to 1.")

if __name__ == "__main__":
    main()
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

In the plane the angle can be anything. In 10001000 dimensions, two random vectors are almost always within a few degrees of perpendicular! The spread of cos⁡θ\cos\theta is about 1/n1/\sqrt n. This is statistical evidence from finite samples, matched against an exact formula for the variance. It is one face of the "concentration of measure" that makes high-dimensional geometry so different from our intuition — and so useful in statistics and machine learning.

Explore

Explore: adding and projecting

Change the components of uu and vv. The sum u+vu + v is the diagonal of the parallelogram; the copy of vv starting at the tip of uu shows "tip to tail". The short segment along vv is the projection of uu onto vv. Try to make the projection vanish: that happens exactly when u⋅v=0u \cdot v = 0.

Vector addition and projection

A point moves along the curve as t changes: x and y are both functions of t.

uvu + v
  • u
  • v
  • u + v
  • v, moved to the tip of u
  • projection of u onto v
  • (0.25, 0.125)
  • (0.063, 0.25)
  • (0.313, 0.375)
  • (2.063, 1.25)
  • (0.044, 0.176)

The complex plane is another picture of R2\R^2: the complex number a+bia + bi is the vector (a,b)(a, b), and adding complex numbers is adding vectors. Drag zz and ww and watch the parallelogram.

Addition is the parallelogram rule

Drag z and w. Adding puts arrows end to end; multiplying multiplies lengths and adds angles.

zwz + w

Drag the points, or focus one with Tab and use the arrow keys (hold Shift for small steps).

z =+i
w =+i
NumberModulusArgument
z2 + i2.23626.6°0.464 rad
w0.5 + 2i2.06276°1.326 rad
z + w2.5 + 3i3.90550.2°0.876 rad

z + w = (2 + 0.5) + (1 + 2)i = 2.5 + 3i

Add the real parts and the imaginary parts separately: z + w is the far corner of the parallelogram built on z and w.

Exercises

Exercises

1. Vectors and coordinate geometry

Warm-upHuman

Find the length of the vector v=(2,−3,6)v = (2, -3, 6).

2. Vectors and coordinate geometry

Warm-upHuman

Compute (1,2,3)⋅(4,−5,6)(1, 2, 3) \cdot (4, -5, 6).

3. Vectors and coordinate geometry

CoreHuman

For which number kk is (2,k)(2, k) perpendicular to (3,−6)(3, -6)?

4. Vectors and coordinate geometry

CoreHuman

Find the angle, in degrees, between u=(1,1,0)u = (1, 1, 0) and v=(1,0,1)v = (1, 0, 1).

5. Vectors and coordinate geometry

CoreHuman

Let u,v,wu, v, w be vectors in R3\R^3 and tt a number. Which of these expressions are vectors? (Choose all that apply.)

Choose all that apply

6. Vectors and coordinate geometry

CoreHuman

For non-zero vectors uu and vv, when is ∣u+v∣=∣u∣+∣v∣|u + v| = |u| + |v|?

Choose one

7. Vectors and coordinate geometry

CoreHuman

At which value of tt does the line (1,0,0)+t (1,2,2)(1, 0, 0) + t\,(1, 2, 2) meet the plane x+y+z=6x + y + z = 6?

8. Vectors and coordinate geometry

StretchHuman

In the vector proof that the diagonals of a parallelogram ABCDABCD bisect each other, which equation expresses 'ABCDABCD is a parallelogram' (with vertices in this order)?

Choose one

9. Vectors and coordinate geometry

StretchFormal

In Mathlib, ⟪u, v⟫ is the dot product and ‖u‖ the length. Which statement is the Cauchy–Schwarz inequality?

Choose one

10. Vectors and coordinate geometry

StretchComputational

Write projection(u, v) that returns the projection of uu onto a non-zero vector vv, u⋅vv⋅v v\dfrac{u\cdot v}{v \cdot v}\,v, as a list of Fractions (vectors are lists of integers of the same length, in any dimension). Raise ValueError if vv is the zero vector.

Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.
Common misconceptionsH

Common misconceptions

Misconception

"A vector has a fixed starting point." A vector records only a length and a direction. The arrow from (0,0)(0, 0) to (3,4)(3, 4) and the arrow from (5,−1)(5, -1) to (8,3)(8, 3) are the same vector (3,4)(3, 4). (A point has a fixed location; the vector AB→=B−A\overrightarrow{AB} = B - A is the move from one point to another.)

Misconception

"The dot product of two vectors is a vector." It is a number (a scalar): (1,2)⋅(3,4)=11(1, 2) \cdot (3, 4) = 11. So expressions such as (u⋅v)⋅w(u \cdot v) \cdot w with a dot between a number and a vector make no sense; (u⋅v) w(u \cdot v)\,w, a number times a vector, does. The product that gives a vector in R3\R^3 is the cross product, a different operation.

Misconception

"∣u+v∣=∣u∣+∣v∣|u + v| = |u| + |v|." Only when uu and vv point in the same direction (or one is zero). In general ∣u+v∣≤∣u∣+∣v∣|u + v| \le |u| + |v|: going via a detour is never shorter. For u=(3,0)u = (3, 0), v=(0,4)v = (0, 4): 5<75 \lt 7.

Misconception

"Perpendicular means the dot product is 11" (or "parallel means 00"). It is the other way round: perpendicular vectors have u⋅v=0u \cdot v = 0, and for parallel vectors ∣u⋅v∣=∣u∣ ∣v∣|u \cdot v| = |u|\,|v|, the largest possible value.

Misconception

"Two lines that never meet are parallel." True in the plane, false in space: skew lines never meet and are not parallel. Parallel means the directions are multiples of each other.

ViewpointsH

Many ways of seeing

Algebra— A vector is anything you can add and scale

The rules we used — u+v=v+uu + v = v + u, (u+v)+w=u+(v+w)(u + v) + w = u + (v + w), t(u+v)=tu+tvt(u + v) = tu + tv, and so on — are the axioms of a vector space. Lists of numbers satisfy them, but so do polynomials, matrices, solutions of the equation y′′+y=0y'' + y = 0, and many more. Linear algebra studies all of these at once, together with the maps between them that respect addition and scaling (see linear algebra). Our proofs of Cauchy–Schwarz and the triangle inequality used only the rules of the dot product, so they hold in every inner product space.

Analysis— Dot products of functions

On the space of continuous functions on [0,1][0, 1], the formula f⋅g=∫01f(x)g(x) dxf \cdot g = \int_0^1 f(x)g(x)\,dx obeys all the rules of a dot product. Cauchy–Schwarz then says

(∫01fg dx)2≤∫01f2 dx∫01g2 dx,\left(\int_0^1 f g\,dx\right)^2 \le \int_0^1 f^2\,dx \int_0^1 g^2\,dx,

which is how Viktor Bunyakovsky (1859) and Hermann Schwarz (1880s) found it, after Augustin-Louis Cauchy's version for sums (1821). In this space, sin⁡(2πmx)\sin(2\pi m x) and sin⁡(2πnx)\sin(2\pi n x) are perpendicular for m≠nm \ne n, and Fourier series are expansions in perpendicular directions — exactly like coordinates (see functional analysis).

Probability— Random directions in many dimensions

As the experiment showed, two random unit vectors in Rn\R^n have cos⁡θ\cos\theta with mean 00 and variance exactly 1/n1/n. Since cos⁡θ\cos\theta is distributed like the first coordinate x1x_1 of a random point on the unit sphere, and x12+⋯+xn2=1x_1^2 + \cdots + x_n^2 = 1 with all coordinates equally distributed, each xi2x_i^2 averages to 1/n1/n. So in high dimensions almost all pairs of directions are nearly perpendicular. Statisticians meet this whenever they compare many measurements at once; correlation coefficients are cosines of angles between data vectors.

Physics— Forces, velocities and work

Newton's laws are vector equations: forces add as vectors (the "parallelogram of forces"), and F=maF = ma holds component by component. The work done by a constant force FF along a displacement dd is F⋅dF \cdot d, and the power delivered to a body moving with velocity vv is F⋅vF \cdot v; perpendicular forces, like the pull of a string on a whirling stone, do no work. The cross product measures turning effects (torque) and oriented areas. Differential forms generalise both products to any dimension (see differential forms and classical mechanics).

History & global context

History and global context

  • Coordinates. René Descartes (La Géométrie, 1637) and Pierre de Fermat connected geometry with algebra by describing points with numbers.
  • Parallelograms of forces and velocities. The rule for combining motions appears in ancient Greek mechanics; Simon Stevin (1586) used a parallelogram (or triangle) of forces, and Isaac Newton stated the parallelogram rule for forces as the first corollary to his laws of motion in the Principia (1687).
  • Algebras of directed quantities. Caspar Wessel (1799) and Jean-Robert Argand (1806) represented complex numbers as arrows in the plane. William Rowan Hamilton invented quaternions in 1843 for rotations in space, and Hermann Grassmann's Ausdehnungslehre (1844) developed vectors in any number of dimensions — far ahead of its time.
  • Vector analysis. In the 1880s Josiah Willard Gibbs in the USA and Oliver Heaviside in Britain independently extracted the dot and cross products from quaternions, creating the notation physicists and engineers still use.
  • Inequalities. Augustin-Louis Cauchy proved the inequality for sums in 1821; Viktor Bunyakovsky (1859) and Hermann Schwarz (1880s) proved it for integrals. In Russia and elsewhere it is often called the Cauchy–Bunyakovsky inequality.
  • Abstraction. Giuseppe Peano gave axioms for vector spaces in 1888, and in the 20th century David Hilbert's work on infinite-dimensional spaces made vectors a language for analysis, quantum mechanics and data science.
Further reading

Further reading

  • Tom M. Apostol, Calculus, Volume 1 (1967), chapters 12–13 — vector algebra and its applications to analytic geometry.
  • Gilbert Strang, Introduction to Linear Algebra (1993; later editions) — vectors, dot products and much more, with a geometric emphasis.
  • Michael J. Crowe, A History of Vector Analysis (1967) — from Hamilton and Grassmann to Gibbs and Heaviside.