12–15 min read Linear Algebra & Computer Graphics Lesson 17 of 32
Part 3 — Linear Algebra (Lesson 4)

Lesson 14.17 — Matrix Multiplication & Linear Transformations

How can a single mathematical operation perform hundreds of geometric and computational tasks simultaneously? How does matrix multiplication power object rotation in 3D graphics, robot arm kinematics, and multi-layer neural networks in modern AI? In this lesson, we master the mechanics of matrix multiplication and unlock the geometric elegance of linear transformations.

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:

$$\text{90° Counterclockwise Rotation: } R = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}, \quad \text{Initial Position: } \mathbf{x} = \begin{bmatrix} 2 \\ 3 \end{bmatrix}$$ $$R\mathbf{x} = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix} 2 \\ 3 \end{bmatrix} = \begin{bmatrix} 0(2) + (-1)(3) \\ 1(2) + 0(3) \end{bmatrix} = \begin{bmatrix} -3 \\ 2 \end{bmatrix}$$

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:

$$A = \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix}, \quad B = \begin{bmatrix} b_{11} & b_{12} \\ b_{21} & b_{22} \end{bmatrix}$$

Their product $C = AB$ is:

$$AB = \begin{bmatrix} a_{11}b_{11} + a_{12}b_{21} & a_{11}b_{12} + a_{12}b_{22} \\ a_{21}b_{11} + a_{22}b_{21} & a_{21}b_{12} + a_{22}b_{22} \end{bmatrix}$$

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

A: m × n
×
B: n × p
→
AB: m × p

The inner dimensions (n and n) must match. The outer dimensions (m and p) determine the resulting shape.

✓ Valid Multiplication: A $(2 \times 3)$ matrix times a $(3 \times 2)$ matrix produces a $\mathbf{2 \times 2}$ matrix.
✗ Invalid Multiplication: A $(2 \times 3)$ matrix times a $(2 \times 3)$ matrix is undefined, because 3 $\ne$ 2!

4 Step-by-Step Worked Example

Let us work through a complete concrete computation. Given:

$$A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}_{(2 \times 3)}, \quad B = \begin{bmatrix} 7 & 8 \\ 9 & 10 \\ 11 & 12 \end{bmatrix}_{(3 \times 2)}$$

Because $A$ has 3 columns and $B$ has 3 rows, their product $C = AB$ is a $2 \times 2$ matrix:

Step 1: Row 1 of A × Column 1 of B ($c_{11}$)
$$c_{11} = 1(7) + 2(9) + 3(11) = 7 + 18 + 33 = 58$$
Step 2: Row 1 of A × Column 2 of B ($c_{12}$)
$$c_{12} = 1(8) + 2(10) + 3(12) = 8 + 20 + 36 = 64$$
Step 3: Row 2 of A × Column 1 of B ($c_{21}$)
$$c_{21} = 4(7) + 5(9) + 6(11) = 28 + 45 + 66 = 139$$
Step 4: Row 2 of A × Column 2 of B ($c_{22}$)
$$c_{22} = 4(8) + 5(10) + 6(12) = 32 + 50 + 72 = 154$$
$$AB = \begin{bmatrix} 58 & 64 \\ 139 & 154 \end{bmatrix}$$

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:

$$c_{ij} = \sum_{k=1}^{n} a_{ik} b_{kj}$$

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:

$$AB \ne BA \quad \text{(Non-Commutative Property)}$$

Let us prove this numerically with two simple $2 \times 2$ matrices:

$$A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, \quad B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix}$$
Product A × B: $$AB = \begin{bmatrix} 1(5)+2(7) & 1(6)+2(8) \\ 3(5)+4(7) & 3(6)+4(8) \end{bmatrix} = \begin{bmatrix} \mathbf{19} & \mathbf{22} \\ \mathbf{43} & \mathbf{50} \end{bmatrix}$$
Product B × A: $$BA = \begin{bmatrix} 5(1)+6(3) & 5(2)+6(4) \\ 7(1)+8(3) & 7(2)+8(4) \end{bmatrix} = \begin{bmatrix} \mathbf{23} & \mathbf{34} \\ \mathbf{31} & \mathbf{46} \end{bmatrix}$$

$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

Associative Property
$$(AB)C = A(BC)$$

Parentheses placement does not alter the final result.

Distributive Property
$$A(B+C) = AB + AC$$

Multiplication distributes cleanly over matrix addition.

Identity Property
$$AI = IA = A$$

Multiplying by the Identity matrix preserves values.

Zero Matrix Factor
$$AO = OA = O$$

Multiplication by zero matrix yields zero.

Scalar Compatibility
$$(kA)B = k(AB) = A(kB)$$

Scalar factors can be extracted or repositioned freely.

Transpose of Product
$$(AB)^T = B^T A^T$$

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:

