# Function Composition

In document Applied Discrete Structures (Page 154-167)

Now that we have a good understanding of what a function is, our next step is to consider an important operation on functions. Our purpose is not to develop the algebra of functions as completely as we did for the algebras of logic, matrices, and sets, but the reader should be aware of the similarities between the algebra of functions and that of matrices. We first define equality of functions.

### 7.3.1 Function Equality

Definition 7.3.1 Equality of Functions. Letf, g:A→B; that is, let f andgboth be functions fromA intoB. Thenf is equal tog (denotedf =g) if and only iff(x) =g(x)for allx∈A.

CHAPTER 7. FUNCTIONS 141 Two functions that have different domains cannot be equal. For example, f :ZZdefined byf(x) =x2 and g:RR defined byg(x) =x2 are not

equal even though the formula that defines them is the same.

On the other hand, it is not uncommon for two functions to be equal even though they are defined differently. For example consider the functions h and k, where h : {−1,0,1,2} → {0,1,2} is defined by h(x) = |x| and k : {−1,0,1,2} → {0,1,2} is defined by k(x) = −x33 +x2+ x

3 appear to be

very different functions. However, they are equal because h(x) = k(x) for x=1,0,1, and2.

### 7.3.2 Function Composition

One of the most important operations on functions is that of composition. Definition 7.3.2 Composition of Functions. Letf :A→Bandg:B C. Then the composition of f followed byg, writteng◦f, is a function from AintoC defined by(g◦f)(x) =g(f(x)), which is read “g off ofx.” ♢ The reader should note that it is traditional to write the composition of functions from right to left. Thus, in the above definition, the first function performed in computing g ◦f is f. On the other hand, for relations, the compositionrs is read from left to right, so that the first relation isr. Example 7.3.3 A basic example. Let f : {1,2,3} → {a, b} be defined by f(1) = a, f(2) = a, and f(3) = b. Let g : {a, b} → {5,6,7} be defined by g(a) = 5 and g(b) = 7. Then g◦f : {1,2,3} → {5,6,7} is defined by (g◦f)(1) = 5, (g◦f)(2) = 5,and (g◦f)(3) = 7. For example, (g◦f)(1) = g(f(l)) =g(a) = 5. Note thatf◦g is not defined. Why?

Letf :RR be defined byf(x) =x3 and let g : RR be defined by g(x) = 3x+ 1. Then, since

(g◦f)(x) =g(f(x)) =g(x3)= 3x3+ 1

we haveg◦f :RRis defined by(g◦f)(x) = 3x3+1. Herefgis also defined

andf◦g:RRis defined by(f◦g)(x) = (3x+1)3. Moreover, since3x3+1̸=

(3x+1)3for at least one real number,gf ̸=fg. Therefore, the commutative

law is not true for functions under the operation of composition. However, the associative law is true for functions under the operation of composition. □ Theorem 7.3.4 Function composition is associative. If f : A B, g:B→C, andh:C→D, thenh◦(g◦f) = (h◦g)◦f.

Proof. Note: In order to prove that two functions are equal, we must use the definition of equality of functions. Assuming that the functions have the same domain, they are equal if, for each domain element, the images of that element under the two functions are equal.

We wish to prove that(h(g◦f))(x) = ((h◦g)◦f)(x)for allx∈A, which is the domain of both functions.

(h(g◦f))(x) =h((g◦f)(x))by the definition of composition =h(g(f(x))) by the definition of composition Similarly,

((h◦g)◦f)(x) = (h◦g)(f(x))by the definition of composition =h(g(f(x))) by the definition of composition . Notice that no matter how the functions the expressionh◦g◦f is grouped,

CHAPTER 7. FUNCTIONS 142 the final image of any element ofx∈Aish(g(f(x)))and soh◦(g◦f) = (h◦g)◦f. ■ Iff is a function on a setA, then the compositions f◦f,f◦f◦f, . . .are valid, and we denote them as f2 , f3, . . .. Repeated compositions of f with itself can be defined recursively. We will discuss this form of definition in detail inSection 8.1.

Definition 7.3.5 Powers of Functions. Letf :A→A.f1=f; that is,f1(a) =f(a), fora∈A.

• Forn≥1,fn+1 =f ◦fn; that is,fn+1(a) =f(fn(a))fora∈A. ♢ Two useful theorems concerning composition are given below. The proofs are left for the exercises.

Theorem 7.3.6 The composition of injections is an injection. If f :A→B andg:B→C are injections, then g◦f :A→C is an injection. Theorem 7.3.7 The composition of surjections is a surjection. If f :A→B andg:B→C are surjections, then g◦f :A→C is a surjection.

We would now like to define the concepts of identity and inverse for func- tions under composition. The motivation and descriptions of the definitions of these terms come from the definitions of the terms in the set of real numbers and for matrices. For real numbers, the numbers 0 and 1 play the unique role that x+ 0 = 0 +x =xand 1 = 1·x=xfor any real number x. 0 and 1 are the identity elements for the reals under the operations of addition and multiplication, respectively. Similarly, then×nzero matrix 0 and then×n identity matrixIare such that for anyn×nmatrixA,A+ 0 = 0 +A=Aand AI =IA=I. Hence, an elegant way of defining the identity function under the operation of composition would be to imitate the above well-known facts. Definition 7.3.8 Identity Function. For any setA, the identity function onAis a function fromAontoA, denoted byi(or, more specifically,iA) such

thati(a) =afor alla∈A.

Based on the definition ofi, we can show that for all functionsf :A→A, f◦i=i◦f =f.

Example 7.3.9 The identity function on {1,2,3}. If A={1,2,3}, then the identity functioni:A→Ais defined byi(1) = 1,i(2) = 2,and i(3) = 3.Example 7.3.10 The identity function on R. The identity function on

Risi:RRdefined byi(x) =x.

### 7.3.3 Inverse Functions

We will introduce the inverse of a function with a special case: the inverse of a function on a set. After you’ve taken the time to understand this concept, you can read about the inverse of a function from one set into another. The reader is encouraged to reread the definition of the inverse of a matrix in Section 5.2 (Definition 5.2.5) to see that the following definition of the inverse function is a direct analogue of that definition.

Definition 7.3.11 Inverse of a Function on a Set. Let f : A →A. If there exists a functiong:A→A such thatg◦f =f ◦g=i, theng is called

CHAPTER 7. FUNCTIONS 143 the inverse off and is denoted byf−1 , read “f inverse.” ♢ Notice that in the definition we refer to “the inverse” as opposed to “an inverse.” It can be proven that a function can never have more than one inverse (see exercises).

An alternate description of the inverse of a function, which can be proven from the definition, is as follows: Letf :A→A be such thatf(a) =b. Then when it exists,f−1is a function fromAtoAsuch thatf1(b) =a. Note that

f−1 “undoes” whatf does.

Example 7.3.12 The inverse of a function on{1,2,3}. LetA={1,2,3} and let f be the function defined on A such that f(1) = 2, f(2) = 3, and f(3) = 1. Then f−1 : A A is defined by f1(1) = 3, f1(2) = 1, and

f−1(3) = 2.

Example 7.3.13 Inverse of a real function. If g :RR is defined by g(x) =x3 , theng1 is the function that undoes whatg does. Sinceg cubes

real numbers, g−1 must be the “reverse” process, namely, takes cube roots.

Therefore, g−1 : R R is defined by g1(x) = 3x. We should show that g−1g=iandgg1=i. We will do the first, and the reader is encouraged

to do the second.

(

g−1◦g)(x) =g−1(g(x)) Definition of composition =g−1(x3) Definition ofg

=3x3 Definition ofg1

=x Definition of cube root

=i(x) Definition of the identity function

Therefore,g−1g=i. Why?

The definition of the inverse of a function alludes to the fact that not all functions have inverses. How do we determine when the inverse of a function exists?

Theorem 7.3.14 Bijections have inverses. Let f :A→A. f−1 exists if

and only if f is a bijection; i. e. f is one-to-one and onto.

Proof. () In this half of the proof, assume thatf−1exists and we must prove that f is one-to-one and onto. To do so, it is convenient for us to use the relation notation, wheref(s) =t is equivalent to(s, t)∈f. To prove thatf is one-to-one, assume thatf(a) =f(b) =c. Alternatively, that means(a, c)and (b, c) are elements of f . We must show that a =b. Since (a, b),(c, b) f, (c, a) and (c, b) are in f−1. By the fact that f1 is a function and c cannot

have two images,aandb must be equal, sof is one-to-one.

Next, to prove thatf is onto, observe that forf−1to be a function, it must

use all of its domain, namely A. Let b be any element of A. Then b has an image underf−1 , f1(b). Another way of writing this is (b, f1(b))f1,

By the definition of the inverse, this is equivalent to(f−1(b), b)∈f. Hence,b is in the range off. Sinceb was chosen arbitrarily, this shows that the range off must be all of A.

() Assumef is one-to-one and onto and we are to provef−1 exists. We

leave this half of the proof to the reader. □ ■ Definition 7.3.15 Permutation. A bijection of a set Ainto itself is called

a permutation ofA.

Next, we will consider the functions for which the domain and codomain are not necessarily equal. How do we define the inverse in this case?

CHAPTER 7. FUNCTIONS 144 Definition 7.3.16 Inverse of a Function (General Case). Letf :A→B, If there exists a functiong:B →Asuch thatg◦f =iAandf◦g=iB , then

gis called the inverse off and is denoted byf−1 , read “f inverse.” ♢ Note the slightly more complicated condition for the inverse in this case because the domains of f◦g andg◦f are different ifAand B are different. The proof of the following theorem isn’t really very different from the special case whereA=B.

Theorem 7.3.17 When does a function have an inverse? Letf :A→B. f−1 exists if and only if f is a bijection.

Example 7.3.18 Another inverse. Let A = {1,2,3} and B = {a, b, c}. Define f : A B by f(1) = a, f(2) = b, and f(3) = c. Then g : B A defined byg(a) = 1,g(b) = 2, andg(c) = 3is the inverse off.

(g◦f)(1) = 1 (g◦f)(2) = 2 (g◦f)(3) = 3    g◦f =iA and (f◦g)(a) =a (f◦g)(b) =b (f◦g)(c) =c    f ◦g=iB

### 7.3.4 Exercises for Section 7.3

1. Let A = {1,2,3,4,5}, B = {a, b, c, d, e, f}, and C = {+,−}. Define f : A B by f(k) equal to the kth letter in the alphabet, and define

g:B→C byg(α) = +ifαis a vowel and g(α) =−ifαis a consonant. (a) Find g◦f.

(b) Does it make sense to discussf ◦g? If not, why not? (c) Doesf−1 exist? Why?

(d) Doesg−1 exist? Why?

2. LetA={1,2,3}. Definef :A→Abyf(1) = 2, f(2) = 1, andf(3) = 3. Findf2,f3,f4andf1.

3. LetA={1,2,3}.

(a) List all permutations ofA.

(b) Find the inverse and square of each of the permutations of part a, where the square of a permutation,f, is the composition f◦f. (c) Show that the composition of any two permutations of A is a per-

mutation ofA.

(d) Prove that ifA is any set where|A|=n, then the number of per- mutations ofA isn!.

4. Defines,u, andd, all functions on the integers, bys(n) =n2,u(n) =n+1,

andd(n) =n−1. Determine: (a) u◦s◦d

CHAPTER 7. FUNCTIONS 145 (c) d◦s◦u

5. Based on the definition of the identity function, show that for all functions f :A→A,f◦i=i◦f =f.

6. Inverse images. Iff is any function fromAintoB, we can describe the inverse image as a function from B into P(A), which is also commonly denoted f−1. IfbB,f1(b) ={aA| f(a) =b}. Iff does have an

inverse, the inverse image ofbis{f−1(b)}.

(a) Let g :RR be defined byg(x) = x2. What are g−1(4), g−1(0) andg−1(1)?

(b) Ifr:RZ, wherer(x) =⌈x⌉, what isr−1(1)?

7. Letf, g, andhall be functions from Zinto Z defined byf(n) =n+ 5, g(n) =n−2,andh(n) =n2. Define:

(a) f◦g (b) f3 (c) f◦h

8. Define the following functions on the integers byf(k) =k+ 1,g(k) = 2k, andh(k) =⌈k/2⌉

(a) Which of these functions are one-to-one? (b) Which of these functions are onto?

(c) Express in simplest terms the compositions f◦g,g◦f,g◦h,h◦g, andh2,

9. LetAbe a nonempty set. Prove that iff is a bijection onAandf◦f =f, thenf is the identity function,i

Hint. You have seen a similar proof in matrix algebra.

10. For the real matrixA=

(

a b c d

)

Recall that a bijection from a set to itself is also referred to as a permutationof the set. Letπbe a permutation of{a, b, c, d}such that abecomesπ(a),bbecomes π(b), etc.

Let B =

(

π(a) π(b) π(c) π(d)

)

. How many permutations of π leave the determinant ofAinvariant, that is, detA=detB?

11. State and prove a theorem on inverse functions analogous to the one that says that if a matrix has an inverse, that inverse is unique.

12. Let f and g be functions whose inverses exist. Prove that (f ◦g)−1 =

g−1f1.

Hint. See Exercise 3 of Section 5.4. 13. ProveTheorem 7.3.6andTheorem 7.3.7. 14. Prove the second half ofTheorem 7.3.14.

15. Prove by induction that if n 2 and f1, f2 , \ldots , fn are invertible

functions on some nonempty setA, then { }(f1◦f2◦ · · · ◦fn)1=fn−1

CHAPTER 7. FUNCTIONS 146 16.

(a) Our definition of cardinality states that two sets, A and B, have the same cardinality if there exists a bijection between the two sets. Why does it not matter whether the bijection is from A into B or B intoA?

(b) Prove that “has the same cardinality as” is an equivalence relation on sets.

17. Construct a table listing as many “Laws of Function Composition” as you can identify. Use previous lists of laws as a guide.

## lations

An essential tool that anyone interested in computer science must master is how to think recursively. The ability to understand definitions, concepts, al- gorithms, etc., that are presented recursively and the ability to put thoughts into a recursive framework are essential in computer science. One of our goals in this chapter is to help the reader become more comfortable with recursion in its commonly encountered forms.

A second goal is to discuss recurrence relations. We will concentrate on methods of solving recurrence relations, including an introduction to generating functions.

### 8.1 The Many Faces of Recursion

Consider the following definitions, all of which should be somewhat familiar to you. When reading them, concentrate on how they are similar.

### 8.1.1 Binomial Coefficients

Here is a recursive definition of binomial coefficients, which we introduced in Chapter 2.

Definition 8.1.1 Binomial Coefficient - Recursion Definition. Assume n≥0 andn≥k≥0. We define(n k ) by • (n 0 ) = 1 • (n n ) = 1and • (n k ) =(n−k1)+(nk11)ifn > k >0 ♢ Observation 8.1.2A word about definitions: Strictly speaking, when mathe- matical objects such as binomial coefficents are defined, they should be defined just once. Since we defined binomial coefficients earlier, in Definition 2.4.3, other statements describing them should be theorems. The theorem, in this case, would be that the “definition” above is consistent with the original defini-

CHAPTER 8. RECURSION AND RECURRENCE RELATIONS 148 tion. Our point in this chapter in discussing recursion is to observe alternative definitions that have a recursive nature. In the exercises, you will have the opportunity to prove that the two definitions are indeed equivalent.

Here is how we can apply the recursive definition to compute (5 2 ) . ( 5 2 ) = ( 4 2 ) + ( 4 1 ) = ( ( 3 2 ) + ( 3 1 ) ) + ( ( 3 1 ) + ( 3 0 ) ) = ( 3 2 ) + 2 ( 3 1 ) + 1 = ( ( 2 2 ) + ( 2 1 ) ) + 2( ( 2 1 ) + ( 2 0 ) ) + 1 = (1 + ( 2 1 ) ) + 2( ( 2 1 ) + 1) + 1 = 3 ( 2 1 ) + 4 = 3( ( 1 1 ) + ( 1 0 ) ) + 4 = 3(1 + 1) + 4 = 10

### 8.1.2 Polynomials and Their Evaluation

Definition 8.1.3 Polynomial Expression inxoverS (Non-Recursive). Letn be an integer, n 0. An nth degree polynomial in x is an expression of the formanxn+an−1xn−1+· · ·+a1x+a0, where an, an−1, . . . , a1, a0 are

elements of some designated set of numbers, S, called the set of coefficients

andan ̸= 0. ♢

We refer to xas a variable here, although the more precise term for xis anindeterminate. There is a distinction between the terms indeterminate and variable, but that distinction will not come into play in our discussions.

Zeroth degree polynomials are called constant polynomials and are simply elements of the set of coefficients.

This definition is often introduced in algebra courses to describe expressions such asf(n) = 4n3+ 2n28n+ 9, a third-degree, or cubic, polynomial inn. This definition has a drawback when the variable is given a value and the expression must be evaluated. For example, suppose that n = 7. Your first impulse is likely to do this:

f(7) = 4·73+ 2·728·7 + 9 = 4·343 + 2·498·7 + 9 = 1423

A count of the number of operations performed shows that five multiplica- tions and three additions/subtractions were performed. The first two multipli- cations compute72 and 73, and the last three multiply the powers of 7 times

the coefficients. This gives you the four terms; and adding/subtracting a list ofknumbers requiresk−1addition/subtractions. The following definition of a polynomial expression suggests another more efficient method of evaluation.

CHAPTER 8. RECURSION AND RECURRENCE RELATIONS 149 Definition 8.1.4 Polynomial Expression inxover S (Recursive). Let S be a set of coefficients andxa variable.

(a) A zeroth degree polynomial expression inxoverS is a nonzero element ofS.

(b) Forn≥1, annthdegree polynomial expression inxoverS is an expres-

sion of the formp(x)x+awhere p(x)is an(n1)st degree polynomial

expression inxanda∈S.

♢ We can easily verify that f(n) = 4n3+ 2n28n+ 9 is a third-degree

In document Applied Discrete Structures (Page 154-167)