13  Composition, Matrix Products, and Inverse Transformations

13.1 Opening question

In Chapter 10, we learned that a matrix represents a linear transformation:

\[ T(x)=Ax. \]

But in applications, transformations rarely occur alone. We often apply one process after another:

  1. scale a vector,
  2. rotate it,
  3. project it,
  4. change coordinates,
  5. apply a model,
  6. transform the output again.

What matrix represents the combined process?

The answer is matrix multiplication.

If

\[ T(x)=Bx \]

and

\[ S(y)=Ay, \]

then applying \(T\) first and then \(S\) gives

\[ S(T(x))=A(Bx)=(AB)x. \]

Thus:

Matrix multiplication is composition of linear transformations.

This chapter develops composition, inverse transformations, geometric transformations, and the meaning of matrix products as chained linear processes.

13.2 Learning goals

After studying this chapter, you should be able to:

  1. Define composition of functions.
  2. Interpret matrix multiplication as composition of linear transformations.
  3. Determine the correct order of matrix products.
  4. Compute standard matrices for composed transformations.
  5. Understand why matrix multiplication is usually not commutative.
  6. Use inverses to reverse linear transformations.
  7. Interpret inverse matrices as inverse transformations.
  8. Analyze rotations, reflections, projections, shears, and scalings.
  9. Visualize compositions using Python plots.
  10. Connect composition to computational pipelines and AI models.

13.3 11.1 Composition of functions

If

\[ T:\mathbb{R}^n\to\mathbb{R}^m \]

and

\[ S:\mathbb{R}^m\to\mathbb{R}^p, \]

then the composition

\[ S\circ T:\mathbb{R}^n\to\mathbb{R}^p \]

is defined by

\[ (S\circ T)(x)=S(T(x)). \]

This means:

  1. first apply \(T\) to \(x\),
  2. then apply \(S\) to the result.

The order matters. The notation \(S\circ T\) means \(T\) acts first.

13.4 11.2 Composition of matrix transformations

Suppose

\[ T(x)=Bx \]

and

\[ S(y)=Ay. \]

Then

\[ (S\circ T)(x)=S(T(x))=S(Bx)=A(Bx). \]

By associativity of matrix multiplication,

\[ A(Bx)=(AB)x. \]

Therefore the standard matrix of \(S\circ T\) is

\[ AB. \]

ImportantComposition rule

If \(T(x)=Bx\) and \(S(y)=Ay\), then

\[ (S\circ T)(x)=(AB)x. \]

The matrix of \(S\circ T\) is \(AB\).

In the product \(AB\), the transformation \(B\) acts first and \(A\) acts second.

13.5 11.3 Order matters

The product

\[ ABx \]

means

\[ A(Bx). \]

So \(B\) acts first and \(A\) acts second.

This is often the opposite of how we read from left to right. For transformations, read from right to left:

\[ ABx = \text{first }B,\text{ then }A. \]

For example, if

\[ B \]

is a rotation and

\[ A \]

is a projection, then

\[ AB \]

means:

  1. rotate,
  2. then project.

But

\[ BA \]

means:

  1. project,
  2. then rotate.

These usually produce different results.

13.6 11.4 Example: rotation followed by projection

Let

\[ R= \begin{bmatrix} 0&-1\\ 1&0 \end{bmatrix} \]

be rotation by \(90^\circ\) counterclockwise, and let

\[ P= \begin{bmatrix} 1&0\\ 0&0 \end{bmatrix} \]

be projection onto the \(x\)-axis.

The composition “rotate first, then project” has matrix

\[ PR. \]

Compute:

\[ PR= \begin{bmatrix} 1&0\\ 0&0 \end{bmatrix} \begin{bmatrix} 0&-1\\ 1&0 \end{bmatrix} = \begin{bmatrix} 0&-1\\ 0&0 \end{bmatrix}. \]

Therefore

\[ PR \begin{bmatrix} x\\y \end{bmatrix} = \begin{bmatrix} -y\\ 0 \end{bmatrix}. \]

13.7 11.5 Example: projection followed by rotation

Now apply projection first and then rotation. The matrix is

\[ RP. \]

Compute:

\[ RP= \begin{bmatrix} 0&-1\\ 1&0 \end{bmatrix} \begin{bmatrix} 1&0\\ 0&0 \end{bmatrix} = \begin{bmatrix} 0&0\\ 1&0 \end{bmatrix}. \]

Thus

\[ RP \begin{bmatrix} x\\y \end{bmatrix} = \begin{bmatrix} 0\\ x \end{bmatrix}. \]

Since

\[ PR\ne RP, \]

