Mathematics.

matrix algebra

Matrix Multiplication

Linear Algebra30 minDifficulty4 out of 10

You should know: matrices

Overview

Matrix multiplication is a binary operation that produces a new matrix from two matrices. For the product AB to be defined, the number of columns of A must equal the number of rows of B: an m×n matrix times an n×p matrix yields an m×p matrix. Unlike ordinary number multiplication, matrix multiplication is not commutative in general — AB ≠ BA — because it represents the composition of two linear transformations, and the order in which transformations are applied matters.

Intuition

Each entry of the product AB is a dot product: the i-th row of A against the j-th column of B. A useful mental model is that matrix multiplication composes functions — if A and B are linear maps, AB means 'first apply B, then apply A' (reading right to left), just like function composition f∘g means 'first g, then f'. That's also why shapes must match up correctly and why order matters.

Interactive Graph

Drag the point to see it transformed by a matrix

Loading visualization…

Formal Definition

Definition

For an m×n matrix A and an n×p matrix B, the product C = AB is the m×p matrix whose (i,j) entry is the dot product of row i of A with column j of B:

A=(aik)m×n,B=(bkj)n×p\mathbf{A} = (a_{ik})_{m \times n},\quad \mathbf{B} = (b_{kj})_{n \times p}

A is m×n, B is n×p — the inner dimensions (n) must match

cij=k=1naikbkjc_{ij} = \sum_{k=1}^{n} a_{ik}\, b_{kj}

The (i,j) entry of C = AB: row i of A dotted with column j of B

Entry formula
C=AB is m×p\mathbf{C} = \mathbf{AB} \text{ is } m \times p

The resulting shape: rows of A by columns of B

Notation

NotationMeaning
AB\mathbf{AB}The matrix product of A and B (A applied after... reading conventions vary; algebraically, row-times-column)
cijc_{ij}The (i,j) entry of the product matrix C = AB
In\mathbf{I}_nIdentity matrix — AI = IA = A for compatible shapes

Properties

Associativity

(AB)C=A(BC)(AB)C = A(BC)

Condition: Whenever the products are defined

Distributivity over addition

A(B+C)=AB+AC,(A+B)C=AC+BCA(B+C) = AB + AC,\quad (A+B)C = AC+BC

NOT commutative in general

ABBAAB \neq BA

Example: Even when both products are defined (e.g. both square of the same size), equality is the exception, not the rule

Transpose of a product

(AB)T=BTAT(AB)^{\mathsf T} = B^{\mathsf T}A^{\mathsf T}

Condition: Order reverses under transpose

Identity element

AIn=ImA=AA\, I_n = I_m\, A = A

Condition: For A of shape m×n

Applications

Composing sequences of transformations (e.g. successive graph adjacency-matrix powers count walks of a given length) and batch linear algebra in numerical libraries all reduce to matrix multiplication.

Worked Examples

  1. c₁₁ = row 1 of A · column 1 of B = 1·5 + 2·7.

    c11=1(5)+2(7)=5+14=19c_{11} = 1(5) + 2(7) = 5 + 14 = 19
  2. c₁₂ = row 1 of A · column 2 of B = 1·6 + 2·8.

    c12=1(6)+2(8)=6+16=22c_{12} = 1(6) + 2(8) = 6 + 16 = 22
  3. c₂₁ = row 2 of A · column 1 of B = 3·5 + 4·7.

    c21=3(5)+4(7)=15+28=43c_{21} = 3(5) + 4(7) = 15 + 28 = 43
  4. c₂₂ = row 2 of A · column 2 of B = 3·6 + 4·8.

    c22=3(6)+4(8)=18+32=50c_{22} = 3(6) + 4(8) = 18 + 32 = 50

Answer: AB = [[19, 22], [43, 50]]

Practice Problems

Difficulty 4/10

Compute AB for A = [[2, 0], [1, 3]] and B = [[4, 1], [0, 2]].

Difficulty 3/10

A is 3×5 and B is 5×2. What is the shape of AB, and is BA defined?

Difficulty 5/10

In 2-D computer graphics, a point (x, y) is rotated 90° counter-clockwise by multiplying by R = [[0, −1], [1, 0]]. Where does the point (3, 1) go?

Difficulty 6/10

In the stiffness method, member forces are found from f = k·d where k is the element stiffness matrix and d the nodal displacements. If k = [[50, −50], [−50, 50]] kN/mm and d = [2, 4]ᵀ mm, compute the nodal forces f.

Common Mistakes

Common Mistake

Multiplying matrices entrywise (like addition), i.e. computing cᵢⱼ = aᵢⱼ·bᵢⱼ.

Matrix multiplication is row-times-column (dot products), not entrywise. The entrywise product (Hadamard product) is a different, less common operation and must be explicitly named as such.

Common Mistake

Assuming AB = BA, as with ordinary numbers.

Matrix multiplication is generally non-commutative; swapping the order can even change whether the product is defined at all, since it depends on matching inner dimensions.

Quiz

For matrices A (m×n) and B (p×q), the product AB is defined only when:
Why is matrix multiplication described as the composition of linear transformations?
In general, for matrices, AB and BA are:

Summary

  • For A (m×n) and B (n×p), the product AB is m×p, with entry cᵢⱼ = Σₖ aᵢₖ bₖⱼ.
  • The product is defined only when A's number of columns equals B's number of rows.
  • Matrix multiplication is associative and distributive over addition, but NOT commutative in general.
  • (AB)ᵀ = BᵀAᵀ — the order reverses under transpose.
  • Matrix multiplication models composition of linear transformations, chained geometric transforms, and neural network layers.

References

  1. BookStrang, G. Introduction to Linear Algebra, 5th ed. Ch. 2.