Vectors
About this lesson
Vectors are the shared language of graphics and machine learning: a point on the screen, a direction toward a light, a word turned into numbers. Follow two vectors, a = [4, 3] and b = [1, 2], as the figure adds them tip to tail, scales them, measures their length, and uses the dot product to find the angle between them.
A vector is a list
A vector is an ordered list of numbers, called components. a = [4, 3] means 4 steps along x, then 3 along y: an arrow from the origin. In machine learning the numbers are features; in graphics, positions and directions.
>>> a = [4, 3] >>> a[0], a[1] # the x and y components (4, 3) >>> len(a) # a 2-dimensional vector 2
Read the lesson step by step
A vector is a list
A vector is an ordered list of numbers, called components. a = [4, 3] means 4 steps along x, then 3 along y: an arrow from the origin. In machine learning the numbers are features; in graphics, positions and directions.
>>> a = [4, 3] >>> a[0], a[1] # the x and y components (4, 3) >>> len(a) # a 2-dimensional vector 2Adding vectors
Add vectors component by component: a + b = [4 + 1, 3 + 2] = [5, 5]. On the plane, slide b so its tail sits on the tip of a. The sum runs from the origin to where b now ends.
>>> a, b = [4, 3], [1, 2] >>> [p + q for p, q in zip(a, b)] [5, 5] >>> a + b # careful: + joins two lists [4, 3, 1, 2]Scaling by a number
Multiplying by a plain number, a scalar, multiplies every component: 2b = [2 × 1, 2 × 2] = [2, 4]. The arrow keeps its direction and doubles in length. A negative scalar flips it the other way.
>>> b = [1, 2] >>> [2 * q for q in b] [2, 4] >>> [-1 * q for q in b] # a negative scalar flips it [-1, -2] >>> 2 * b # careful: * repeats a list [1, 2, 1, 2]Length from Pythagoras
The length, or norm, of a is written |a|. Its components are the two short sides of a right triangle, so Pythagoras gives |a| = √(4² + 3²) = √25 = 5. In more dimensions, square every component, add, take the root.
>>> import math >>> math.sqrt(4**2 + 3**2) 5.0 >>> a = [4, 3] >>> math.hypot(*a) 5.0 >>> math.hypot(1, 2, 2) # works in 3D too 3.0Unit vectors
Divide a vector by its length to get a unit vector: length 1, same direction. â = a ÷ 5 = [0.8, 0.6]. Graphics code normalises directions like this before it uses them, for example in lighting.
>>> import math >>> a = [4, 3] >>> n = math.hypot(*a) >>> [x / n for x in a] [0.8, 0.6] >>> math.hypot(0.8, 0.6) 1.0The dot product
The dot product multiplies matching components and adds the results: a · b = 4 × 1 + 3 × 2 = 10. Two vectors go in, one plain number comes out. It measures how much the two point the same way.
>>> import math >>> a, b = [4, 3], [1, 2] >>> sum(p * q for p, q in zip(a, b)) 10 >>> math.sumprod(a, b) # Python 3.12 and later 10 >>> math.sumprod(b, a) # order does not matter 10Dot product and angle
The dot product also equals |a| |b| cos θ, where θ is the angle between the arrows. Here |b| = √5 ≈ 2.236, so cos θ = 10 ÷ (5 × 2.236) ≈ 0.894, and θ ≈ 26.6°.
>>> import math >>> cos_t = 10 / (5 * math.hypot(1, 2)) >>> cos_t 0.8944271909999159 >>> math.degrees(math.acos(cos_t)) 26.565051177077994Zero means perpendicular
Take c = [−3, 4]. Then a · c = 4 × (−3) + 3 × 4 = 0. A zero dot product means cos θ = 0, so θ = 90°: the two arrows are perpendicular, at right angles.
>>> import math >>> math.sumprod([4, 3], [-3, 4]) 0 >>> math.degrees(math.acos(0)) 90.0Graphics: lighting a surface
Graphics shades surfaces with dot products. With unit vectors n, the way a surface faces, and l, toward the light, its brightness is max(0, n · l). n = [0, 1] and l = â = [0.8, 0.6] give 0.6: 60% lit.
>>> import math >>> n, l = [0, 1], [0.8, 0.6] >>> max(0, math.sumprod(n, l)) 0.6 >>> max(0, math.sumprod(n, [0.8, -0.6])) # light from below 0ML: cosine similarity
Machine learning turns words, images and users into vectors with hundreds of numbers. Cosine similarity, a · b ÷ (|a| |b|), scores how alike two are: a with b gives 0.894, with c gives 0, with −a gives −1.
>>> import math >>> def cos_sim(u, v): ... return math.sumprod(u, v) / (math.hypot(*u) * math.hypot(*v)) ... >>> cos_sim([4, 3], [1, 2]) 0.8944271909999159 >>> cos_sim([4, 3], [-3, 4]), cos_sim([4, 3], [-4, -3]) (0.0, -1.0)
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01What is [2, 1] + [3, 4]?
Add matching components: [2 + 3, 1 + 4] = [5, 5]. [2, 1, 3, 4] is what Python's + does to two lists, which is not vector addition.
02What is the length |v| of v = [6, 8]?
Pythagoras: √(6² + 8²) = √(36 + 64) = √100 = 10. In Python, math.hypot(6, 8) gives 10.0.
03What is the dot product [2, 3] · [4, −1]?
Multiply matching components and add: 2 × 4 + 3 × (−1) = 8 − 3 = 5. A dot product is always one plain number, never a vector.
04Two vectors, neither of them zero, have a dot product of 0. What does that tell you?
a · b = |a| |b| cos θ. With both lengths above 0, the dot product is 0 only when cos θ = 0, so θ = 90°.
05In Python, a = [1, 2]. What does 3 * a give?
For a list, * repeats it. To scale a vector write [3 * x for x in a], which gives [3, 6], or use a NumPy array, where 3 * a scales every component.
Glossary
- Vector
- An ordered list of numbers, such as [4, 3]. It can be drawn as an arrow from the origin: 4 along x, then 3 along y.
- Component
- One number in a vector. In a = [4, 3], a[0] = 4 is the x component and a[1] = 3 the y component.
- Scalar
- A plain single number. Multiplying a vector by a scalar multiplies every component, which stretches, shrinks or flips the arrow.
- Norm (length)
- How long a vector's arrow is, written |a|. For [x, y] it is √(x² + y²), by Pythagoras.
- Unit vector
- A vector of length 1, which keeps only a direction. Divide any non-zero vector by its length to get one.
- Dot product
- Multiply matching components and add: a · b = a[0] × b[0] + a[1] × b[1]. It also equals |a| |b| cos θ.
- Perpendicular (orthogonal)
- At right angles, 90° apart. Two non-zero vectors are perpendicular exactly when their dot product is 0.
- Cosine similarity
- a · b ÷ (|a| |b|), the cosine of the angle between two vectors: 1 for the same direction, 0 for perpendicular, −1 for opposite.
Notes and sources
- Books write vectors in bold (a) or with an arrow over the letter, often as a column of numbers. In real code, NumPy arrays do the maths for you: a + b, 2 * b, a @ b (the dot product) and np.linalg.norm(a) (the length).
- Every rule here works in any number of dimensions. A vector with n components has length √(x₁² + x₂² + … + xₙ²), and its dot product with another adds n products.
- The dot product of a vector with itself is its length squared: a · a = 4² + 3² = 25 = |a|². Order never matters: a · b = b · a.
- The zero vector [0, 0] has length 0 and no direction, so it cannot be turned into a unit vector: that would divide by zero.
- max(0, n · l) is Lambert’s cosine law: a surface tilted away from the light catches less of it, and a surface facing away (a negative dot product) gets none.
- Gilbert Strang, Introduction to Linear Algebra, chapter 1: Introduction to Vectors
- MIT OpenCourseWare 18.06 Linear Algebra (Strang)
- Jurafsky & Martin, Speech and Language Processing, chapter 6: Vector Semantics and Embeddings (cosine similarity)
- LearnOpenGL: Basic Lighting (diffuse light from a dot product)
- Python library: math (hypot, sumprod, acos, degrees)
- Python library: common sequence operations (+ joins lists, * repeats them)
- NumPy: numpy.linalg.norm
Quiz Unlocks after the last step.