1 Introduction: Executing Many Calculations with One Operation
Suppose you are developing a 2D video game. A character currently stands at position $(2, 3)$. You want to rotate this character 90 degrees counterclockwise around the coordinate origin.
What is the character's new position? Calculating sines and cosines individually for every single vertex in an animation model would be tedious and computationally inefficient. In modern Computer Graphics and game engines, this entire spatial rotation is executed in a single matrix-vector multiplication:
Instantly, the new position $(-3, 2)$ is computed! Without manually evaluating trigonometric identities, a matrix transformation has mapped the coordinates cleanly.
Matrix Multiplication
An algebraic operation where each entry of the resulting matrix is formed by taking the dot product of a row from the first matrix and a column from the second.
Linear Transformation
A geometric mapping on vector spaces that leaves the origin fixed and preserves the core vector operations of addition and scalar scaling.
2 What is Matrix Multiplication?
Unlike scalar arithmetic, matrix multiplication is not an element-by-element operation. Instead, it computes the dot products between the rows of the first matrix and the columns of the second matrix.
Consider two general $2 \times 2$ matrices:
Their product $C = AB$ is:
To compute entry $c_{ij}$, you take the dot product of Row $i$ of matrix $A$ and Column $j$ of matrix $B$.
3 Dimension Compatibility Rules
Matrix multiplication is only defined under a strict dimensional constraint:
The Compatibility Condition
The product $AB$ exists if and only if the number of columns in matrix $A$ equals the number of rows in matrix $B$.
Dimension Matching Rule
The inner dimensions (n and n) must match. The outer dimensions (m and p) determine the resulting shape.
4 Step-by-Step Worked Example
Let us work through a complete concrete computation. Given:
Because $A$ has 3 columns and $B$ has 3 rows, their product $C = AB$ is a $2 \times 2$ matrix:
5 General Mathematical Formula
For any $m \times n$ matrix $A$ and $n \times p$ matrix $B$, the general summation formula for entry $c_{ij}$ in product $C = AB$ is:
Here, $c_{ij}$ represents the entry at row $i$ and column $j$ in the product matrix, $a_{ik}$ iterates across Row $i$ of $A$, and $b_{kj}$ iterates down Column $j$ of $B$. The index $k$ runs from $1$ to $n$.
6 Why Matrix Multiplication is Non-Commutative
In ordinary arithmetic, multiplication is commutative: $2 \times 3 = 3 \times 2 = 6$. For matrices, however, changing the order changes the result completely:
Let us prove this numerically with two simple $2 \times 2$ matrices:
$AB$ and $BA$ are completely different matrices! Moreover, for non-square matrices (e.g., $2 \times 3$ and $3 \times 4$), $AB$ exists while $BA$ cannot even be evaluated.
7 Core Algebraic Properties of Matrix Multiplication
Parentheses placement does not alter the final result.
Multiplication distributes cleanly over matrix addition.
Multiplying by the Identity matrix preserves values.
Multiplication by zero matrix yields zero.
Scalar factors can be extracted or repositioned freely.
Transposing inverts the operational order!
8 Matrix–Vector Multiplication
Multiplying an $m \times n$ matrix $A$ by an $n \times 1$ column vector $\mathbf{x}$ produces an $m \times 1$ output vector:
Here, matrix $A$ acts as a transformation engine that maps input vector $\begin{bmatrix} 4 \\ 2 \end{bmatrix}$ to output vector $\begin{bmatrix} 10 \\ 10 \end{bmatrix}$.
9 What is a Linear Transformation?
A Linear Transformation $T$ is a vector space mapping that strictly preserves two core mathematical axioms:
10 How a Matrix Represents a Linear Transformation
Every finite-dimensional linear transformation can be expressed as a matrix multiplication:
How does this work geometrically? In 2D Cartesian space, we have standard basis vectors:
Where $\hat{\mathbf{i}}$ lands after transformation forms the first column of $A$, and where $\hat{\mathbf{j}}$ lands forms the second column:
11 Core 2D Linear Transformations
11.1 Identity Transformation
Preserves every coordinate exactly as it was.
11.2 Scaling Transformation
Stretches or shrinks the plane by factors $s_x$ and $s_y$ along the respective axes. $s_x = s_y$ gives uniform scaling; $s_x \ne s_y$ gives non-uniform scaling.
11.3 Rotation Transformation
Rotates vectors counterclockwise around the origin by angle $\theta$:
Properties: Preserves length and angles. Rotation matrices are Orthogonal ($R^{-1} = R^T$, $\det(R) = 1$).
11.4 Reflection Transformation
Reflects coordinates across a reference axis:
11.5 Shear Transformation
Slants the shape along one axis while holding the perpendicular axis fixed:
12 Linear Transformations vs. Translation
A fundamental distinction in computer graphics: Is translating (moving) an object a Linear Transformation?
Answer: No! Ordinary translation is not linear.
Linear transformations must keep the origin invariant ($T(\mathbf{0}) = \mathbf{0}$). A translation $T(x, y) = (x+a, y+b)$ maps $(0, 0)$ to $(a, b) \ne (0, 0)$.
13 Chaining Composite Transformations
In real game engines, an object may be scaled ($S$), then rotated ($R$). Matrix multiplication allows chaining these into a single Composite Transformation Matrix:
14 Matrix Multiplication & Transformations in Python (NumPy)
Use the @ operator or np.matmul() for true matrix multiplication. Remember that * executes element-wise multiplication!
import numpy as np
# 1. Matrix multiplication with @ operator
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[7, 8], [9, 10], [11, 12]])
C = A @ B # Shape: (2, 2)
print("A @ B:\n", C)
# 2. 2D rotation matrix (90 degrees = pi/2)
theta = np.pi / 2
R = np.array([
[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]
])
pos = np.array([2, 3])
rotated_pos = R @ pos
print("Rotated Position:", np.round(rotated_pos)) # [-3, 2]
# 3. Composite transformation (Scale then Rotate)
S = np.array([[2, 0], [0, 3]])
composite = R @ S
print("Composite Transform:", composite @ pos)
15 Real-World Applications in Computer Science & AI
1. Computer Graphics & Game Engines
Mesh transformations, camera viewport projections, and skeletal animation hierarchies rely on parallel matrix multiplication pipelines.
2. Deep Learning & Neural Networks
Dense layers evaluate $Z = XW + b$, and Transformer models evaluate multi-head self-attention via massive batched matrix products ($QK^T$).
3. Robotics & Kinematics
Chained homogeneous transformation matrices compute forward kinematics to track the end-effector tool tip from joint motor angles.
4. Image Processing & Vision
Spatial image warping, perspective correction, and augmented reality camera registration map pixels using 2D and 3D affine matrices.
16 8 Common Pitfalls
Pitfall 1: Expecting Element-Wise Multiplication
Matrix multiplication is a dot product of rows and columns, not direct coordinate-by-coordinate multiplication.
Pitfall 2: Overlooking Inner Dimensions
Columns of matrix $A$ must equal rows of matrix $B$. Otherwise, multiplication is undefined.
Pitfall 3: Assuming Commutativity ($AB = BA$)
Matrix multiplication is strictly non-commutative. Swapping order alters the resulting transformation.
Pitfall 4: Treating Translation as Linear
Translation moves the origin ($T(\mathbf{0}) \ne \mathbf{0}$), making it an affine, not linear, transformation.
Pitfall 5: Forgetting to Reverse Transpose Order
The transpose of a product reverses operands: $(AB)^T = B^T A^T$.
Pitfall 6: Conflating `*` and `@` in Python
NumPy's `*` operator performs element-wise Hadamard multiplication; `@` performs mathematical matrix multiplication.
Pitfall 7: Confusing Rotation Direction
In standard Cartesian math, positive angles represent counterclockwise rotation.
Pitfall 8: Reversing Composite Pipeline Order
To apply transformation $S$ followed by $R$, write $R \cdot S$ because matrices evaluate right-to-left.
17 Key Takeaways
Row dot Column products: $c_{ij} = \sum a_{ik} b_{kj}$.
$(m \times n) \times (n \times p) = (m \times p)$. Inner $n$ must match.
Order matters critically: $AB \ne BA$ in general.
Preserve addition and scalar scaling: $T(\mathbf{x}) = A\mathbf{x}$.
$\begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}$ preserves vector lengths.
Axis stretching and parallel geometric skewing.
Moves origin; unified using Homogeneous Coordinates.
Successive matrices multiply right-to-left: $C = R \cdot S$.
NumPy uses `@` for true matrix multiplication.
Interactive Matrix & Transformation Workbench
Step through row-column dot products, inspect 2D geometric transformations, and visualize composite pipeline non-commutativity.
Edit matrices $A$ and $B$, or click on any cell in product $C$ to inspect the corresponding row-column dot product breakdown:
Entry c₁₁ Computation (Row 1 of A × Col 1 of B):
Transformation Controls
Input vector (1, 1) scales to (2, 1), and then rotates 90° to reach (-1, 2).