II.9 · Linear Algebra
Rows compute outputs; columns assemble the same output.
One product has two complementary readings. Follow row dot-products, then rebuild the identical vector as a weighted sum of columns—including rectangular and incompatible shapes.
One product, read by rows and by columns
The 2 outputs are row dot-products. The same result is a weighted sum of A's 2 columns.
- matrix A
- 2×2 [2, −1; 1, 3]
- input column
- (4, 2)
- input → output shape
- 2×2 · 2×1 → 2×1
- matrix-vector product
- (6, 10)
Exact expressions use =. Rounded computations use ≈. Every view uses this same state.
The 2 outputs are row dot-products. The same result is a weighted sum of A's 2 columns.
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
Predict the canonical output using the first row only, then verify the first inspector component. Next reconstruct the same output from weighted columns. Choose 2×3 · 3×1 and 3×2 · 2×1 to separate input count from output count. End with the rejected product.
Intuition: one machine, two useful readings
A matrix with columns accepts input components. Each of its rows produces one output component, so an matrix maps to .
Rows answer, “How is this output coordinate measured from all inputs?” Columns answer, “What output direction does this input coordinate control?” Both readings use exactly the same products.
Definition
For and , the product is defined by
Equivalently, if is column of ,
The product is defined only when the matrix column count equals the vector length.
Mechanics: check shape before arithmetic
Write dimensions next to the factors:
The equal inner dimensions certify compatibility. The outer dimensions describe the result. Thus a matrix accepts three inputs and emits two outputs; a matrix accepts two inputs and emits three outputs. The board in the stage keeps these shapes visible when a 2D plot would be misleading.
Derivation: rows and columns are the same distributive law
For a example,
The row view groups by output coordinate. The column view groups by input weight. Regrouping changes the explanation, not the result.
Worked examples
With
the rows give . The columns give . Both expose useful structure: the first emphasizes output formulas; the second emphasizes feature contributions.
Inspect the worked calculation
Check that A has one column for every input entry.
- matrix A
- 2×2 [2, −1; 1, 3]
- input column
- (4, 2)
- input → output shape
- 2×2 · 2×1 → 2×1
- matrix-vector product
- (6, 10)
For a matrix, three weighted columns still add to one vector, but that vector has two components. For a matrix, two weighted columns live in three-dimensional output coordinates. The stage displays exact values and shapes without pretending those outputs belong on the 2D coordinate plane.
Executable reference code
The Python implementation validates nonempty rectangular rows and exact dimensions before multiplying. The NumPy example compares the at-operator result with an explicit row and column reconstruction.
from fractions import Fraction as F
def matvec(a, x):
if not a or len(a[0]) != len(x) or any(len(row) != len(x) for row in a):
raise ValueError("an m×n matrix needs an input with n entries")
return tuple(sum((entry * value for entry, value in zip(row, x)), F(0)) for row in a)
A = ((F(2), F(-1)), (F(1), F(3)))
x = (F(4), F(2))
assert matvec(A, x) == (6, 10)
assert matvec(((F(1), F(2), F(3)), (F(0), F(-1), F(2))), (F(2), F(1), F(-1))) == (1, -3)import numpy as np
A = np.array([[1.0, 2.0, 3.0], [0.0, -1.0, 2.0]])
x = np.array([2.0, 1.0, -1.0])
assert A.shape == (2, 3) and x.shape == (3,)
np.testing.assert_allclose(A @ x, [1.0, -3.0])
try:
A @ np.array([2.0, 1.0])
except ValueError:
pass
else:
raise AssertionError("incompatible dimensions must fail")Explore
In the square view, set one input weight to zero and identify the surviving column. Negate a weight and watch its contribution reverse. In the wide view, alter only the third input and find the third column’s contribution. In the tall view, count three row results. Explain the rejected case using dimensions before reading its message.
Common mistakes
Do not multiply corresponding entries and stop; each output combines a full row with the input. Do not confuse the number of columns with the number of outputs. A Python list plus another list concatenates rather than performing vector addition. Finally, transposing an input to make shapes “work” changes the mathematical object unless that transpose was part of the problem.
Numerical considerations
The lesson performs entered decimal arithmetic exactly, which makes equality between the row and column readings literal. Production arrays usually use floating point, where different accumulation orders can differ slightly. Shape validation should happen before numerical work, and comparisons should use an error policy matched to scale rather than a universal hidden tolerance.
Engineering and ML connections
Rows of a linear layer define output features; columns describe how one input feature affects every output. A rectangular matrix naturally changes dimension, as in projecting many features to a smaller embedding or expanding a hidden representation. Batch operations add another axis later, but each individual sample still follows this product.
Exercises
Summary and checkpoints
An matrix accepts inputs, emits outputs, and can be read by rows or columns. Check shape first; then choose the reading that exposes the structure you need. Next we connect several such maps without losing their order.