the order matters.

This is a geometric explanation for why matrix multiplication is usually not commutative.

13.8 11.6 Python visualization: composition order

import numpy as np
import matplotlib.pyplot as plt

def plot_transformed_grid(A, title):
    xs = np.linspace(-2, 2, 9)
    ys = np.linspace(-2, 2, 9)

    plt.figure(figsize=(6, 6))

    for x in xs:
        pts = np.array([[x, y] for y in ys])
        trans = pts @ A.T
        plt.plot(trans[:, 0], trans[:, 1], linewidth=0.8)

    for y in ys:
        pts = np.array([[x, y] for x in xs])
        trans = pts @ A.T
        plt.plot(trans[:, 0], trans[:, 1], linewidth=0.8)

    e1 = A @ np.array([1, 0])
    e2 = A @ np.array([0, 1])

    plt.arrow(0, 0, e1[0], e1[1], head_width=0.08, length_includes_head=True)
    plt.arrow(0, 0, e2[0], e2[1], head_width=0.08, length_includes_head=True)
    plt.text(e1[0] + 0.08, e1[1], "$Ae_1$")
    plt.text(e2[0] + 0.08, e2[1], "$Ae_2$")

    plt.axhline(0, linewidth=0.8)
    plt.axvline(0, linewidth=0.8)
    plt.grid(True)
    plt.axis("equal")
    plt.title(title)
    plt.show()

R = np.array([[0, -1],
              [1,  0]], dtype=float)

P = np.array([[1, 0],
              [0, 0]], dtype=float)

plot_transformed_grid(P @ R, "Rotate first, then project: PR")
plot_transformed_grid(R @ P, "Project first, then rotate: RP")

The two plots show different collapsed lines. This is composition order made visible.

13.9 11.7 Composition preserves linearity

If

\[ T:\mathbb{R}^n\to\mathbb{R}^m \]

and

\[ S:\mathbb{R}^m\to\mathbb{R}^p \]

are linear transformations, then

\[ S\circ T \]

is also linear.

Indeed,

\[ (S\circ T)(u+v)=S(T(u+v)). \]

Since \(T\) is linear,

\[ T(u+v)=T(u)+T(v). \]

So

\[ S(T(u+v))=S(T(u)+T(v)). \]

Since \(S\) is linear,

\[ S(T(u)+T(v))=S(T(u))+S(T(v)). \]

Therefore

\[ (S\circ T)(u+v)=(S\circ T)(u)+(S\circ T)(v). \]

Similarly,

\[ (S\circ T)(cu)=c(S\circ T)(u). \]

Thus the composition is linear.

13.10 11.8 Composition of several transformations

Suppose we apply three transformations:

\[ T_1(x)=A_1x, \]

\[ T_2(y)=A_2y, \]

and

\[ T_3(z)=A_3z. \]

Then applying \(T_1\) first, then \(T_2\), then \(T_3\) gives

\[ T_3(T_2(T_1(x)))=A_3A_2A_1x. \]

The matrix product is

\[ A_3A_2A_1. \]

Again, the rightmost matrix acts first.

TipPipeline rule

For a pipeline

\[ x \xrightarrow{A_1} A_1x \xrightarrow{A_2} A_2A_1x \xrightarrow{A_3} A_3A_2A_1x, \]

the combined matrix is

\[ A_3A_2A_1. \]

13.11 11.9 Geometric transformations as building blocks

Many geometric transformations in \(\mathbb{R}^2\) can be built from basic matrices.

13.11.1 Scaling

\[ S= \begin{bmatrix} a&0\\ 0&b \end{bmatrix} \]

maps

\[ (x,y)\mapsto (ax,by). \]

13.11.2 Rotation

Rotation by angle \(\theta\) is

\[ R_\theta= \begin{bmatrix} \cos\theta&-\sin\theta\\ \sin\theta&\cos\theta \end{bmatrix}. \]

13.11.3 Reflection across the \(x\)-axis

\[ F= \begin{bmatrix} 1&0\\ 0&-1 \end{bmatrix}. \]

13.11.4 Shear

\[ H= \begin{bmatrix} 1&k\\ 0&1 \end{bmatrix}. \]

By composing these transformations, we can build more complicated linear maps.

13.12 11.10 Rotation matrices

The rotation matrix

\[ R_\theta= \begin{bmatrix} \cos\theta&-\sin\theta\\ \sin\theta&\cos\theta \end{bmatrix} \]

rotates every vector counterclockwise by angle \(\theta\).

Important special cases:

For \(\theta=0\),

\[ R_0=I. \]

For \(\theta=\frac{\pi}{2}\),

