functions - Department of Computing | Imperialpg/Discrete/Slides/Functions/functions.pdf ·...
Transcript of functions - Department of Computing | Imperialpg/Discrete/Slides/Functions/functions.pdf ·...
Functions
1
Introducing Functions
A function f from a setA to a setB, writtenf : A → B, is a
relationf ⊆ A × B such thateveryelement ofA is related to
oneelement ofB; in logical notation
1. (a, b1) ∈ f ∧ (a, b2) ∈ f ⇒ b1 = b2;
2. ∀a ∈ A.∃b ∈ B. (a, b) ∈ f .
The setA is called thedomain andB theco-domainof f .
If a ∈ A, thenf(a) denotes the uniqueb ∈ B st. (a, b) ∈ f .
2
Comments
If the domainA is then-ary productA1 × . . . × An, then we
often writef(a1, . . . , an) instead off((a1, . . . , an)).
The intended meaning should be clear from the context.
Recall the difference between the following two Haskell
functions:
f :: A -> B -> C -> D
f :: (A,B,C) -> D
Our definition of function is not curried.
3
Image Set
Let f : A → B. For anyX ⊆ A, define theimageof X under
f to be
f [X]def= {f(a) : a ∈ X}
The setf [A] is called theimage setof f .
4
Example
Let A = {1, 2, 3} andB = {a, b, c}.
Let f ⊆ A × B be defined byf = {(1, a), (2, b), (3, a)}.
A B
1
2
3
a
b
c
The image set off is {a, b}.
The image of{1, 3} underf is {a}.
5
Example
Let A = {1, 2, 3} andB = {a, b}. Let f ⊆ A × B be defined
by f = {(1, a), (1, b), (2, b), (3, a)}.
Thisf is not a well-defined function.
A B
1
2
3
a
b
6
Examples
The following are examples of functions with infinite domains
and co-domains:
1. the functionf : N ×N 7→ N defined byf(x, y) = x + y;
2. the functionf : N 7→ N defined byf(x) = x2;
3. the functionf : R 7→ R defined byf(x) = x + 3.
The binary relationR on the reals defined byx R y if and only
if x = y2 is not a function
7
Cardinality
Let A → B denote the set of all functions fromA to B, where
A andB are finite sets. If|A| = m and|B| = n, then
|A → B| = nm.
Sketch proof
For each element ofA, there aren independent ways of
mapping it toB.
You do not need to remember this proof.
8
Partial Functions
A partial functionf from a setA to a setB, writtenf : A ⇀ B, is a relationf ⊆ A × B such that justsomeelements ofA are related tounique elements ofB:
(a, b1) ∈ f ∧ (a, b2) ∈ f ⇒ b1 = b2.
The partial functionf is regarded asundefinedon thoseelements which do not have an image underf .
We call this undefined value⊥ (pronouncedbottom).
A partial function fromA to B is a function fromA to(B + {⊥}).
9
Examples of Partial Functions
1. Haskell functions which return run-time errors on some (orall) arguments.
2. The relationR = {(1, a), (3, a)} ⊆ {1, 2, 3} × {a, b}:A B
a
b3
2
1
Not every element inA maps to an element inB.
3. The binary relationR onR defined byx R y iff√
x = y.It is not defined whenx is negative.
10
Properties of Functions
Let f : A → B be a function.
1. f is onto if and only if every element ofB is in the image
of f :
∀b ∈ B.∃a ∈ A. f(a) = b.
2. f is one-to-oneif and only if for eachb ∈ B there is at
most onea ∈ A with f(a) = b:
∀a, a′ ∈ A. f(a) = f(a′) impliesa = a′.
3. f is abijection iff f is both one-to-one and onto.
11
Example
Let A = {1, 2, 3} andB = {a, b}. The functionf = {(1, a), (2, b), (3, a)} is onto, butnot one-to-one:
A B
1
2
3
a
b
We cannot define a one-to-one function fromA to B. There aretoo many elements inA for them to map uniquely toB.
12
Example
Let A = {a, b} andB = {1, 2, 3}. The function
f = {(a, 3), (b, 1)} is one-to-one, but not onto:
a
b
1
2
3
It is not possible to define an onto function fromA to B. There
are not enough elements inA to map to all the elements ofB.
13
Example
Let A = {a, b, c} andB = {1, 2, 3}. The function
f = {(a, 1), (b, 3), c, 2)} is bijective:
a
b
c
1
2
3
A B
14
Example
The functionf on natural numbers defined byf(x, y) = x + y
is onto but not one-to-one.
To prove thatf is onto, take an arbitraryn ∈ N . Then
f(n, 0) = n + 0 = n.
To show thatf is not one-to-one, we need to produce a
counter-example: that is, find(m1, m2), (n1, n2) such that
(m1, m2) 6= (n1, n2), butf(m1, m2) = f(n1, n2).
For example,(1, 0) and(0, 1).
15
Examples
1. The functionf on natural numbers defined byf(x) = x2
is one-to-one. The similar functionf on integers is not.
2. The functionf on integers defined byf(x) = x + 1 is
onto. The similar function on natural numbers is not.
3. The functionf on the real numbers given by
f(x) = 4x + 3 is a bijective function.
The proof is given in the notes.
16
The Pigeonhole Principle
Pigeonhole Principle
Let f : A → B be a function, whereA andB arefinite. If|A| > |B|, thenf cannot be a one-to-one function.
Example
Let A = {1, 2, 3} andB = {a, b}. A functionf : A → B
cannot be one-to-one, sinceA is too big.
It is not possible to prove this property directly.
The pigeonhole principle states that we assume that theproperty is true.
17
Proposition
Let A andB be finite sets, letf : A → B and letX ⊆ A. Then
|f [X]| ≤ |X|.
Proof Suppose for contradiction that|f [X]| > |X|. Define afunctionp : f [X] → X by
p(b) = somea ∈ X such thatf(a) = b.
There is such ana by definition off [X]. We are placing themembers off [X] in the pigeonholesX. By the pigeonholeprinciple, there is somea ∈ X andb, b′ ∈ f [X] withp(b) = p(b′) = a. But thenf(a) = b andf(a) = b′.Contradiction.
18
Proposition
Let A andB befinite sets, and let
f : A → B.
1. If f is one-to-one, then|A| ≤ |B|.
2. If f is onto, then|A| ≥ |B|.
3. If f is a bijection, then|A| = |B|.
Proof
Part (a) is the contrapositive of the
pigeonhole principle.
For (b), notice that iff is onto then
f [A] = B. Hence,|f [A]| = |B|. Also
|A| ≥ |f [A]| by previous proposition.
Therefore|A| ≥ |B| as required.
Part (c) follows from parts (a) and (b).
19
Composition
Let A, B andC be arbitrary sets, and letf : A → B andg : B → C be functions.
Thecompositionof f with g, writteng ◦ f : A → C, is afunction defined by
g ◦ f(a)def= g(f(a))
for every elementa ∈ A. In Haskell notation, we would write
(g.f) a = g (f a)
It is easy to check thatg ◦ f is indeed a function.
The co-domain off must be the same as the domain ofg.
20
Example
Let A = {1, 2, 3}, B = {a, b, c}, f = {(1, a), (2, b), (3, a)}andg = {(a, 3), (b, 1)}.
Theng ◦ f = {(1, 3), (2, 1), (3, 3)}.
2
3
1
2
3
A B A
1 a
b
21
Associativity
Let f : A → B, g : B → C andh : C → D be arbitrary
functions. Thenh ◦ (g ◦ f) = (h ◦ g) ◦ f .
Proof Let a ∈ A be arbitrary. Then
(h ◦ (g ◦ f))(a) = h((g ◦ f)(a))
= h(g(f(a)))
= (h ◦ g)(f(a))
= ((h ◦ g) ◦ f)(a)
22
Proposition
Let f : A → B andg : B → C be arbitrarybijections. Theng ◦ f is a bijection.
Proof It is enough to show that
1. if f, g are onto then so isg ◦ f ;
2. if f, g are one-to-one then so isg ◦ f .
Assumef andg are onto. Letc ∈ C. Sinceg is onto, we can findb ∈ B such thatg(b) = c. Sincef is onto, we can finda ∈ A such thatf(a) = b. Henceg ◦ f(a) = g(f(a)) = g(b) = c.
Assumef andg are one-to-one. Leta1, a2 ∈ A such thatg ◦ f(a1) = g ◦ f(a2).Theng(f(a1)) = g(f(a2)). Sinceg isone-to-one,f(a1) = f(a2). Sincef is alsoone-to-one,a1 = a2.
23
Identity
Let A be a set. Define theidentity function onA, denoted
idA : A → A, by idA(a) = a for all a ∈ A.
In Haskell, we would declare the function
id :: A -> A
id x = x
24
Inverse
Let f : A → B be an arbitrary function. The function
g : B → A is aninverseof f if and only if
for all a ∈ A, g(f(a)) = a
for all b ∈ B, f(g(b)) = b
Another way of stating the same property is thatg ◦ f = idA
andf ◦ g = idB .
25
Examples
1. The inverse relation of the functionf : {1, 2} → {1, 2}defined byf(1) = f(2) = 1 is not a function.
When an inverse function exists, it corresponds to the
inverse relation.
2. LetA = {a, b, c}, B = {1, 2, 3},
f = {(a, 1), (b, 3), (c, 2)} andg = {(1, a), (2, c), (3, b)}.
Theng is an inverse off .
26
Proposition
Let f : A → B be a bijection, and definef−1 : B → A by
f−1(b) = a wheneverf(a) = b
The relationf−1 is a well-defined function.
It is the inverse off (as shown in next proposition).
Proof
Let b ∈ B. Sincef is onto, there is ana such thatf(a) = b.
Sincef is one-to-one, thisa is unique. Thusf−1 is a function.
It satisfies the conditions for beingan inverse off .
27
Proposition
Let f : A → B. If f has an inverseg, thenf must be abijection and the inverse is unique.
Proof To show thatf is onto, letb ∈ B. Sincef(g(b)) = b, itfollows thatb must be in the image off .
To show thatf is one-to-one, leta1, a2 ∈ A. Supposef(a1) = f(a2). Theng(f(a1)) = g(f(a2)). Sinceg ◦ f = idA, it follows thata1 = a2.
To show that the inverse is unique, suppose thatg, g′ are bothinverses off . Let b ∈ B. Thenf(g(b)) = f(g′(b)) sinceg, g′
are inverses. Henceg(b) = g′(b) sincef is one-to-one.
28
Example
Consider the functionf : N → N defined by
f(x) = x + 1, x odd
= x − 1, x even
It is easy to check that(f ◦ f)(x) = x, considering the cases
whenx is odd and even separately. Thereforef is its own
inverse, and we can deduce that it is a bijection.
29
Cardinality of Sets
Definition
For any setsA, B, defineA ∼ B if and only if there is bijectionfrom A to B.
Proposition Relation∼ is reflexive, symmetric and transitive.
Proof Relation∼ is reflexive , asidA : A → A is a bijection.
To show that it is symmetric,A ∼ B implies that there is abijectionf : A → B. By previous proposition, it follows thatfhas an inversef−1 which is also a bijection. HenceB ∼ A.
The fact that the relation∼ is transitive follows from previousproposition.
30
Example
Let A, B, C be arbitrary sets, and consider the products
(A × B) × C andA × (B × C).
There is a natural bijectionf : (A × B) × C → A × (B × C):
f : ((a, b), c) 7→ (a, (b, c))
Also define the functiong : A × (B × C) → (A × B) × C:
g : (a, (b, c)) 7→ ((a, b), c)
Functiong is the inverse off .
31
Example
Consider the setEven of even natural numbers.
There is a bijection betweenEven andN given byf(n) = 2n.
Not all functions fromEven toN are bijections.
The functiong : Even → N given byg(n) = n is one-to-one
but not onto.
To show thatEven ∼ N , it is enough to show theexistenceof
such a bijection.
32
Example
Recall that the cardinality of afinite set is the number of
elements in that set. Let|A| = n. There is a bijection
cA : {1, 2, . . . , n} → A.
Let A andB be two finite sets. IfA andB have the same
number of elements, we can define a bijectionf : A → B by
f(a) = (cB ◦ c−1
A)(a).
Two finite sets have the same number of elements if and only if
there is a bijection between then.
33
Cardinality
Given twoarbitrary setsA andB, thenA has the same
cardinality asB, written |A| = |B|, if and only if A ∼ B.
Notice that this definition is forall sets.
34
Exploring Infinite Sets
The set of natural numbers is one of the simplest infinite sets.
We can build it up by stages:
0
0, 1
0, 1, 2
. . .
Infinite sets which can be built up in finite portions by stages
are particularly nice for computing.
35
Countable
A setA is countable if and only if A is finite orA ∼ N .
The elements of a countable setA can be listed as a finite or
infinite sequence of distinct terms:A = {a1, a2, a3, . . .}.
36
Example
The integersZ are countable, since they can be listed as:
0,−1, 1,−2, 2,−3, 3, . . .
This ‘counting’ bijectiong : Z → N is defined formally by
g(x) = 2x, x ≥ 0
= −1 − 2x, x < 0
The set of integersZ is like two copies of the natural numbers.
37
Example
The setN 2 is countable:
0 1 2 3 4
1
2
3
4
Comment
The rational numbers are also countable.
38
Uncountable Sets
Cantor showed that there areuncountablesets.
An important example is the set of realsR.
Another example is the power setP(N ).
We cannot manipulate reals in the way we can natural numbers.
Instead, we use approximations: for example, the floating point
decimals of typeFloat in Haskell.
For more information, see Truss, section 2.4.
39