Matrix Operations
Matrices add entry by entry, multiply row by column with inner sizes that must match, and transpose flips rows into columns.
Why Does This Exist?
Every dense layer of a neural network is a matrix operation. A batch of inputs multiplies a weight matrix, adds a bias, and moves on. Centering data subtracts means entry by entry, attention scores a matrix against its transpose, and backpropagation flows through the same three verbs in reverse. If addition, multiplication, and transpose feel shaky, nothing built on top of them can feel solid.
Vectors, matrices, and tensors introduces what these objects are. This page is the verbs: the three operations, their shape rules, and the order trap. The dedicated matrix multiplication page goes deeper on the geometry of that one operation; here multiplication sits alongside its two siblings so the three can be compared side by side.
The one idea you need first
A shape like reads rows first: 2 rows, 3 columns. Entry sits in row , column . In plain words, the first number says how tall the table is and the second says how wide. Nearly every error in this subject is a shape error, so read both shapes before computing anything.
Think of It Like This
Recipes scaled across kitchens
Think of a matrix as a recipe card. Rows are dishes, columns are ingredients. Adding two cards blends two menus dish by dish: lasagna with lasagna, sauce with sauce. Multiplying is fulfilling orders: each row of the first card, one dish and everything it needs, meets each column of the second card, one supplier and everything they stock, and each meeting produces one total cost. Transposing rewrites the card sideways, ingredients as rows, so tomorrow's computation reads naturally.
Where it stops: real matrices multiply numbers, not intentions, and the row meets column rule has no flexibility. A dish listing 3 ingredient amounts can only meet a supplier listing exactly 3 prices. That rigidity is the inner size rule with an apron on.
How It Actually Works
Addition works entry by entry and needs identical shapes. In plain words, each position combines only with the same position in the other matrix:
A plus a is undefined. Libraries bend this rule with broadcasting, but the mathematics does not: same shape or no sum.
Multiplication works row by column, and the inner sizes must match. For , entry is row of dotted with column of , using the dot product. An times an gives an : the inner values must agree and the outer sizes survive into the answer.
Worked example
Take these two matrices, both , so the product is :
Compute each entry as one row meeting one column:
So , exactly the numbers drawn in the diagram above, where the highlighted row of meets the highlighted column of to make the highlighted . Reversing the order gives : check for the top left, and the rest follow the same way. Both products exist and they disagree everywhere, which proves the headline warning that in general.
Transpose flips across the diagonal. Rows become columns, so an becomes an , and entry moves to :
Transposing twice returns the original, so . Sizes in this subject are measured with norms, which is where the length of a row or column gets its number.
Code
The point of this snippet is the check, not the formula: multiply the worked matrices and confirm every entry, including the reversed order, so a sign or order slip cannot hide.
import numpy as np
def mat_product(a: np.ndarray, b: np.ndarray) -> np.ndarray: # One call, but the shapes must already agree on the inner size. return a @ b
a = np.array([[1, 2], [3, 4]])b = np.array([[5, 6], [7, 8]])print(mat_product(a, b)) # -> [[19 22] [43 50]]print(mat_product(b, a)) # -> [[23 34] [31 46]]Both outputs match the hand computation, including the reversed product that proves order matters.
Watch Out For
Multiplying matrices whose inner sizes disagree
The symptom is a shape error late at night: a weight meeting a input and refusing to multiply. The fix is the outer inner chant. Write both shapes down, check the two middle numbers match, and read the answer shape off the two outer numbers. Nine times out of ten the repair is a missing transpose on one side, which is exactly why transpose exists as an operation rather than a convenience.
Assuming AB equals BA
Scalar habits die hard, and matrix multiplication is not commutative. The symptom is a silently wrong result, because often has a valid shape and computes without complaint, as the worked example shows with two fully valid but different answers. Rotations, projections, and gradients all depend on the order. When porting a formula, preserve the order exactly; when debugging, swap the suspects deliberately and compare against the expected , , , .
The Quick Version
- Addition is entry by entry and needs identical shapes, or the sum is undefined.
- Multiplication is row by column: an times an gives an .
- The worked product here is , from and three siblings.
- Transpose moves entry to and flips an into an .
- in general: the reversed example gives .
- When shapes refuse to multiply, check the inner sizes first, then suspect a missing transpose.