Vectors and the dot product
A vector is an arrow and a list of numbers at the same time. The dot product is where those two views meet.
- Move freely between a vector as numbers and a vector as an arrow
- Read the dot product as a shadow, and know what its sign tells you
- Use length, unit vectors and span without memorising formulas
A vector like \((3, -2)\) is two things at once. Algebraically it is an ordered list of numbers. Geometrically it is an arrow from the origin to the point \((3, -2)\). Neither view is the “real” one; the skill is switching between them, because some questions are trivial in one and painful in the other.
v2 = [3, -2] # a 2D vector
v3 = [4, -3, 2] # a 3D vector
v3t = np.transpose(v3)
Adding and scaling
Adding vectors is adding numbers position by position. Geometrically it is walking: follow the first arrow, then the second from where you stopped.
v1 = np.array([3, -1])
v2 = np.array([2, 4])
v3 = v1 + v2 # (5, 3): tip-to-tail
Multiplying by a scalar stretches the arrow, and a negative scalar flips it round. v1 * -0.3 points the other way and is a third as long. Every arrow you can make by stretching and adding a set of vectors is called their span — for two non-parallel vectors in the plane, that is the whole plane.
The dot product
The dot product multiplies matching entries and adds them up. The notebook computes it four ways, and they all agree:
dp1 = sum(np.multiply(v1, v2)) # multiply, then sum
dp2 = np.dot(v1, v2) # the library call
dp4 = 0
for i in range(len(v1)): # the definition, written out
dp4 += v1[i] * v2[i]
That is the algebraic definition. The geometric one is
\[\mathbf{a}\cdot\mathbf{b} = \|\mathbf{a}\|\,\|\mathbf{b}\|\cos\theta\]and the useful way to read it is as a shadow: drop a perpendicular from \(\mathbf{b}\) onto the line through \(\mathbf{a}\), and the dot product is the length of that shadow times the length of \(\mathbf{a}\).
So the sign of the dot product is a direction test:
- positive — the vectors point roughly the same way (angle under 90°),
- zero — they are perpendicular,
- negative — they point roughly opposite ways.
The notebook checks the two formulas agree numerically:
ang = np.arccos(np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)))
dp_a = np.dot(v1, v2) # algebraic
dp_g = np.linalg.norm(v1) * np.linalg.norm(v2) * np.cos(ang) # geometric
Length, and unit vectors
A vector dotted with itself gives its length squared — Pythagoras in any number of dimensions:
\[\|\mathbf{v}\| = \sqrt{\mathbf{v}\cdot\mathbf{v}}\]Dividing a vector by its length gives a unit vector: same direction, length one. Unit vectors are how you talk about a pure direction, and dotting with one gives the shadow length directly.
Properties worth knowing
The notebook tests these with random vectors, and the results are worth remembering precisely:
| Property | Holds? |
|---|---|
| \(\mathbf{a}\cdot(\mathbf{b}+\mathbf{c}) = \mathbf{a}\cdot\mathbf{b} + \mathbf{a}\cdot\mathbf{c}\) (distributive) | yes |
| \(\mathbf{a}\cdot\mathbf{b} = \mathbf{b}\cdot\mathbf{a}\) (commutative) | yes |
| \(\mathbf{a}\cdot(\mathbf{b}\cdot\mathbf{c}) = (\mathbf{a}\cdot\mathbf{b})\cdot\mathbf{c}\) (associative) | no — the two sides are not even the same kind of object |
And the Cauchy–Schwarz inequality, \(\vert \mathbf{a}\cdot\mathbf{b}\vert \le |\mathbf{a}|\,|\mathbf{b}|\), is just the statement that \(\vert \cos\theta\vert \le 1\), with equality exactly when the vectors are parallel.
Other products
- Hadamard (element-wise):
np.multiply(v1, v2)— another vector, no geometry to speak of. - Outer product:
np.outer(v1, v2)— a whole matrix, every entry of one times every entry of the other. It reappears in the SVD as the building block of a rank-one matrix. - Cross product (3D only): a vector perpendicular to both inputs, whose length is the area of the parallelogram they span.
Try it yourself. In the animation, set \(\vert \mathbf{a}\vert = \vert \mathbf{b}\vert \) and watch the largest value the dot product reaches — then check it equals \(|\mathbf{a}|^2\). In the notebook, find two non-zero 3D vectors whose dot product is exactly zero, and confirm with np.cross that their cross product is perpendicular to both.