Matrix Operations
Addition and Subtraction: Matching Shapes Only
Two matrices can be added only when they have exactly the same dimensions â the same number of rows and the same number of columns. The sum is built entry by entry, , and subtraction works the same way, . A matrix and a matrix each hold six numbers, yet they cannot be added: their shapes disagree, so there is no meaningful pairing of entries. There is no padding trick and no reshaping rule that rescues the operation â you either match shapes or the sum does not exist.
Because ordinary number addition is commutative and associative, matrix addition inherits both facts: and . The zero matrix of the right size acts as the additive identity, , and every matrix has an additive inverse, , where is just .
Scalar Multiplication: One Number, Every Entry
Multiplying a matrix by a scalar produces the matrix whose every entry is multiplied by : . The shape never changes â a matrix scaled by is still . Scalars distribute over addition, , two scalars nest as , and the scalar leaves a matrix alone. One useful identity is : a pure number commutes with any matrix, which is why exam solutions freely move constants across an equals sign when matrices are involved.
A trap worth separating now, because it resurfaces in the determinants lessons: scaling one row of a matrix by multiplies the determinant by , but scaling the whole matrix by multiplies the determinant of an matrix by , since all rows grow at once.
Matrix Multiplication: Row Times Column
For the product to exist, the number of columns in must equal the number of rows in . If is and is , the product is : the inner dimensions cancel and the outer dimensions and survive. Entry is the dot product of row of with column of :
Each single entry therefore costs multiplications and additions, and a full product costs multiplications â two matrices demand one million multiply-accumulate steps, which is why large matrix products dominate runtime in graphics and machine learning.
Multiplication Is Not Commutative â Even When Both Products Exist
Commutativity fails in the strongest possible way: there are nonzero matrices whose product in one order is the zero matrix while the product in the other order is not. The pair and is the standard counterexample. Even when both and are square and nonzero, they typically differ, because matrix multiplication records the composition of transformations, and composing "stretch, then rotate" rarely matches "rotate, then stretch".
Operation Rules at a Glance
| Operation | Defined when | Result shape | Commutative? | Anchor fact |
|---|---|---|---|---|
| identical shapes | same as | yes | ||
| identical shapes | same as | no | ||
| always | same as | |||
| cols of = rows of | rows of by cols of | no | ||
| square | same as | n/a | needs square |
Verifying the Rules in Python
The fastest way to internalise the algebra is to implement the three operations from scratch and assert the identities the textbook claims.
def add(A, B):
assert len(A) == len(B) and len(A[0]) == len(B[0]), "shapes must match"
return [[A[i][j] + B[i][j] for j in range(len(A[0]))] for i in range(len(A))]
def scale(k, A):
return [[k * a for a in row] for row in A]
def mul(A, B):
n, m, p = len(A), len(A[0]), len(B[0])
assert m == len(B), "cols(A) must equal rows(B)"
C = [[0] * p for _ in range(n)]
for i in range(n):
for j in range(p):
C[i][j] = sum(A[i][k] * B[k][j] for k in range(m))
return C
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
C = [[0, 1], [1, 0]]
print(add(A, B)) # [[6, 8], [10, 12]]
print(scale(3, A)) # [[3, 6], [9, 12]]
print(mul(A, B)) # [[19, 22], [43, 50]]
print(mul(A, mul(B, C)) == mul(mul(A, B), C)) # True â associative
print(mul(A, add(B, C)) == add(mul(A, B), mul(A, C))) # True â distributive
print(mul(A, B) == mul(B, A)) # False â not commutative
A Dimension Checklist Before You Multiply
Exam errors in matrix multiplication are almost always dimension errors, and they are avoidable with a short ritual. Write the shape above each matrix as soon as it appears. Confirm the inner numbers match: for , cols of must equal rows of . Read the result shape from the outer numbers: rows of by cols of . When an equation chains several products, check each product separately â and always agree by associativity, but may not even be defined. If a product looks impossible, look for a transpose: flips the order, and that flip often repairs a mismatched pair.
Where These Operations Appear in Practice
Matrix addition models anything that superposes contributions: combining two discount schedules entry by entry, or adding two displacement lists in a physics table. Scalar multiplication rescales a whole system at once â weighting every feature of a data row, or dilating every coordinate of a shape. Matrix multiplication is the workhorse: solving via row operations, composing geometric transformations, and counting walks in a graph. The adjacency matrix of a directed graph has the property that entry counts the number of walks of length from node to node â a fact that turns a counting question into a single matrix power.
In computer graphics, rotating a point in the plane by angle means multiplying by . Because these matrices encode pure rotations, any two of them do commute with each other â â even though matrix multiplication in general refuses to commute. Stretching matrices, on the other hand, do not commute with rotations, which is exactly why the order of operations matters when a renderer composes a scene graph.
import math
def rot(theta_deg):
t = math.radians(theta_deg)
c, s = round(math.cos(t), 6), round(math.sin(t), 6)
return [[c, -s], [s, c]]
def mul(A, B):
return [[sum(A[i][k] * B[k][j] for k in range(len(B)))
for j in range(len(B[0]))] for i in range(len(A))]
def apply(A, v):
return [A[0][0] * v[0] + A[0][1] * v[1],
A[1][0] * v[0] + A[1][1] * v[1]]
def close(A, B, tol=1e-5):
return all(abs(A[i][j] - B[i][j]) < tol
for i in range(len(A)) for j in range(len(A[0])))
R30, R60, R90 = rot(30), rot(60), rot(90)
print(apply(R90, [1, 0])) # approximately [0, 1]: (1,0) rotates onto the y-axis
print(close(mul(R30, R60), R90)) # True: R(30) R(60) = R(90)
print(close(mul(R60, R30), R90)) # True: rotations commute with each other
Common Mistakes
Adding a 2x3 matrix to a 3x2 matrix because both hold six numbers
Fix: Compare shapes before touching entries: addition requires identical row count and identical column count, nothing less.
Writing AB = BA whenever both products happen to be defined
Fix: Treat multiplication as composition and keep the written order; only special families such as powers of one matrix commute.
Multiplying A (2x3) by B (2x3) because the shapes look compatible
Fix: Identical non-square shapes never multiply; reorder to A times B only when cols of A equal rows of B, otherwise transpose one factor.
Best Practices
- Annotate every matrix with its shape the moment it appears in a solution.
- Check the dimension of each product before computing a single entry.
- Use the counterexample A = [[1, 0], [0, 0]] and B = [[0, 0], [1, 0]] to kill any lingering assumption of commutativity.
- Verify algebraic identities with a five-line Python script rather than trusting one numerical instance.
Key Takeaways
- Addition and subtraction need identical shapes; multiplication needs cols of A to equal rows of B, and the result takes the outer dimensions.
- The product entry (AB)ij is a dot product of row i of A with column j of B, costing m times n times p multiplications for a full product.
- Matrix multiplication is associative and distributive but not commutative; zero divisors such as the standard 2x2 pair prove the failure dramatically.
- Scalar multiplication scales every entry and preserves shape, which is different from scaling a single row when determinants enter the picture.