Lists and arrows
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.
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.
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.
Length and direction
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.
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.
The 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.
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°.
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.
Graphics and ML
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.
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.
Try it in the Python prompt
;; step 1/10 · A vector is a list >>> a = [4, 3] >>> a[0], a[1] # the x and y components (4, 3) >>> len(a) # a 2-dimensional vector 2
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.