1234512345xyθθa[0]4a[1]31a, b = [4, 3], [1, 2]2[p + q for p, q in zip(a, b)]3[2 * q for q in b]4math.hypot(*a)5math.sumprod(a, b)b[0]1b[1]2r[0]r[1]
Component rows: a[0] is x, a[1] is yArrows: a (rose), b (blue) and the result r (mint)Calculator: one plain number, such as a length or a dot productPython card: each operation in plain PythonTwo vectors drawn as arrows from the origin of a sheet of graph paper: a = [4, 3] and b = [1, 2]. Their components sit in the rows at the right, a[0] and a[1] for x and y. The mint result arrow r shows each operation: a + b = [5, 5] (b slid tip to tail), 2b = [2, 4], the unit vector â = [0.8, 0.6] on the unit circle, and c = [−3, 4] at right angles to a. The calculator adds up lengths and dot 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.

Lists and arrows

  1. 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.

Length and direction

The dot product

Graphics and ML

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.