\[ R_{\pi/2}= \begin{bmatrix} 0&-1\\ 1&0 \end{bmatrix}. \]

For \(\theta=\pi\),

\[ R_{\pi}= \begin{bmatrix} -1&0\\ 0&-1 \end{bmatrix}. \]

Rotation matrices satisfy

\[ R_\alpha R_\beta=R_{\alpha+\beta}. \]

This means composing rotations adds angles.

13.13 11.11 Python visualization: composing rotations

theta = np.pi / 6
phi = np.pi / 4

R_theta = np.array([
    [np.cos(theta), -np.sin(theta)],
    [np.sin(theta),  np.cos(theta)]
])

R_phi = np.array([
    [np.cos(phi), -np.sin(phi)],
    [np.sin(phi),  np.cos(phi)]
])

R_composed = R_phi @ R_theta

R_expected = np.array([
    [np.cos(theta + phi), -np.sin(theta + phi)],
    [np.sin(theta + phi),  np.cos(theta + phi)]
])

R_composed, R_expected
(array([[ 0.25881905, -0.96592583],
        [ 0.96592583,  0.25881905]]),
 array([[ 0.25881905, -0.96592583],
        [ 0.96592583,  0.25881905]]))

The two matrices are the same up to roundoff error.

plot_transformed_grid(R_composed, "Rotation by theta followed by rotation by phi")

13.14 11.12 Shears and scalings

A horizontal shear

\[ H_k= \begin{bmatrix} 1&k\\ 0&1 \end{bmatrix} \]

maps

\[ (x,y)\mapsto (x+ky,y). \]

A scaling

\[ S= \begin{bmatrix} a&0\\ 0&b \end{bmatrix} \]

maps

\[ (x,y)\mapsto (ax,by). \]

The compositions

\[ SH_k \]

and

\[ H_kS \]

are generally different.

13.14.1 Compute \(SH_k\)

\[ SH_k= \begin{bmatrix} a&0\\ 0&b \end{bmatrix} \begin{bmatrix} 1&k\\ 0&1 \end{bmatrix} = \begin{bmatrix} a&ak\\ 0&b \end{bmatrix}. \]

13.14.2 Compute \(H_kS\)

\[ H_kS= \begin{bmatrix} 1&k\\ 0&1 \end{bmatrix} \begin{bmatrix} a&0\\ 0&b \end{bmatrix} = \begin{bmatrix} a&kb\\ 0&b \end{bmatrix}. \]

These are equal only when

\[ ak=kb. \]

If \(k\ne 0\), this means \(a=b\).

So scaling and shearing usually do not commute.

13.15 11.13 Inverse transformations

A transformation

\[ T:\mathbb{R}^n\to\mathbb{R}^n \]

has an inverse transformation if there exists a transformation

\[ T^{-1}:\mathbb{R}^n\to\mathbb{R}^n \]

such that

\[ T^{-1}(T(x))=x \]

and

\[ T(T^{-1}(x))=x \]

for all \(x\).

If

\[ T(x)=Ax, \]

then \(T\) has an inverse exactly when \(A\) is invertible.

In that case,

\[ T^{-1}(x)=A^{-1}x. \]

So inverse matrices are inverse transformations.

13.16 11.14 Examples of inverse transformations

13.16.1 Scaling

If

\[ S= \begin{bmatrix} 2&0\\ 0&3 \end{bmatrix}, \]

then

\[ S^{-1}= \begin{bmatrix} 1/2&0\\ 0&1/3 \end{bmatrix}. \]

The inverse scaling divides by the original scale factors.

13.16.2 Rotation

If

\[ R_\theta \]

rotates by \(\theta\), then

\[ R_\theta^{-1}=R_{-\theta}. \]

The inverse rotation rotates by the opposite angle.

13.16.3 Shear

If

\[ H_k= \begin{bmatrix} 1&k\\ 0&1 \end{bmatrix}, \]

then

\[ H_k^{-1}= \begin{bmatrix} 1&-k\\ 0&1 \end{bmatrix}. \]

The inverse shear uses the opposite shear parameter.

13.16.4 Projection

Projection onto the \(x\)-axis

\[ P= \begin{bmatrix} 1&0\\ 0&0 \end{bmatrix} \]

does not have an inverse, because it loses the \(y\)-coordinate.

13.17 11.15 Inverse of a composition

If \(A\) and \(B\) are invertible, then \(AB\) is invertible and

\[ (AB)^{-1}=B^{-1}A^{-1}. \]

The order reverses.

Why?

The product \(AB\) means first apply \(B\), then apply \(A\).

