TExcelTAD
Home › Calculus Essentials › Vectors

Vectors

A vector is a quantity with both a size and a direction, such as a velocity, a force or the step from one point to another. This lesson covers components, addition, scaling, length and unit vectors, and shows how NumPy arrays make all of it one-liners.

Arrows and components

A scalar is a single number (a temperature, a mass). A vector has a magnitude (length) and a direction. You can draw it as an arrow, but for computing you write it as a list of components:

v=⟨3,4⟩a 2D vector: 3 along x, 4 along yw=⟨1,−2,5⟩a 3D vectorv=3i+4jthe same 2D vector with unit vectors i=⟨1,0⟩, j=⟨0,1⟩\begin{aligned} v &= \langle 3, 4 \rangle && \text{a 2D vector: 3 along } x, \text{ 4 along } y \\ w &= \langle 1, -2, 5 \rangle && \text{a 3D vector} \\ v &= 3i + 4j && \text{the same 2D vector with unit vectors } i = \langle 1, 0 \rangle,\ j = \langle 0, 1 \rangle \end{aligned}

A vector has no fixed position. The arrow from (1,1)(1, 1) to (4,5)(4, 5) and the arrow from (0,0)(0, 0) to (3,4)(3, 4) are the same vector ⟨3,4⟩\langle 3, 4 \rangle. The vector from point AA to point BB is B−AB - A, the displacement. A position vector is the special case that starts at the origin.

The basic operations

Operation Formula (2D) Meaning
Addition ⟨a1,a2⟩+⟨b1,b2⟩=⟨a1+b1,a2+b2⟩\langle a_1, a_2 \rangle + \langle b_1, b_2 \rangle = \langle a_1 + b_1, a_2 + b_2 \rangle tip-to-tail: walk aa, then walk bb
Subtraction b−a=⟨b1−a1,b2−a2⟩b - a = \langle b_1 - a_1, b_2 - a_2 \rangle the arrow from aa's tip to bb's tip
Scalar multiple c⋅⟨a1,a2⟩=⟨c⋅a1,c⋅a2⟩c \cdot \langle a_1, a_2 \rangle = \langle c \cdot a_1, c \cdot a_2 \rangle stretch by cc; a negative cc flips direction
Magnitude ∥a∥=a12+a22\lVert a \rVert = \sqrt{a_1^2 + a_2^2} length (Pythagoras)
Unit vector a^=a/∥a∥\hat{a} = a / \lVert a \rVert same direction, length 1

In 3D every formula gets a third component, and the magnitude becomes a12+a22+a32\sqrt{a_1^2 + a_2^2 + a_3^2}.

NumPy arrays add and scale component-wise, so the table translates directly:

python · try it yourself
import numpy as np

a = np.array([3.0, 4.0])
b = np.array([-1.0, 2.0])

print("a + b    =", a + b)
print("b - a    =", b - a)
print("2.5 * a  =", 2.5 * a)
print("|a|      =", np.linalg.norm(a))
print("unit a   =", a / np.linalg.norm(a))
print("|unit a| =", np.linalg.norm(a / np.linalg.norm(a)))
a + b    = [2. 6.]
b - a    = [-4. -2.]
2.5 * a  = [ 7.5 10. ]
|a|      = 5.0
unit a   = [0.6 0.8]
|unit a| = 1.0

Note: Normalising the zero vector divides by zero. Real code checks the length first, or adds a tiny epsilon, before dividing.

Distance, direction and midpoints

Three questions come up constantly: how far apart are two points, which way does one lie from the other, and where is the point a fraction tt of the way between them? All three use the displacement d=B−Ad = B - A:

distance=∥B−A∥direction=(B−A)/∥B−A∥point at t=A+t⋅(B−A)t=0 gives A, t=1 gives B, t=0.5 the midpoint\begin{aligned} \text{distance} &= \lVert B - A \rVert \\ \text{direction} &= (B - A) / \lVert B - A \rVert \\ \text{point at } t &= A + t \cdot (B - A) && t = 0 \text{ gives } A,\ t = 1 \text{ gives } B,\ t = 0.5 \text{ the midpoint} \end{aligned}

The last line is linear interpolation (lerp). It is also the vector equation of the line through AA and BB: as tt runs over all real numbers, A+t⋅dA + t \cdot d sweeps out the whole line.

python · try it yourself
import numpy as np

A = np.array([2.0, -1.0, 3.0])
B = np.array([8.0, 1.0, 6.0])

d = B - A
dist = np.linalg.norm(d)
print("displacement:", d)
print("distance    :", dist)
print("direction   :", np.round(d / dist, 4))
for t in [0.0, 0.25, 0.5, 1.0]:
    print(f"t = {t:4}: {A + t * d}")
displacement: [6. 2. 3.]
distance    : 7.0
direction   : [0.8571 0.2857 0.4286]
t =  0.0: [ 2. -1.  3.]
t = 0.25: [ 3.5  -0.5   3.75]
t =  0.5: [5.  0.  4.5]
t =  1.0: [8. 1. 6.]

The distance is a whole number because 62+22+32=49=726^2 + 2^2 + 3^2 = 49 = 7^2.

Vectors in calculus

In calculus, vectors carry rates of change that have a direction. The velocity of a moving point is a vector, its acceleration is a vector, and the gradient of a function of several variables is a vector pointing uphill. The later lessons of this section (motion in space, curves) are about vectors that change with time.

