98. Functional composition
98.1 Definition: composition of functions
If $F\colon A \to B$ and $G\colon B \to C$, then $G \circ F\colon A \to C$ is the function defined for all $a \in A$ by $(G \circ F)(a) = G(xxF(a))$. $G \circ F$ is the composite of $F$ and $G$, and the operation “$\circ$” is called functional composition.
98.1.1 How to think about composition The composite of two functions is obtained by feeding the output of one into the input of the other. Suppose $F\colon A \to B$ and $G\colon B \to C$ are functions. If $a$ is any element of $A$, then $F(a)$ is an element of $B$, and so $G(F(a))$ is an element of $C$. Thus applying $F$, then $G$, gives a function from $A$ to $C$, and that is the composite $G \circ F\colon A \to C$.
98.1.2 Remarks
a) You may be familiar with the idea of functional composition in connection with the chain rule in calculus.
b) Our definition of $G \circ F$ requires that the codomain of $F$ be the domain of $G$. Actually, the expression $G(F(a))$ makes sense even if $\operatorname{cod} F$ is only included in $\operatorname{dom} G$, and many authors allow the composite $G \circ F$ to be formed in that case, too. We will not follow that practice here.
98.1.3 Example If $A = \{1,2,3,4\}$, $B = \{3,4,5,6\}$, $C = \{1,3,5,7\}$, $F$ is defined by $F(1) = F(3) = 5$, $F(2) = 3$ and $F(4) = 6$, and $G$ is defined by $G(3) = 7$, $G(4) = 5$, $G(5) = 1$ and $G(6) = 3$, then $G \circ F$ takes $1 \mapsto 1$, $2 \mapsto 7$, $3 \mapsto 1$ and $4 \mapsto 3$.
98.1.4 Warning Applying the function $G \circ F$ to an element of $A$ involves applying $F$, then $G$ — in other words, the notation “$G \circ F$” is read from right to left.
Functional composition is associative when it is defined:
98.2 Theorem
If $F\colon A \to B$, $G\colon B \to C$ and $H\colon C \to D$ are all functions, then $H \circ (G \circ F)$ and $(H \circ G) \circ F$ are both defined and
$$H \circ (G \circ F) = (H \circ G) \circ F$$
Proof Let $a \in A$. Then by applying Definition 98.1 twice,
$$\bigl(H \circ (G \circ F)\bigr)(a) = H\bigl((G \circ F)(a)\bigr) = H(G(F(a)))$$
and similarly
$$\bigl((H \circ G) \circ F\bigr)(a) = (H \circ G)(F(a)) = H(G(F(a)))$$
so $H \circ (G \circ F) = (H \circ G) \circ F$.
98.2.1 Warning Commutativity is a different story. If $F\colon A \to B$ and $G\colon B \to C$, $G \circ F$ is defined, but $F \circ G$ is not defined unless $A = C$. If $A = C$, then $G \circ F\colon A \to C$ and $F \circ G\colon C \to A$, so normally $F \circ G \neq G \circ F$. Commutativity may fail even when $A = B = C$: For example, let $S = x \mapsto x^{2}\colon \mathrm{R} \to \mathrm{R}$ and $T = x \mapsto x+1\colon \mathrm{R} \to \mathrm{R}$. Then for any $x \in \mathrm{R}$, $S(T(x)) = (x+1)^{2}$ and $T(S(x)) = x^{2}+1$, so $S \circ T \neq T \circ S$.
Pondering the following examples of functional composition may be helpful in understanding the idea of composition.
98.2.2 Example Let $\mathrm{SQ} = x \mapsto x^{2}\colon \mathrm{R} \to \mathrm{R}^{+}$ and $\mathrm{SQRT} = x \mapsto \sqrt{x}\colon \mathrm{R}^{+} \to \mathrm{R}$. ($\sqrt{x}$ denotes the nonnegative square root of $x$.) Let $\mathrm{ABS}$ denote the absolute value function from $\mathrm{R}$ to $\mathrm{R}$. Then the following are true.
(i) $\mathrm{SQRT} \circ \mathrm{SQ} = \mathrm{ABS}\colon \mathrm{R} \to \mathrm{R}$.
(ii) $(\mathrm{SQRT} \circ \mathrm{SQ})|\mathrm{R}^{+} = \mathrm{id}_{\mathrm{R}^{+}}$.
(iii) $\mathrm{SQ} \circ \mathrm{SQRT} = \mathrm{id}_{\mathrm{R}^{+}}$.
98.2.3 Example If $F\colon A \to B$ is any function, then
(i) $F \circ \mathrm{id}_{A} = F$ and
(ii) $\mathrm{id}_{B} \circ F = F$.
This is analogous to the property that an identity element for a binary operation has (see 50.1), but in fact composition of functions is not a binary operation since it is not defined for all pairs of functions.
98.2.4 Example If $A \subseteq B$ and $B \subseteq C$, and $i\colon A \to B$ and $j\colon B \to C$ are the corresponding inclusion functions, then $j \circ i$ is the inclusion of $A$ into $C$.
98.2.5 Example If $F\colon A \to B$ and $C \subseteq A$ with inclusion map $i\colon C \to A$, then $F|C = F \circ i$. In other words, restriction is composition with inclusion.
98.2.6 Exercise Describe explicitly (give the domain and codomain and either the graph or a formula) the composite $G \circ F$ if
a) $F\colon \{1,2,3,4\} \to \{3,4,5,6\}$, with $1 \mapsto 3$, $2 \mapsto 4$, $3 \mapsto 6$, and $4 \mapsto 5$, and $G\colon \{3,4,5,6\} \to \{1,3,5,7,9\}$ with $3 \mapsto 1$, $4 \mapsto 7$, $5 \mapsto 7$ and $6 \mapsto 3$.
b) $F\colon x \mapsto x^{3}\colon \mathrm{R} \to \mathrm{R}$, $G\colon x \mapsto 2x\colon \mathrm{R} \to \mathrm{R}$.
c) $F\colon x \mapsto 2x\colon \mathrm{R} \to \mathrm{R}$. $G\colon x \mapsto x^{3}\colon \mathrm{R} \to \mathrm{R}$,
d) $F = \text{inclusion}\colon \mathrm{N} \to \mathrm{R}$, $G\colon x \mapsto (x/2)\colon \mathrm{R} \to \mathrm{R}$.
e) $F = p_{1}\colon \mathrm{R} \times \mathrm{R} \to \mathrm{R}$, $G\colon x \mapsto (3,x)\colon \mathrm{R} \to \mathrm{R} \times \mathrm{R}$.
(Answer on page 248.)
98.2.7 Exercise Let $F\colon A \to B$, $G\colon B \to C$. Show the following facts:
a) If $F$ and $G$ are both injective, so is $G \circ F$.
b) If $F$ and $G$ are both surjective, so is $G \circ F$.
c) If $F$ and $G$ are both bijective, so is $G \circ F$.
d) If $G \circ F$ is surjective, so is $G$.
e) If $G \circ F$ is injective, so is $F$.
98.2.8 Exercise Give examples of functions $F$ and $G$ for which $G \circ F$ is defined and
a) $F$ is injective but $G \circ F$ is not.
b) $G$ is surjective but $G \circ F$ is not.
c) $G \circ F$ is injective but $G$ is not.
d) $G \circ F$ is surjective but $F$ is not.
98.2.9 Exercise (hard) Let $A$, $B$ and $C$ be sets.
a) Prove that if $F\colon A \to B$ is a function and $C$ is nonempty, then $G \mapsto F \circ G\colon A^{C} \to A^{C}$ is a function which is injective if and only if $F$ is injective, and surjective if and only if $F$ is surjective.
b) Prove that if $H\colon B \to C$ is a function and $A$ has more than one element, then $G \mapsto (G \circ H)\colon A^{C} \to A^{B}$ is a function which is injective if and only if $H$ is surjective, and surjective if and only if $H$ is injective.
Pages 140–142 of the book (PDF).