Matrices as maps, span, basis and the views of matrix multiplication
Before this lesson: nothing from this track; this is the first lesson. You need vectors in (lists of real numbers), vector addition and multiplication by a number. Time: about 50 minutes, plus the notebook.
Why this matters #
A linear layer computes . Every output is a combination of the columns of , so all outputs live in the span of those columns. Independence and basis answer "how many directions are really there?": how many features carry new information, and whether coordinates are unique. The views of matrix multiplication are tools for every later lesson: the column view gives rank bounds, and the outer-product view is the language of low rank (SVD, PCA, LoRA, attention).
1. Predict first #
2. The picture #
Start in the plane. Write and for the standard basis vectors. Every equals . A matrix with columns gives
So and : the columns are where the basis vectors land. The unit square (spanned by ) goes to the parallelogram spanned by . The grid lines go to parallel, evenly spaced lines, and the origin stays fixed. This is what "linear" looks like.
Try it. Drag the orange and blue tips: they are the columns of , the images of and . The grey arrow is a test vector and is its image, built as . Use the presets for the five cases in the table below, and make the columns parallel to see the plane flatten.
To read a matrix, draw its two columns:
| Matrix | , | What it does to the plane |
|---|---|---|
| , | stretch along by 2, squeeze along by 1/2 | |
| , turned by | rotate by the angle , counter-clockwise | |
| , | shear: | |
| , | reflect across the -axis | |
| , | flatten the plane onto the line through |
The last row is the warning sign: parallel columns flatten. Every output lies on one line, and the input direction goes to zero.
Span, independence and basis, as pictures. The span of some vectors is everything their combinations reach: one nonzero vector spans a line through 0, two non-parallel vectors in span a plane through 0. A third vector inside that plane adds no new direction: the three are dependent. A basis has enough vectors to reach everything and no extra ones; its size is the dimension.
Span in . Rotate the figure by dragging. With "v₃ in the plane", the third vector adds no new direction: the span stays a plane and the readout shows the dependency. Edit one entry of to lift it off the plane and the span becomes all of . The sliders move the thick dark arrow, the combination ; it can never leave the span.
The columns of a matrix are and . Where does send ?
. You never need row-times-column: take steps along the first column and steps along the second.
Three vectors in all lie in one plane through . What is their span?
Combinations of vectors in a plane through stay in that plane, so they can never leave it. If the three vectors are parallel, the span shrinks to a line. Lengths do not matter: scaling a vector does not change its span.
3. Definitions and main results #
Linear maps and their matrices #
A map is linear if for all vectors and all numbers . With this gives . So a shift with is not linear; it is affine.
Theorem (a linear map is fixed by the images of the basis vectors). Let be linear, and let be the matrix whose column is . Then for every .
Proof.
- Write : the entries of are its coordinates in the standard basis.
- Linearity gives .
- The product is defined by . Read column by column, this says , where is column of .
- With , steps 2 and 3 give the same vector.
So once the basis is fixed, "matrix" and "linear map" are the same thing. The vectors store everything about .
Span, subspace, independence, basis, dimension #
Let , and let be the matrix with these columns.
- A linear combination is , with .
- The span is the set of all linear combinations: , the column space of .
- A subspace is a set that contains 0 and is closed under addition and scaling. Every span is a subspace.
- are linearly independent if only for . Otherwise they are dependent, and every with is a combination of the others.
- A basis of a subspace is an independent list that spans . The dimension is the number of vectors in a basis.
Coordinates are unique. Let be a basis and . Then , and independence gives for every . The numbers are the coordinates of in that basis. Lesson 04 converts between bases.
The null-space test for independence #
Theorem (null-space test). The columns of are independent , where is the null space of .
Proof. . A nonzero with is exactly a nontrivial combination that equals 0.
How to run the test. Use elimination (lesson 02 gives the full theory): subtract multiples of rows to create zeros below each pivot. A column without a pivot is free.
- Every column has a pivot: , the vectors are independent.
- Column is free: set and the other free entries to 0, and solve for the pivot entries from the bottom row up. This gives a nonzero with : the dependency.
Corollary (too many vectors). More than vectors in are always dependent. Each pivot needs its own row, so there are at most pivots. With columns, at least one column is free.
You eliminate the matrix whose columns are four vectors in . What can you say before you start?
Each pivot needs its own row, and has only 3 rows. So at least one of the 4 columns is free, and a free column gives a nonzero with . More than vectors in are always dependent.
Dimension is well defined #
Lemma. If span a subspace , then any vectors in are dependent.
Proof.
- Each lies in , so . In matrix form, with and of size .
- is wide (), so by the corollary it has a vector with .
- Then with : the are dependent.
Theorem (every basis has the same size). If and are both bases of , then .
Proof. The 's span and the 's are independent, so the lemma rules out . Hence . Swap the roles of the two bases: .
Two more consequences of the lemma (Axler 2.38 and 2.42): independent vectors in also span it, and vectors that span are also independent. So to decide whether vectors form a basis of , check one thing only: independence (, or from lesson 03).
Four views of matrix multiplication #
Let be , with columns and rows . Let be , with columns and rows . The product is .
| View | Formula | What it says |
|---|---|---|
| entry | row of times column of | |
| column | each column of is a combination of the columns of | |
| row | row of | each row of is a combination of the rows of |
| outer product | a sum of matrices of rank at most 1 |
Why they agree. All four compute the same numbers ; only the order of the loops changes. For the outer-product view: the entry of is , and the sum over gives .
One product, four readings. Switch the view and press Next part to step through the columns, rows, entries or rank-1 terms of for , . In the column view, each column of is a walk along the columns and of , so it stays in .
Why the product is defined this way. is the matrix of the composition "first , then ": . Its column is (the column view), and the columns fix the map. Composition is associative, so , but not commutative: "rotate, then shear" differs from "shear, then rotate".
Two consequences you will use all the time.
- Rank bound. By the column view, . By the row view, the row space of lies inside the row space of . So (lesson 02 defines rank and shows row rank = column rank).
- Low rank. An outer product with has rank 1: column is . A sum of outer products equals , where has columns and has columns . Its rank is at most .
Cost. Every view needs multiplications and about as many additions; the views differ only in the loop order and the memory access. Blocked GEMM kernels on CPUs and GPUs use the outer-product order: they add up products of thin column blocks of and row blocks of .
Answers to section 1. (1) No. Three vectors in are more than , so they are dependent (corollary). (2) One. Column of is , a multiple of , so all columns lie on one line: rank 1 when .
and are nonzero. What is the rank of the matrix ?
Column of is , a multiple of . All columns lie on the line through , so the rank is 1. This is why the outer-product view is the language of low rank: a sum of outer products has rank at most .
4. Worked example #
(a) A matrix as a map. sends and . Without row-times-column:
The unit square goes to the parallelogram with corners , , , . The map is : the shear , then a vertical stretch by 2. The area becomes 2 (lesson 03: area scale ).
(b) Independence and basis. Do , , form a basis of ? Put them as columns and eliminate:
Pivots are in columns 1 and 2; column 3 is free. Set . Row 2 gives , so . Row 1 gives , so . So , and
Not a basis. The span is a plane with basis , dimension 2. A normal vector of the plane is , so the plane is . Check : ✓.
Now replace by : , so is outside the plane, and are three independent vectors in : a basis.
(c) Four views of one product.
- Entry: .
- Column: column 1 of is , and column 2 is .
- Row: row 2 of is .
- Outer products (column of times row of ):
5. In code #
NumPy checks the hand computations above. The notebook for this lesson has these cells and the exercises.
import numpy as np
from scipy.linalg import null_space
# (a) the columns are the images of e1 and e2
A = np.array([[1., 1.], [0., 2.]])
print(A @ np.array([1., 0.]), A @ np.array([0., 1.])) # [1. 0.] [1. 2.]
print(A @ np.array([2., 1.])) # [3. 2.]
# (b) independence through the null space
V = np.array([[1., 0., 2.],
[2., 1., 5.],
[0., 1., 1.]]) # columns v1, v2, v3
c = null_space(V)[:, 0] # unit length, any sign
print(np.linalg.matrix_rank(V), c / c[2]) # 2 [-2. -1. 1.]
# (c) the outer-product view adds up to the product
A3 = np.array([[1., 0.], [2., 1.], [0., 3.]])
B = np.array([[1., 2.], [1., -1.]])
outer_sum = sum(np.outer(A3[:, k], B[k, :]) for k in range(2))
print(np.array_equal(outer_sum, A3 @ B)) # True
print((A3 @ B).tolist()) # [[1.0, 2.0], [3.0, 3.0], [3.0, -3.0]]
null_space returns a unit vector with an arbitrary sign; dividing by its last entry gives the
hand scaling. With floats, libraries decide independence with a tolerance on the singular values
(lessons 08 and 13).
6. Where it shows up #
- Neural networks. A linear layer outputs a combination of the columns of , weighted by the input. With a bias, is affine, not linear. A one-hot input selects column of ; an embedding lookup is this product, done without the multiplication.
- Low rank. LoRA writes a weight update as , with of size and of size . By the outer-product view, is a sum of rank-1 matrices, so , with parameters instead of . The attention score matrix has rank at most the head dimension . Truncated SVD and PCA (lessons 08–09) keep the largest outer products.
- ML for science. A basis is a choice of coordinates for a field: empirical orthogonal functions (EOFs, the PCA basis) or spherical harmonics in climate science, Fourier modes in spectral solvers and Fourier neural operators. Keeping a few basis vectors means working in a subspace. Independence is identifiability: the effects of dependent predictors in a regression cannot be separated (lesson 02).
7. Common mistakes #
- Rows instead of columns. is column of , not row .
- Checking independence pair by pair. , , are pairwise non-parallel, but they are dependent. Independence is about all combinations at once.
- Spanning set vs basis. A spanning set may contain extra vectors; a basis contains none. The dimension counts the vectors of a basis, not of any spanning set.
- . False in general, even for square matrices; for non-square shapes may not even exist.
- Forgetting the origin. A line that misses 0 is not a subspace, and with is not a linear map.
8. Check yourself #
- Which matrix rotates the plane by counter-clockwise? Find it from the images
of and .
Answer
and , so the columns give . - Do , , form a basis of ?
Answer
Yes. With the vectors as columns, , then : three pivots, so and the vectors are independent. Three independent vectors in also span it. (Also .) - . Give its column space and its null space,
and say what it does to the plane.
Answer
is the line through . is the line through , because . The plane is flattened onto the line through , and the direction goes to 0. - Compute for , , ,
, and write it as one matrix product.
Answer
with (columns ) and (rows ). - Why is the map never one-to-one when is ?
Answer
Its three columns are vectors in , so they are dependent: some has . Then for every , so two different inputs give the same output.
What is column of a matrix , as a map?
: the image of the -th standard basis vector. The columns fix the whole linear map.
Write as a combination of columns.
, where is column of .
What does a matrix with parallel columns do to the plane?
It flattens the plane onto a line (rank 1, ). One input direction is sent to .
Define linearly independent.
only for . Equivalently, for .
How do you test whether given vectors are independent?
Put them as the columns of and eliminate. Every column has a pivot ⇔ ⇔ independent. A free column gives the dependency.
Why are 4 vectors in always dependent?
Each pivot needs its own row; with 3 rows there are at most 3 pivots, so one column is free and gives a nonzero null vector.
What is a basis, and what is the dimension?
A basis is an independent list that spans the subspace. The dimension is the number of vectors in any basis (all bases have the same size).
Name the four views of the product .
Entry (), column (), row () and outer product ().
What is the rank of for nonzero ?
- Every column is a multiple of .
9. Interview angle #
- "What does a matrix do, geometrically?" — It sends the basis vectors to its columns; linearity fixes the rest. Know the cases: stretch, rotation, shear, reflection, flattening.
- "Give the ways to compute , and when each is useful." — Entry (dot products); column (outputs live in ); row; outer product (low-rank sums, GEMM accumulation, LoRA, SVD).
- "How do you check if vectors are independent? And with floats?" — Null space or rank by elimination in exact arithmetic; with floats, compare the smallest singular value with the largest.
- "Why does every basis have the same size?" — If vectors span, any vectors are dependent, because a wide coefficient matrix has a nonzero null vector.
10. Practice #
The notebook for this lesson runs in Colab. It has four short exercises, each with a check cell and a folded solution:
- Build the rotation and a shear from the images of and (a helper in the notebook draws what a matrix does to a grid).
- Write
matmul_columns,matmul_rowsandmatmul_outerwith loops, and check all of them againstA @ B. - Decide independence and find a dependency with the null space (
scipy.linalg.null_space). - Is reachable? Test whether a vector lies in the column space of a matrix.
11. Watch and read #
- Gilbert Strang, MIT 18.06 (2010), lecture 9: Independence, basis, and dimension; lecture 3 shows the views of .
- Boyd and Vandenberghe, Introduction to Applied Linear Algebra (VMLS), free PDF: §5.1–5.2 (independence, basis) and §10.1 (row, column and outer-product views of ).
- Sheldon Axler, Linear Algebra Done Right, 4th edition, free PDF: §2A–2C (2.22 is the lemma of section 3, 2.34 the equal-size theorem).
Runs in your browser with nothing to install. The notebook repeats the key demos and has exercises with checks; solutions are inside, folded.
No Google account needed: Python runs inside your browser. The first start downloads about 40 MB; your changes are saved in this browser only.