To undo this process, we must first undo \(A\), then undo \(B\).

So the inverse is

\[ B^{-1}A^{-1}. \]

Check:

\[ (AB)(B^{-1}A^{-1}) = A(BB^{-1})A^{-1} = AIA^{-1} = AA^{-1} = I. \]

Similarly,

\[ (B^{-1}A^{-1})(AB)=I. \]

ImportantInverse of a product

If \(A\) and \(B\) are invertible, then

\[ (AB)^{-1}=B^{-1}A^{-1}. \]

13.18 11.16 Python: inverse transformations

S = np.array([[2, 0],
              [0, 3]], dtype=float)

S_inv = np.linalg.inv(S)

plot_transformed_grid(S, "Scaling by 2 and 3")
plot_transformed_grid(S_inv, "Inverse scaling")

For a rotation:

theta = np.pi / 5

R = np.array([
    [np.cos(theta), -np.sin(theta)],
    [np.sin(theta),  np.cos(theta)]
])

R_inv = np.linalg.inv(R)

plot_transformed_grid(R, "Rotation")
plot_transformed_grid(R_inv, "Inverse rotation")

13.19 11.17 Composition and coordinate change

Change of basis is also a composition.

Suppose \(A\) is a matrix in standard coordinates and \(P\) is a basis matrix.

The matrix in the new basis is

\[ P^{-1}AP. \]

This product means:

  1. start with coordinates in the new basis,
  2. multiply by \(P\) to convert to standard coordinates,
  3. apply \(A\) in standard coordinates,
  4. multiply by \(P^{-1}\) to convert back to the new basis.

So

\[ P^{-1}AP \]

is a composition of three transformations.

This is the conceptual meaning of similarity.

13.20 11.18 Computational pipelines

A computational pipeline often has the form

\[ x\mapsto A_1x\mapsto A_2A_1x\mapsto A_3A_2A_1x. \]

Examples:

  • preprocess data,
  • rotate or project features,
  • apply a model matrix,
  • transform the output.

The combined linear part of the pipeline is a matrix product.

In neural networks, a purely linear sequence of layers without nonlinear activation would collapse into a single matrix product:

\[ A_kA_{k-1}\cdots A_1x. \]

This is why nonlinear activation functions are essential in deep learning. Without nonlinearities, many linear layers are just one linear layer.

13.21 11.19 Python: a simple linear pipeline

# Step 1: scale
S = np.array([[2, 0],
              [0, 1]], dtype=float)

# Step 2: rotate
theta = np.pi / 4
R = np.array([
    [np.cos(theta), -np.sin(theta)],
    [np.sin(theta),  np.cos(theta)]
])

# Step 3: project
P = np.array([[1, 0],
              [0, 0]], dtype=float)

# Combined transformation: scale first, then rotate, then project
A_total = P @ R @ S

plot_transformed_grid(A_total, "Scale, then rotate, then project")

The combined matrix is

\[ A_{\text{total}}=PRS. \]

The rightmost matrix \(S\) acts first.

13.22 11.20 Algorithmic summary

NoteAlgorithm: matrix of a composition

Suppose

\[ T_1(x)=A_1x,\quad T_2(x)=A_2x,\quad \ldots,\quad T_k(x)=A_kx. \]

If we apply \(T_1\) first, then \(T_2\), and continue until \(T_k\), then the combined matrix is

\[ A_kA_{k-1}\cdots A_1. \]

NoteAlgorithm: inverse transformation

If \(T(x)=Ax\) and \(A\) is invertible, then

\[ T^{-1}(x)=A^{-1}x. \]

If \(A\) is not invertible, then \(T\) has no inverse transformation.

NoteAlgorithm: inverse of a product

If \(A_1,\ldots,A_k\) are invertible, then

\[ (A_kA_{k-1}\cdots A_1)^{-1} = A_1^{-1}\cdots A_{k-1}^{-1}A_k^{-1}. \]

The inverse order reverses.

13.23 11.21 Common mistakes

13.23.1 Mistake 1: reading transformation products left to right

In

\[ ABx, \]

the matrix \(B\) acts first and \(A\) acts second.

13.23.2 Mistake 2: assuming \(AB=BA\)

Compositions usually depend on order, so matrix multiplication is usually not commutative.

13.23.3 Mistake 3: reversing the inverse-product formula incorrectly

The correct formula is

\[ (AB)^{-1}=B^{-1}A^{-1}. \]

The inverse order reverses.

13.23.4 Mistake 4: trying to invert a projection

Projection loses information and does not have an inverse transformation.

13.23.5 Mistake 5: forgetting dimension compatibility

For \(S\circ T\) to be defined, the output space of \(T\) must match the input space of \(S\).

For matrix products, the inner dimensions must match.

13.24 11.22 Chapter summary

Composition means applying one transformation after another.

If

\[ T(x)=Bx \]

and

\[ S(y)=Ay, \]

then

\[ (S\circ T)(x)=A(Bx)=(AB)x. \]

So the matrix of the composition is

\[ AB. \]

The rightmost matrix acts first.

Matrix multiplication is usually not commutative because transformation order usually matters.

A linear transformation \(T(x)=Ax\) is invertible exactly when \(A\) is invertible.

In that case,

\[ T^{-1}(x)=A^{-1}x. \]

If \(A\) and \(B\) are invertible, then

\[ (AB)^{-1}=B^{-1}A^{-1}. \]

Change of basis is a composition:

\[ P^{-1}AP. \]

A linear computational pipeline can be represented as a product of matrices.

13.25 Exercises

13.25.1 A. Composition order

Let

\[ R= \begin{bmatrix} 0&-1\\ 1&0 \end{bmatrix}, \qquad P= \begin{bmatrix} 1&0\\ 0&0 \end{bmatrix}. \]

  1. Compute \(PR\).
  2. Compute \(RP\).
  3. Apply both matrices to

\[ x= \begin{bmatrix} 2\\ 3 \end{bmatrix}. \]

  1. Explain geometrically why the results are different.

13.25.2 B. Transformation products

For each description, write the combined matrix.

  1. First scale by

\[ S= \begin{bmatrix} 2&0\\ 0&3 \end{bmatrix}, \]

then rotate by

\[ R= \begin{bmatrix} 0&-1\\ 1&0 \end{bmatrix}. \]

  1. First rotate by \(R\), then project using

\[ P= \begin{bmatrix} 1&0\\ 0&0 \end{bmatrix}. \]

  1. First shear by

\[ H= \begin{bmatrix} 1&2\\ 0&1 \end{bmatrix}, \]

then reflect across the \(x\)-axis.

  1. First apply \(A_1\), then \(A_2\), then \(A_3\).

13.25.3 C. Inverse transformations

Find the inverse matrix if it exists.

\[ \begin{bmatrix} 2&0\\ 0&3 \end{bmatrix} \]

\[ \begin{bmatrix} 0&-1\\ 1&0 \end{bmatrix} \]

\[ \begin{bmatrix} 1&2\\ 0&1 \end{bmatrix} \]

\[ \begin{bmatrix} 1&0\\ 0&0 \end{bmatrix} \]

13.25.4 D. Inverse of a product

Let

\[ A= \begin{bmatrix} 1&1\\ 0&1 \end{bmatrix}, \qquad B= \begin{bmatrix} 2&0\\ 0&3 \end{bmatrix}. \]

  1. Compute \(AB\).
  2. Compute \((AB)^{-1}\) directly.
  3. Compute \(B^{-1}A^{-1}\).
  4. Verify that the answers in parts 2 and 3 agree.

13.25.5 E. Change of basis as composition

Let

\[ A= \begin{bmatrix} 2&1\\ 0&3 \end{bmatrix} \]

and

\[ P= \begin{bmatrix} 1&1\\ 1&-1 \end{bmatrix}. \]

  1. Explain the steps represented by \(P^{-1}AP\).
  2. Compute \(P^{-1}AP\).
  3. Interpret the result as the matrix of the same transformation in a new basis.

13.25.6 F. Python exercises

  1. Write a Python function to plot the image of a grid under a \(2\times 2\) matrix.
  2. Plot \(PR\) and \(RP\) for projection and rotation.
  3. Plot a composition of scaling, rotation, and projection.
  4. Verify numerically that \((AB)^{-1}=B^{-1}A^{-1}\) for two invertible matrices.
  5. Find two matrices \(A\) and \(B\) such that \(AB\ne BA\), and visualize the two transformations.

13.25.7 G. Reflection

Write a short paragraph answering each question.

  1. Why does matrix multiplication represent composition?
  2. Why does the rightmost matrix act first?
  3. Why is matrix multiplication usually not commutative?
  4. Why does the inverse of a product reverse order?
  5. How does composition appear in computational pipelines?

13.26 AI-age connection

Modern computational models are built from composed transformations. Data preprocessing, feature maps, projections, linear layers, and coordinate changes can all be represented by matrices when they are linear. A sequence of linear transformations collapses into one matrix product. This is why nonlinear operations are essential in deep learning: without nonlinearities, many layers would simply be one linear transformation. Composition is therefore the bridge from matrix algebra to computational pipelines.