The dot and cross products
P84.vectors.02 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Vectors: magnitude, direction and components
Where next?
This module unlocks
Go up a level
P84.vectors.02 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Vectors: magnitude, direction and components
This module unlocks
Go up a level
Vectors add and scale; the goal of this lesson is to multiply them. There are two useful ways, and they answer different questions. The dot product of two vectors is a number that says how much they point the same way: it measures angles, shadows and the work a force does. The cross product of two vectors in space is a third vector, standing at right angles to both: it is how physics describes turning, as torque and angular momentum. You need the previous lesson's components and magnitudes, and the cosine and sine of an angle.
The dot product of u = (u₁, u₂, u₃) and v = (v₁, v₂, v₃), where u₁, u₂ and u₃ are u's first, second and third components (and the same for v), is the number
u · v = u₁v₁ + u₂v₂ + u₃v₃
(with two terms in the plane, three in space). For u = (1, 2, 3) and v = (4, 5, 6) it is 4 + 10 + 18 = 32. The answer is a scalar, not a vector.
The definition looks arbitrary until you see what it measures:
u · v = |u| × |v| × cos θ, where θ is the angle between u and v.
So the dot product is large and positive when the arrows point roughly the same way, zero when they are perpendicular (at right angles, θ = 90°, cos θ = 0), and negative when they point more than 90° apart. Turned round, it finds the angle between two vectors from their components alone:
cos θ = (u · v) ÷ (|u| × |v|)
For u = (2, 1) and v = (1, 3): u · v = 2 + 3 = 5, |u| = √5 and |v| = √10, so cos θ = 5 ÷ √50 ≈ 0.7071 and θ = 45°. And (3, 4) · (4, −3) = 12 − 12 = 0: those two are perpendicular, which you can check on squared paper.
Compute the squared length of u − v in two ways. With components, each term is (uᵢ − vᵢ)² = uᵢ² − 2uᵢvᵢ + vᵢ², so |u − v|² = |u|² + |v|² − 2(u · v).
With geometry, put the tails of u and v together, with v along the x-axis: v = (|v|, 0) and, resolving u at angle θ, u = (|u| cos θ, |u| sin θ). Then
|u − v|² = (|u| cos θ − |v|)² + (|u| sin θ)²
= |u|²(cos²θ + sin²θ) − 2|u||v| cos θ + |v|²
= |u|² + |v|² − 2|u||v| cos θ
using sin²θ + cos²θ = 1 in the middle line. The two results are the same length, so u · v = |u||v| cos θ. The length of u − v and the angle between the arrows do not depend on where the axes are, so laying v along the x-axis lost nothing.
Shine a light straight down onto the line of v, and u casts a shadow along it. The signed length of that shadow is the scalar projection of u on v:
|u| cos θ = (u · v) ÷ |v|
For u = (3, 4) and v along the x-axis, (1, 0), the shadow is 3: the x-component, as it should be. The shadow is negative when u points backwards along v.
Physics uses the shadow to measure effort. A force that moves an object along a displacement does work, and only the part of the force along the path counts:
W = F · d = |F| × |d| × cos θ
Pull a sledge 5 m with a force of 10 newtons along a rope at 60° to the ground, and the work done is 10 × 5 × cos 60° = 25 joules (a newton is the unit of force, a joule the unit of work). In components, F ≈ (5, 8.660) and d = (5, 0), and F · d = 25: the upward part of the pull does no work along the ground.
In space there is a second product. The cross product of u and v is the vector
u × v = (u₂v₃ − u₃v₂, u₃v₁ − u₁v₃, u₁v₂ − u₂v₁)
For u = (1, 2, 3) and v = (4, 5, 6) it is (12 − 15, 12 − 6, 5 − 8) = (−3, 6, −3). Mind the sign ×. Between two numbers, as in |u| × |v| × cos θ above, it is ordinary multiplication, and a number times a vector is written side by side, as in 2(3, 4) = (6, 8). Between two vectors, u × v is the cross product, a different operation whose answer is a vector.
Its geometry has three parts:
Order matters. v × u = −(u × v): the same length, the opposite way. Swapping u and v in the formula swaps the two products in every component, for instance v₂u₃ − v₃u₂ = −(u₂v₃ − u₃v₂), so every component changes sign. The cross product of a vector with itself, or with any vector along the same line, is (0, 0, 0), since sin 0 = 0.
So the cross product breaks a law that multiplication of numbers obeys: a × b = b × a. One example is enough to show it, (1, 0, 0) × (0, 1, 0) = (0, 0, 1) but (0, 1, 0) × (1, 0, 0) = (0, 0, −1). That is the rule for testing any claimed law: a law that holds on every example you try is only a candidate, but one counterexample is enough to sink it.
Take the dot product of u with u × v and expand:
u · (u × v) = u₁(u₂v₃ − u₃v₂) + u₂(u₃v₁ − u₁v₃) + u₃(u₁v₂ − u₂v₁)
= u₁u₂v₃ − u₁u₃v₂ + u₂u₃v₁ − u₁u₂v₃ + u₁u₃v₂ − u₂u₃v₁ = 0
Every product appears once with a plus sign and once with a minus sign, so the sum is 0 and u × v is perpendicular to u. The same expansion with v in place of u shows v · (u × v) = 0.
Square the three components of u × v and add them. Each square gives two squared products and one cross term, for example (u₁v₂ − u₂v₁)² = u₁²v₂² + u₂²v₁² − 2u₁v₁u₂v₂. The six squared products are all the uᵢ²vⱼ² with i ≠ j. On the other side, |u|²|v|² = (u₁² + u₂² + u₃²)(v₁² + v₂² + v₃²) is all nine uᵢ²vⱼ², and (u · v)² is the three uᵢ²vᵢ² plus the same three cross terms with a plus sign. Subtracting, the uᵢ²vᵢ² cancel and the cross terms match, which gives Lagrange's identity:
|u × v|² = |u|²|v|² − (u · v)²
For u = (1, 2, 3) and v = (4, 5, 6): |u × v|² = 9 + 36 + 9 = 54, and |u|²|v|² − (u · v)² = 14 × 77 − 32² = 1078 − 1024 = 54.
Now put u · v = |u||v| cos θ into it and use cos²θ + sin²θ = 1:
|u × v|² = |u|²|v|² − |u|²|v|² cos²θ = |u|²|v|² sin²θ
The angle between two vectors lies between 0° and 180°, where sin θ is never negative, so taking square roots gives |u × v| = |u||v| sin θ.
numpy computes both products, and the magnitude:
import numpy as np
u = np.array([1, 2, 3])
v = np.array([4, 5, 6])
print(np.dot(u, v)) # 32
print(np.cross(u, v)) # [-3 6 -3]
print(np.linalg.norm(u)) # 3.7416573867739413
Push on a spanner and the bolt turns. How hard it turns depends on the force, on how far from the bolt you push, and on the angle: pushing along the handle turns nothing. The torque is
τ = r × F
where r is the position vector of the point where you push, the arrow from the bolt to that point (not a radius), F is the force, and × is the cross product of these two vectors (τ is the Greek letter tau). With a handle 0.25 m long along the x-axis and a push of 40 newtons straight across it, along the y-axis, τ = (0.25, 0, 0) × (0, 40, 0) = (0, 0, 10): a torque of 10 newton-metres, pointing along the bolt's axis. Push at 30° to the handle instead and only 40 × sin 30° = 20 newtons count, for 5 newton-metres. The direction of τ is the axis of the turning, and the right-hand rule says which way round.
Physics builds on this: angular momentum, the quantity that keeps a planet or a spinning top turning, is also a cross product, r × p, with r the position vector again and p the momentum (the mass times the velocity, a scalar times a vector). Torque is what changes it.
Loading the exercise...
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
Two coding rungs in your browser: the first uses numpy's dot and cross,
the second builds the cross product by hand and tests which laws it obeys.
Both need a little Python (Beginner Python teaches it).
Loading exercise…
Loading exercise…
Loading your notes...