The dot product
The dot product of two vectors multiplies matching components and adds the results. It turns two vectors into one number, a scalar. Both vectors must have the same number of components.
For and , the dot product is .
Rule: Dot product
Multiply component of by component of , for every , and add. The is the sum notation of lesson 0.2.2. The answer is a number, not a vector.
A weighted sum
The dot product is a weighted sum: each component of one vector weights the matching component of the other. The shop of lesson 3.1.1 holds stock . Its four products sell at prices dollars each.
| product | 1 | 2 | 3 | 4 | total |
|---|---|---|---|---|---|
| stock | 12 | 0 | 7 | 30 | |
| price ($) | 2.50 | 4 | 10 | 0.80 | |
| ($) | 30 | 0 | 70 | 24 | 124 |
So : the stock is worth $124. The units multiply, items times dollars per item, giving dollars. Case study 3.1.4 uses the same weighted sum for a portfolio's return.
Its rules
The dot product follows rules much like ordinary multiplication. For vectors , , of the same size and a scalar :
- Order does not matter: .
- Scalars come out: . So .
- It spreads over sums: .
- A vector with itself: , never negative. For it is 10.
Try it
In NumPy the dot product is u @ v, or np.dot(u, v). Both print 5.
u * v is something else: the vector of products, [3 2]. Adding its components gives the dot product. The last line is the stock's value.
Length
The length of a vector, written , is the length of its arrow. In the plane the components are the legs of a right-angled triangle. So Pythagoras' theorem (lesson 1.7.1) gives .
The sum of squares under the root is . So the length of is , and the length of is . Lesson 1.7.2 measured the modulus of a complex number the same way.
In Pythagoras applies twice and gives the same formula: has length . In the formula is the definition of length.
Rule: Length
The length is in the same units as the components. It is never negative, and it is 0 only for the zero vector.
Squares remove signs, so also has length 5. Scaling by multiplies the length by , the size of (lesson 0.3.3). So , and .
Distance between two points
The arrow from the tip of to the tip of is (lesson 3.1.1). Its length is the distance between the two points. For and it is .
Try it
np.linalg.norm computes the length. The first line prints it for , next to .
The loop measures the other examples, then the last line gives the distance between the tips of and .
Unit vectors
A unit vector has length 1. Dividing a non-zero vector by its length gives the unit vector pointing the same way; this is called normalising it.
has length 5, so its unit vector is . For it is .
The zero vector has length 0 and no direction, so it has no unit vector: the division would be by 0.
Rule: Unit vector
A vector of length 1 pointing the same way as . In the plane every unit vector is for some angle : a point on the unit circle of lesson 1.7.1.
The unit vectors along the axes are and . A dot product with them picks out a component: and .
Try it
The first line normalises and , then checks that the second unit vector has length 1, after rounding.
The last line picks out the two components of with and .
The angle between two vectors
The dot product measures how far two vectors point the same way. Take two unit vectors on the unit circle, at angles and (alpha and beta) from the positive horizontal axis. Their dot product is the cosine of the angle between them, :
| dot product | |||
|---|---|---|---|
| 0° | 60° | 0.5 | 0.5 |
| 30° | 75° | 0.707107 | 0.707107 |
| 20° | 140° | ||
| 50° | 140° | 0 | 0 |
Any non-zero vector is its length times a unit vector, and scalars come out of a dot product. So , where (theta) is the angle between and .
Rule: Angle between two vectors
For non-zero and . The angle is taken between 0 and , that is 0° to 180°, which is what math.acos returns (lesson 1.7.1).
Example 1. For and , the dot product is 5 and the lengths are and :
That is the cosine of 45°, so .
Example 2. In , take and . The dot product is 1 and both lengths are , so and . With more than three components there is no picture, and the formula defines the angle.
The two extremes: points the same way as , with cosine 1 and angle 0°. points the opposite way, with cosine and angle 180°. The angle is always taken the short way round, and it does not say which vector is clockwise from the other.
Try it
The loop checks the table: two unit vectors at angles and , their dot product, then .
Then the cell prints the cosine of Example 1. angle is the rule above: np.clip keeps the cosine inside , for a reason given below, and np.degrees converts the radians from np.arccos.
The sign of the dot product
Lengths are positive, so has the sign of . The sign alone says whether the angle is below, at or above 90°.
- Positive: acute, below 90°. , and the angle is 45°.
- Zero: a right angle. , and the angle is 90°.
- Negative: obtuse, above 90°. , and the angle is 135°.
against three vectors of the same length, . The dot product is positive for an acute angle, zero for a right angle and negative for an obtuse one.
Drag the tip of round and watch the sign of change as the angle passes 90°. Then make a multiple of : the angle is 0° or 180°, and is as large as the two lengths allow. The dark arrow is the shadow of , explained at the end of this lesson.
Explore
Drag the tips of u and v. Watch the sign of u · v as the angle passes 90°. The dark arrow is the projection of v on u: the shadow v casts on the line through u.
- u
- (3, 1), length 3.1623
- v
- (1, 2), length 2.2361
- u · v
- 3 × 1 + 1 × 2 = 5
- Angle
- cos θ = 0.7071, so θ = 45°
- Projection of v on u
- (1.5, 0.5), signed length u · v / |u| = 1.5811
- So
- The dot product is positive, so the angle is less than 90°.
Orthogonal vectors
Two vectors are orthogonal when their dot product is 0. For non-zero vectors that means they meet at a right angle; "perpendicular" is the everyday word.
- and are orthogonal. In general, turned a quarter turn is (lesson 1.7.2), and .
- In , , so they are orthogonal too.
- The axes are orthogonal: .
Rule: Orthogonal vectors
and are orthogonal. A dot product is far easier to check than an angle, in any number of dimensions. The zero vector is orthogonal to every vector, though it has no angle.
Module 3.5 builds projections and least squares on this one idea.
Try it
Each dot product here is 0: with , its quarter turn; the pair in ; and with .
The Cauchy–Schwarz inequality
In the plane, , and a cosine lies between and 1. So the size of a dot product never exceeds the product of the two lengths. This is the Cauchy–Schwarz inequality; it holds in every , and is stated here without proof.
Rule: Cauchy–Schwarz inequality
Equality holds exactly when one vector is a multiple of the other. In it is why the ratio in the angle rule always lies in , so every pair of non-zero vectors has an angle.
For and : . For and the two sides are equal: .
Try it
The loop draws 10,000 random pairs of vectors in and records the largest . It never reaches 1; the largest is 0.995.
rng is a random number generator with a fixed seed, so every run gives the same numbers.
Two rounding traps
Computers round every decimal, and two things can go wrong. For vectors pointing the same way, the computed cosine can come out a hair above 1. Then math.acos stops with an error, and np.arccos returns nan, "not a number".
The fix is to clip the cosine into first, with np.clip(cos, -1, 1). The second trap is the reverse: a dot product that should be 0 can come out as a tiny number such as . Test orthogonality with a tolerance, not with == 0.
Try it
This cell uses plain Python arithmetic, so its last digits are the same on every computer. For and , the cosine prints as 1.0000000000000002, and math.acos raises ValueError.
The last lines test two orthogonal vectors: the dot product is not exactly 0, but it is within of it.
The shadow of one vector on another
Shine a light straight down onto the line through . The shadow of on that line has signed length , which is . It is positive when the shadow falls along and negative when it falls behind the origin.
For it is , the same as . For , at 135° to , it is . For a vector orthogonal to the shadow is a single point, of length 0.
The shadow of a vector on the line through , cut off by a drop at right angles. Its signed length is : 1.58 for , and for , which points away from .
The shadow is also called the projection of on . Lesson 3.5.2 develops it: the shadow as a vector, and the nearest point on a line or a plane.
Try it
Each line prints the signed length of the shadow on : for , for and for the orthogonal .
The second value on the first line is , the same number.
Common mistakes
- The dot product is a number. In NumPy,
u * vis the vector of products,[3 2]; the dot product isu @ v. - Length squares the components. , not . And has length 5 too: the squares remove the signs.
- Scaling by a negative number does not give a negative length. .
- The zero vector has no direction. It has no unit vector and no angle with anything.
- Divide by both lengths. The cosine is ; the dot product 5 on its own is not a cosine.
np.arccosgives radians. Convert withnp.degrees, and clip the cosine into first.- The shadow of on divides by , the length of the vector it falls on, not .
- Test for 0 with a tolerance. A dot product that should be 0 may compute as about .
The dot product, length and angle
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- , a number: a weighted sum. It is symmetric, scalars come out, and it spreads over sums. In NumPy,
u @ v. - , by Pythagoras; ; the distance between two points is .
- A unit vector has length 1; normalises a non-zero .
- , with from 0° to 180°. A positive dot product means acute, zero a right angle, negative obtuse.
- Orthogonal means .
- Cauchy–Schwarz: , so the cosine stays in . Clip it before
arccos. - The shadow of on the line through has signed length .