Skip to main content
Back to module
Calculus & Linear Algebra for STEM · Vectors and Matrices

Vectors

Reading40mLesson 1 of 3 in this module

Add and scale vectors, measure their length, and use the dot product to find angles and test for perpendicularity.

Why this matters

A single number cannot describe a force acting at an angle, a wind velocity, the colour of a pixel or a customer's purchase history. A vector can. Structural engineers add force vectors to check that a joint balances, physicists resolve velocities into components, and data scientists treat every row of a dataset as a vector so they can measure how similar two rows are. Linear algebra, the second half of this course, is built on vectors.

What a vector is

A vector is an ordered list of numbers. Geometrically, in 2D or 3D it is an arrow with a length and a direction; in data work it can have hundreds of entries.

v = (3, 4)          a 2D vector: 3 right, 4 up
F = (120, −40, 0)   a force in newtons along x, y, z

The numbers are the vector's components. An arrow's position does not matter, only its length and direction.

Adding and scaling

  • Addition works component by component: (3, 4) + (1, −2) = (4, 2). Geometrically, place the arrows tip to tail.
  • Scalar multiplication stretches the vector: 2 · (3, 4) = (6, 8). A negative scalar flips its direction.

Worked example: two cables pull on a ring with forces (300, 400) N and (−100, 200) N. The resultant is (200, 600) N. A third force of (−200, −600) N would balance the ring, so the sum would be the zero vector. That is the basic check of statics.

Length (magnitude)

By Pythagoras, the length of v = (v₁, v₂, …, vₙ) is:

|v| = √(v₁² + v₂² + … + vₙ²)

|(3, 4)| = √(9 + 16) = 5. Dividing a vector by its length gives a unit vector (length 1) pointing the same way: (0.6, 0.8).

The dot product

The dot product multiplies matching components and adds them up:

u · v = u₁v₁ + u₂v₂ + … + uₙvₙ
(1, 2, 3) · (4, 5, 6) = 4 + 10 + 18 = 32

It turns two vectors into one number, and that number has a geometric meaning:

u · v = |u| · |v| · cos θ

where θ is the angle between them. So:

  • u · v > 0: they point roughly the same way (θ < 90°).
  • u · v = 0: they are perpendicular (orthogonal).
  • u · v < 0: they point roughly in opposite directions.

Rearranging gives the angle: cos θ = (u · v) / (|u| |v|). Also notice that v · v = |v|², so length comes from the dot product too.

Where the dot product shows up

  • Work in physics: W = F · d. Pushing at an angle only counts the component of force along the motion.
  • Projections: the component of u along a unit vector e is u · e. Engineers use it to resolve loads along a member.
  • Similarity in data: cosine similarity, (u · v) / (|u| |v|), compares documents, user profiles and embeddings in search and recommendation systems.
  • Weighted sums: a linear model's prediction w₁x₁ + w₂x₂ + … is the dot product w · x.

In code

Plain Python handles small vectors well:

import math

def dot(u, v):
    return sum(a * b for a, b in zip(u, v))

def length(v):
    return math.sqrt(dot(v, v))

print(dot([1, 2, 3], [4, 5, 6]))   # 32

With NumPy, numpy.array([1, 2, 3]) @ numpy.array([4, 5, 6]) or numpy.dot does the same thing, fast, for millions of entries.

Common mistakes

  • Adding vectors of different sizes. Both must have the same number of components.
  • Treating the dot product as a vector. It is a single number. (The cross product, used for torque in 3D, is a vector.)
  • Forgetting to normalise before reading cos θ; divide by both lengths.
  • Mixing units. Do not add a force vector in newtons to one in kilonewtons.

Quick recap

  • A vector is an ordered list of components: an arrow with length and direction.
  • Add component-wise; scale by multiplying every component.
  • Length is √(v · v); a unit vector has length 1.
  • u · v = Σ uᵢvᵢ = |u||v|cos θ: zero means perpendicular.

Try it

In the Do tab, write the dot product and use it to find lengths and angles.

Also in this lesson: