Skip to main content
Logo image

Section 10.4 Composition of functions

composition function
a function Aβ†’C created from given functions f:Aβ†’B and g:Bβ†’C by a↦g(f(a))
g∘f
the composition of functions f:Aβ†’B and g:Bβ†’C, so that g∘f:Aβ†’C by (g∘f)(a)=g(f(a))
A Venn diagram of a function composition.
Figure 10.4.1. A Venn diagram of a function composition.

Example 10.4.2. A composition of two functions.

Consider the functions
f:Rβ†’Rβ‰₯0,g:Rβ‰₯0β†’Rβ‰₯0,x↦x2,x↦x,
Then we have
g∘f:Rβ†’Rβ‰₯0,x↦x2=|x|.

Warning 10.4.3. Composition order matters.

The notation for the composition of functions f and g involves a reversal of order, so that we write g∘f. This is so that when we use this notation with input-output notation (g∘f)(a), the notation reminds us that f must first be applied to the input a, and then g is applied to the result f(a).
In general, f∘gβ‰ g∘f. Usually, one of the two is not even defined, because domains and codomains of f and g will not necessarily match up in both orders. And when both are defined, the two different orders of composition usually have different domains and codomains.

Example 10.4.4. Comparing composition order.

Consider functions
f:Nβ†’N,g:Nβ†’N,n↦n2,n↦n+1.
Then, both f∘g:Nβ†’N and g∘f:Nβ†’N are defined. But they are not equal, as
(f∘g)(n)=(n+1)2=n2+2n+1,(g∘f)(n)=n2+1.

Example 10.4.5. An undefined composition.

Consider functions
sqrt:Nβ†’R,flr:Rβ†’Z,n↦n,xβ†¦βŒŠxβŒ‹.
(See Example 10.3.14 for a description of the flr function.)
Then, flr∘sqrt:Nβ†’Z is defined, with
(flr∘sqrt)(n)=⌊nβŒ‹.
But sqrt∘flr is not defined, as the codomain of flr does not match the domain of sqrt. In particular, flr will sometimes return a negative output, and we cannot use such an output as an input in sqrt.

Checkpoint 10.4.6. Properties of compositions.

Consider functions f:A→B and g:B→C.
  1. If g∘f is injective, are either or both of f,g necessarily injective?
  2. Answer the same question as above with β€œinjective” replaced by β€œsurjective”.
  3. Demonstrate that if both f and g are bijective, then the composition g∘f is also bijective.
Of course, we can compose any number of functions.

Example 10.4.7. A composition of three functions.

Let us reconsider the function defined by algorithm in Example 10.1.5. As the function description involved a multi-step algorithm, we should be able to break the steps involved into their own functions, then recreate the original functions as a composition.
First, define abs:P(Z)β†’P(N) by
abs(X)={|x||x∈X}.
Next, define min:P(N)β†’N so that min(X) outputs the minimum number in input set X, and outputs 0 in case X=βˆ….
Finally, define f:N→N by f(n)=2n+1.
Each of these functions represents one step in the algorithm defining the function in Example 10.1.5, but to recreate that function we need to compose the functions in the correct order: write Ο†=f∘min∘abs, so that
Ο†(X)=2min(abs(X))+1
computes the same result for an input set X as the algorithm described in Example 10.1.5.