Latticework

Command Palette

Search for a command to run...

Linear Algebra

Vectors & Matrices

18 min

Explanation

A vector is just an ordered list of numbers — [3, 4] can represent a point, a direction, a set of feature weights, anything with multiple components. Two operations you'll use constantly:

Vector addition (component-wise): [1, 2] + [3, 4] = [4, 6]

Scalar multiplication: 2 * [1, 2] = [2, 4]

The dot product multiplies corresponding components and sums them — it's a single number (a "scalar") that captures how much two vectors point in the same direction:

def dot_product(a, b):
    return sum(x * y for x, y in zip(a, b))

dot_product([1, 2, 3], [4, 5, 6])   # 1*4 + 2*5 + 3*6 = 32

You already met this exact computation in the NumPy course (np.dot) — this course is about understanding what it means and why it works; NumPy is about computing it efficiently at scale. Both matter.

Try it

Same portfolio-return calculation as the NumPy course's linear algebra lesson — a dot product IS a weighted sum. Recognizing that pattern is the actual skill; the library call is just syntax.

Loading editor…
Explanation

A matrix is a grid of numbers — a list of rows, each row a list of numbers of the same length. [[1, 2], [3, 4]] is a 2×2 matrix. The transpose of a matrix flips it over its diagonal, turning rows into columns:

def transpose(m):
    return [[row[i] for row in m] for i in range(len(m[0]))]

transpose([[1, 2, 3], [4, 5, 6]])
# [[1, 4], [2, 5], [3, 6]]

The identity matrix (1s on the diagonal, 0s elsewhere) is the matrix equivalent of the number 1 — multiplying any matrix by it leaves the matrix unchanged, the same way multiplying any number by 1 does.

Exercise

Write `dot_product(a, b)`, computing the dot product of two equal-length vectors (Python lists) from scratch — no NumPy. Multiply corresponding elements and sum the results.

Exercise

Write `magnitude(v)`, returning a vector's Euclidean length (its L2 norm): the square root of the sum of its squared components.

Quiz

Geometrically, what does it mean for the dot product of two (nonzero) vectors to equal zero?

Checkpoint

You can compute a dot product and a vector's magnitude from first principles, and know what a matrix transpose and identity matrix are.