Vectors
A vector is a quantity with both size and direction, such as a displacement, a velocity or a force. This lesson defines vectors in and geometrically and by components, proves the algebraic rules they obey, and introduces length, unit vectors and the standard basis .
For a gentler first pass, see Vectors in Calculus Essentials.
Geometric and component definitions
Geometrically, a vector is a directed line segment, an arrow. Two arrows with the same length and direction represent the same vector, wherever they start. The arrow from to is written .
Put the tail at the origin and the head lands on a single point . That point's coordinates are the vector's components:
Angle brackets separate the vector from the point . For and ,
The zero vector is the only vector with no direction.
Operations
| Operation | Components | Geometry |
|---|---|---|
| tip-to-tail: the diagonal of the parallelogram on and | ||
| stretch by , reversed if | ||
| the arrow from the tip of to the tip of | ||
| the length, from the distance formula |
Two nonzero vectors are parallel if one is a scalar multiple of the other.
Properties. For all vectors and scalars :
Proof. Each identity holds component by component, because it holds for real numbers. For example, the first component of is , which is the first component of . ∎
These eight axioms are exactly the definition of a vector space. , polynomials and matrices all satisfy them, which is why linear algebra applies to all of them (Linear Algebra).
Length, unit vectors and the basis
A unit vector has length 1. Any nonzero gives the unit vector in its direction:
The scaling rule holds because . It shows that really has length 1.
The standard basis vectors are , and . Every vector is a unique combination of them:
A 2D unit vector at angle from the positive -axis is , so any 2D vector is .
import numpy as np
A, B = np.array([1.0, 4.0, -2.0]), np.array([3.0, 1.0, 4.0])
a = B - A # head minus tail
print("AB =", a, " |AB| =", float(np.linalg.norm(a)))
u = a / np.linalg.norm(a)
print("unit vector:", np.round(u, 4), " length", round(float(np.linalg.norm(u)), 12))
b = np.array([2.0, 0.0, -1.0])
print("a + b =", a + b, " 3a - 2b =", 3 * a - 2 * b)
print("|a + b| =", round(float(np.linalg.norm(a + b)), 4),
"<= |a| + |b| =", round(float(np.linalg.norm(a) + np.linalg.norm(b)), 4))AB = [ 2. -3. 6.] |AB| = 7.0
unit vector: [ 0.2857 -0.4286 0.8571] length 1.0
a + b = [ 4. -3. 5.] 3a - 2b = [ 2. -9. 20.]
|a + b| = 7.0711 <= |a| + |b| = 9.2361
The last line is the triangle inequality : one side of a triangle is never longer than the other two together. It is proved with the Cauchy–Schwarz inequality in The Dot Product.
Forces and equilibrium
Forces are vectors, and the net force on an object is their sum. An object is in equilibrium when the forces sum to . Take a 50 N lamp hanging from two cables at 40° and 55° above the horizontal, pulling left and right. With tensions and :
import numpy as np
d1, d2 = np.radians(40), np.radians(55)
M = np.array([[-np.cos(d1), np.cos(d2)], # x-components
[ np.sin(d1), np.sin(d2)]]) # y-components
rhs = np.array([0.0, 50.0]) # must cancel the weight
t1, t2 = np.linalg.solve(M, rhs)
print(f"|T1| = {t1:.3f} N, |T2| = {t2:.3f} N")
net = t1 * np.array([-np.cos(d1), np.sin(d1)]) + t2 * np.array([np.cos(d2), np.sin(d2)]) + [0, -50]
print("net force:", np.round(net, 10) + 0.0)|T1| = 28.788 N, |T2| = 38.449 N
net force: [0. 0.]
The steeper cable carries more of the weight.
In software
- Games. Positions are points, while velocities, offsets and forces are vectors. "Move toward the target at speed s" is
pos += s·dt·(target − pos)/|target − pos|. Normalising the zero vector divides by zero, the classic NaN bug when the agent already stands on its target. See Length and Normalize. - Physics engines sum force vectors every tick and integrate them (Bodies and Forces).
- ML. A data row, an image or a word embedding is a vector in , and every operation in this lesson works unchanged in dimensions.
Exercises
Exercise 1: Components and length
Find and for and , then the unit vector in the same direction.
Answer
, , unit vector .
Solution
Exercise 2: A vector with a given length
Find the vector of length 10 pointing in the direction opposite to .
Answer
Solution
, so the unit vector is . Multiply by : . Its length is by the scaling rule.
Exercise 3: Parallel or not?
Are and parallel? Are and ?
Answer
The first pair is parallel (). The second pair is not.
Solution
For the first pair, each component ratio is , so one vector is times the other. For the second pair, the ratios are 2, 2 and . They are not all equal, so no single scalar works.
Exercise 4: A hanging sign (code)
A 120 N sign hangs from two cables at 30° (pulling left) and 60° (pulling right) above the horizontal. Find the tensions. Then check the exact values 60 N and .
Answer
(the 30° cable), (the 60° cable).
Solution
import numpy as np
a1, a2 = np.radians(30), np.radians(60)
M = np.array([[-np.cos(a1), np.cos(a2)], [np.sin(a1), np.sin(a2)]])
t1, t2 = np.linalg.solve(M, [0.0, 120.0])
print(round(float(t1), 6), round(float(t2), 6), round(60 * np.sqrt(3), 6))60.0 103.923048 103.923048
Test yourself
What is the vector from to ?
If , what is ?
- −12
- 4
- 12
- 7
Which of these is a unit vector?