$$A = \begin{bmatrix} 2 & 1 \\ 1 & 3 \end{bmatrix}, \quad \mathbf{x} = \begin{bmatrix} 4 \\ 2 \end{bmatrix}$$ $$A\mathbf{x} = \begin{bmatrix} 2(4) + 1(2) \\ 1(4) + 3(2) \end{bmatrix} = \begin{bmatrix} 8 + 2 \\ 4 + 6 \end{bmatrix} = \begin{bmatrix} 10 \\ 10 \end{bmatrix}$$

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:

1. Additivity $$T(\mathbf{u} + \mathbf{v}) = T(\mathbf{u}) + T(\mathbf{v})$$ Transforming the sum of two vectors equals the sum of their individual transformations.
2. Homogeneity (Scalar Scaling) $$T(k\mathbf{v}) = k T(\mathbf{v})$$ Scaling a vector before or after transformation yields identical results.
Geometric Intuition: Under any linear transformation, the origin remains fixed ($T(\mathbf{0}) = \mathbf{0}$), and all grid lines remain straight and parallel (no curving, warping, or bending).

10 How a Matrix Represents a Linear Transformation

Every finite-dimensional linear transformation can be expressed as a matrix multiplication:

$$T(\mathbf{x}) = A\mathbf{x}$$

How does this work geometrically? In 2D Cartesian space, we have standard basis vectors:

$$\hat{\mathbf{i}} = \begin{bmatrix} 1 \\ 0 \end{bmatrix}, \quad \hat{\mathbf{j}} = \begin{bmatrix} 0 \\ 1 \end{bmatrix}$$

Where $\hat{\mathbf{i}}$ lands after transformation forms the first column of $A$, and where $\hat{\mathbf{j}}$ lands forms the second column:

$$A = \begin{bmatrix} T(\hat{\mathbf{i}}) & T(\hat{\mathbf{j}}) \end{bmatrix}$$

11 Core 2D Linear Transformations

11.1 Identity Transformation

Preserves every coordinate exactly as it was.

$$I = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \implies I\begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} x \\ y \end{bmatrix}$$

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.

$$S = \begin{bmatrix} s_x & 0 \\ 0 & s_y \end{bmatrix} \implies S\begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} s_x x \\ s_y y \end{bmatrix}$$

11.3 Rotation Transformation

Rotates vectors counterclockwise around the origin by angle $\theta$:

$$R(\theta) = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}$$

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:

Across $x$-axis: $$F_x = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}$$
Across $y$-axis: $$F_y = \begin{bmatrix} -1 & 0 \\ 0 & 1 \end{bmatrix}$$

11.5 Shear Transformation

Slants the shape along one axis while holding the perpendicular axis fixed:

$$\text{Horizontal Shear: } H_x = \begin{bmatrix} 1 & k \\ 0 & 1 \end{bmatrix} \implies \begin{bmatrix} x + ky \\ y \end{bmatrix}$$

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)$.

Affine Transformations & Homogeneous Coordinates: Combining linear transformations with translations yields an Affine Transformation:
$$T(\mathbf{x}) = A\mathbf{x} + \mathbf{b}$$
Graphics engines handle translations seamlessly via Homogeneous Coordinates, appending a 1 to vectors to pack translation directly into a $3 \times 3$ (or 3D $4 \times 4$) matrix:
$$\begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}$$

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:

$$R(S\mathbf{x}) = (RS)\mathbf{x} = C\mathbf{x} \quad \text{where } C = RS$$
Order Sensitivity: Because matrix multiplication is non-commutative ($RS \ne SR$), scaling then rotating ($RS$) produces a completely different orientation and position than rotating then scaling ($SR$)! Matrices act from right to left on input vectors.

14 Matrix Multiplication & Transformations in Python (NumPy)

Use the @ operator or np.matmul() for true matrix multiplication. Remember that * executes element-wise multiplication!

linear_transformations.py Python 3
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

1. Multiplication Rule

Row dot Column products: $c_{ij} = \sum a_{ik} b_{kj}$.

2. Inner Dimensions

$(m \times n) \times (n \times p) = (m \times p)$. Inner $n$ must match.

3. Non-Commutative

Order matters critically: $AB \ne BA$ in general.

4. Linear Transformations

Preserve addition and scalar scaling: $T(\mathbf{x}) = A\mathbf{x}$.

5. 2D Rotation

$\begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}$ preserves vector lengths.

6. Scaling & Shear

Axis stretching and parallel geometric skewing.

7. Translation is Affine

Moves origin; unified using Homogeneous Coordinates.

8. Chaining Transforms

Successive matrices multiply right-to-left: $C = R \cdot S$.

9. Python Implementation

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:

Matrix A ($2 \times 2$)
Matrix B ($2 \times 2$)
Result: C = AB

Entry c₁₁ Computation (Row 1 of A × Col 1 of B):

c₁₁ = (1 × 5) + (2 × 7) = 5 + 14 = 19