In software

  • Games and graphics. Positions, velocities, forces, surface normals and light directions are all 3-vectors. The core game-loop step position += velocity * dt is vector addition and scaling. See Vectors and Length and Normalize in Game Math.
  • Machine learning. A data row with 20 features is a 20-dimensional vector, and so is a word embedding with 768 components. The same formulas for length and distance work in any number of dimensions.
  • NumPy. Element-wise arithmetic and np.linalg.norm (see NumPy Linear Algebra) do these operations on millions of vectors at once.
python · try it yourself
import numpy as np

pos = np.array([0.0, 10.0])      # metres
vel = np.array([4.0, 0.0])       # metres per second
gravity = np.array([0.0, -9.8])
dt = 0.1

for step in range(3):
    vel = vel + gravity * dt
    pos = pos + vel * dt
    print(f"step {step + 1}: pos = {np.round(pos, 3)}, speed = {np.linalg.norm(vel):.3f}")
step 1: pos = [0.4   9.902], speed = 4.118
step 2: pos = [0.8   9.706], speed = 4.454
step 3: pos = [1.2   9.412], speed = 4.964

Each frame adds a small vector to the velocity and another to the position. That is the semi-implicit Euler integrator covered in Physics Integration in Game Loops.

Exercises

Exercise 1: Combine two vectors

Let u=⟨2,−3⟩u = \langle 2, -3 \rangle and v=⟨−4,1⟩v = \langle -4, 1 \rangle. Compute 3u−2v3u - 2v and its magnitude.

Answer

3u−2v=⟨14,−11⟩3u - 2v = \langle 14, -11 \rangle with magnitude 317≈17.80\sqrt{317} \approx 17.80.

Solution
3u=⟨6,−9⟩2v=⟨−8,2⟩3u−2v=⟨6−(−8),−9−2⟩=⟨14,−11⟩∥⟨14,−11⟩∥=196+121=317≈17.804\begin{aligned} 3u &= \langle 6, -9 \rangle \\ 2v &= \langle -8, 2 \rangle \\ 3u - 2v &= \langle 6 - (-8), -9 - 2 \rangle = \langle 14, -11 \rangle \\ \lVert \langle 14, -11 \rangle \rVert &= \sqrt{196 + 121} = \sqrt{317} \approx 17.804 \end{aligned}

Exercise 2: Unit vector

Find the unit vector in the direction of w=⟨2,−1,2⟩w = \langle 2, -1, 2 \rangle.

Answer

w^=⟨2/3,−1/3,2/3⟩\hat{w} = \langle 2/3, -1/3, 2/3 \rangle.

Solution

∥w∥=4+1+4=9=3\lVert w \rVert = \sqrt{4 + 1 + 4} = \sqrt{9} = 3, so w^=w/3=⟨2/3,−1/3,2/3⟩\hat{w} = w / 3 = \langle 2/3, -1/3, 2/3 \rangle. Check: (4+1+4)/9=1(4 + 1 + 4)/9 = 1, so its length is 1.

Exercise 3: A point one third of the way

Find the point one third of the way from P=(1,5,−2)P = (1, 5, -2) to Q=(7,−1,4)Q = (7, -1, 4), and the distance PQPQ.

Answer

The point is (3,3,0)(3, 3, 0). The distance is 63≈10.3926\sqrt{3} \approx 10.392.

Solution
d=Q−P=⟨6,−6,6⟩P+(1/3)⋅d=(1+2,5−2,−2+2)=(3,3,0)∥d∥=36+36+36=108=63≈10.392\begin{aligned} d &= Q - P = \langle 6, -6, 6 \rangle \\ P + (1/3) \cdot d &= (1 + 2, 5 - 2, -2 + 2) = (3, 3, 0) \\ \lVert d \rVert &= \sqrt{36 + 36 + 36} = \sqrt{108} = 6\sqrt{3} \approx 10.392 \end{aligned}
python · try it yourself
import numpy as np
P, Q = np.array([1, 5, -2]), np.array([7, -1, 4])
print(P + (Q - P) / 3, round(float(np.linalg.norm(Q - P)), 3))
[3. 3. 0.] 10.392

Exercise 4: Fixed-speed movement (code)

An enemy at (0,0)(0, 0) chases a player at (30,40)(30, 40) at 5 units per second. Write a function step(enemy, player, speed, dt) that returns the enemy's new position after one frame, and use it to find where the enemy is after 1 second (one step with dt=1dt = 1).

Answer

(3,4)(3, 4): it moves 5 units along the direction ⟨0.6,0.8⟩\langle 0.6, 0.8 \rangle.

Solution

Normalise the displacement to get a direction, then scale it by speed * dt. Clamping to the remaining distance stops the enemy from overshooting.

python · try it yourself
import numpy as np

def step(enemy, player, speed, dt):
    d = player - enemy
    dist = np.linalg.norm(d)
    if dist < 1e-9:
        return enemy
    move = min(speed * dt, dist)
    return enemy + d / dist * move

print(step(np.array([0.0, 0.0]), np.array([30.0, 40.0]), 5.0, 1.0))
[3. 4.]

Test yourself

What is the magnitude of ⟨1,2,2⟩\langle 1, 2, 2 \rangle?

  1. 5
  2. 3
  3. 5\sqrt{5}
  4. 9

Which expression gives the vector from point AA to point BB?

  1. A−BA - B
  2. A+BA + B
  3. B−AB - A
  4. (A+B)/2(A + B) / 2

What does multiplying a vector by −2 do?

  1. Doubles its length and reverses its direction
  2. Halves its length
  3. Rotates it by 90°
  4. Leaves the direction and halves the length