Vectors and magnitude
A 2D vector is just an ordered pair (x, y). It has a length, called the magnitude:
|v| = √(x² + y²)
This is the Pythagorean theorem in disguise. A 3D vector works the same way with an extra z term.
The dot product
The dot product of two vectors tells you how much they point in the same direction. Multiply matching coordinates and add the results:
u · v = x₁x₂ + y₁y₂
If the dot product is zero, the vectors are perpendicular. If it is positive and large, the vectors point the same way. This one idea is behind lighting in computer graphics and similarity in machine learning.
Matrices and multiplication
A matrix is a grid of numbers. To multiply a matrix by a vector, take the dot product of each matrix row with the vector. The result is a new vector.
For a 2x2 matrix and 2D vector:
[a b; c d] · [x; y] = [ax + by; cx + dy]
Matrices are like functions: you put a vector in, and a transformed vector comes out.
Key idea: Matrix multiplication is a compact way to do many dot products at once. It turns geometry into arithmetic.
Rotation in 2D
A rotation matrix rotates a point by an angle θ around the origin:
R(θ) = [cos θ, -sin θ; sin θ, cos θ]
If you multiply this matrix by a vector (x, y), the new vector is the original one rotated by θ. This is exactly how game engines spin objects on screen.
Interactive vector transformer
Enter a 2D vector and an angle in degrees. The tool will draw the original vector and the rotated vector, and show each step of the matrix multiplication.
Matrices and vectors in Python
Python can represent vectors as lists and matrices as lists of lists. The code below shows magnitude, dot product, and rotation.
import math def magnitude(v): return math.sqrt(sum(x**2 for x in v)) def dot(u, v): return sum(ui * vi for ui, vi in zip(u, v)) def matrix_vector_multiply(M, v): return [dot(row, v) for row in M] def rotate_2d(v, degrees): theta = degrees * math.pi / 180 c = math.cos(theta) s = math.sin(theta) R = [[c, -s], [s, c]] return matrix_vector_multiply(R, v) v = [2, 1] print("magnitude", magnitude(v)) print("rotated", rotate_2d(v, 45))
magnitude 2.23606797749979 rotated [0.7071067811865474, 2.1213203435596424]
Notice how rotate_2d builds the rotation matrix from the angle, then reuses the generic matrix-vector multiply. Composition is the whole point of matrices.
Quick quiz
1. What does the dot product u · v measure?
2. How do you multiply a 2x2 matrix by a 2D vector?
3. What does the matrix [cosθ -sinθ; sinθ cosθ] do?
Keep learning
You have used code to explore algebra, geometry, trigonometry, calculus, statistics, sequences, exponents, and linear algebra. These tools are the foundation of data science, graphics, and AI.
Part of the “Coding for High School Math” series.