0% found this document useful (0 votes)
4 views

07 Functions

Uploaded by

girgis.girgis242
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

07 Functions

Uploaded by

girgis.girgis242
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Functions CS 212: Foundations of CS I

Lecture 7
Announcements

• HW3 due on Thursday at 11:59pm on Gradescope

• Midterm in 2 weeks
Functions
Functions
Definition:
A function 𝑓 from a set 𝐴 to a set 𝐵 is an assignment of each element of
𝐴 to exactly one element of 𝐵.

• This is often denoted: 𝑓: 𝐴 → 𝐵

• We write 𝑓 𝑎 = 𝑏 to denote: 𝑓 maps 𝑎 ∈ 𝐴 to 𝑏 ∈ 𝐵.


Functions
Definition:
For a function 𝑓: 𝐴 → 𝐵, we call:

• 𝐴 the domain of 𝑓

• 𝐵 the codomain of 𝑓

• The set {𝑏 ∈ 𝐵 ∶ 𝑏 = 𝑓 𝑎 for some 𝑎 ∈ 𝐴} the range of 𝑓


Function Example

𝐴 𝐵
𝑓: 1,2,3 → {𝑎, 𝑏, 𝑐}

1 𝑎 𝑓 1 =𝑎
𝑓 2 =𝑏
2 𝑏
𝑓 3 =𝑎

3 𝑐 Domain of 𝑓 is {1,2,3}
Codomain of 𝑓 is {𝑎, 𝑏, 𝑐}
Range of 𝑓 is {𝑎, 𝑏}
Function Non-Example

𝐴 𝐵
The element 1 ∈ 𝐴 is
mapped to two elements
1 𝑎 of 𝐵. Not allowed!

2 𝑏
The element 3 ∈ 𝐴 is not
mapped to any element
3 𝑐 of 𝐵. Not allowed!
Function Examples
• 𝑓 ∶ 1, 2, 3, 4, 5 → {1, 4, 9, 16, 25, 36, 49} where 𝑓 𝑥 = 𝑥 2 .
Square function, except only possible inputs are {1,2,3,4,5}. Observe
that 𝑓’s range is {1, 4, 9, 16, 25}.

• 𝑔 ∶ ℝ → ℤ where 𝑔 𝑥 = 𝑥
This is the floor function, which rounds 𝑥 down to the largest integer less
than or equal to 𝑥. For example, 1.75 = 1, and −2.3 = −3.

• ℎ ∶ StudentIDs → StudentNames where


ℎ 𝑥 = full name of student with ID 𝑥
This function is well-defined because every student ID corresponds to a
student name.
Function Properties
Injective Functions
Definition:
A function 𝑓: 𝐴 → 𝐵 is injective (or one-to-one) if: for all 𝑥, 𝑦 ∈ 𝐴,
if 𝑥 ≠ 𝑦, then 𝑓 𝑥 ≠ 𝑓(𝑦).

In other words: An injective function never maps two elements of 𝐴 to


the same element of 𝐵.

1 1
𝑎 𝑎 Is not
Is 2
2 injective! injective!
𝑏 𝑏
3 3
𝑐 𝑐 4
4
Injective Functions
A function 𝑓: 𝐴 → 𝐵 is injective (or one-to-one) if for all 𝑥, 𝑦 ∈ 𝐴, if 𝑥 ≠ 𝑦,
then 𝑓 𝑥 ≠ 𝑓(𝑦).

The contrapositive of this gives us an equivalent definition:


A function 𝑓: 𝐴 → 𝐵 is injective (or one-to-one) if for all 𝑥, 𝑦 ∈ 𝐴, if
𝑓 𝑥 = 𝑓(𝑦), then 𝑥 = 𝑦.
𝑓: 𝐴 → 𝐵 is injective if
Injectivity Example 1 for all 𝑥, 𝑦 ∈ 𝐴, if 𝑥 ≠ 𝑦,
then 𝑓 𝑥 ≠ 𝑓(𝑦).

Ex 1: Consider the function 𝑓: ℤ → ℤ defined 𝑓 𝑥 = 𝑥 2 . Prove or


disprove: 𝑓 is injective.

We disprove this claim by giving a counterexample.


Observe that 1 ≠ −1, but 𝑓 1 = 12 = −1 2 = 𝑓(−1).
𝑓: 𝐴 → 𝐵 is injective if
Injectivity Example 2 for all 𝑥, 𝑦 ∈ 𝐴, if 𝑥 ≠ 𝑦,
then 𝑓 𝑥 ≠ 𝑓(𝑦).

Ex 2: Consider the function 𝑓: ℕ → ℕ defined 𝑓 𝑥 = 2𝑥 + 1. Prove or


disprove: 𝑓 is injective.

We prove this claim. Let 𝑥, 𝑦 ∈ ℕ be arbitrary, and suppose that 𝑓 𝑥 =


𝑓(𝑦). Then 2𝑥 + 1 = 2𝑦 + 1. Subtracting 1 from both sides, we have
2𝑥 = 2𝑦. Dividing both sides by 2, we have 𝑥 = 𝑦. Since 𝑓 𝑥 = 𝑓(𝑦)
implies 𝑥 = 𝑦 for arbitrary 𝑥, 𝑦, 𝑓 is injective.
Surjective Functions
Definition:
A function 𝑓: 𝐴 → 𝐵 is surjective (or onto) if: for every 𝑏 ∈ 𝐵, there exists
some 𝑎 ∈ 𝐴 such that 𝑓 𝑎 = 𝑏.

In other words: A surjective function’s range is equal to its codomain.

1
𝑎 𝑎 Is not
Is 2
1 surjective! surjective!
𝑏 𝑏
2 3
𝑐 𝑐 4
𝑓: 𝐴 → 𝐵 is surjective if for
Surjective Example 1 every 𝑏 ∈ 𝐵, there exists some
𝑎 ∈ 𝐴 such that 𝑓 𝑎 = 𝑏.

Ex 1: 𝑓: ℕ → ℕ defined 𝑓 𝑥 = 2𝑥 + 1. Prove or disprove: 𝑓 is surjective.

We disprove this claim by providing a counterexample. There is no


𝑎 ∈ ℕ such that 𝑓 𝑎 = 0.

1
(If there were, then 0 = 2𝑎 + 1, so 𝑎 = − , which is not a natural
2
number).
𝑓: 𝐴 → 𝐵 is surjective if for
Surjective Example 2 every 𝑏 ∈ 𝐵, there exists some
𝑎 ∈ 𝐴 such that 𝑓 𝑎 = 𝑏.

Ex 2: 𝑓: ℝ → ℝ defined 𝑓 𝑥 = 2𝑥 + 1. Prove or disprove: 𝑓 is surjective.

𝑏−1
Let 𝑏 ∈ ℝ be arbitrary. Let 𝑎 = .
Since 𝑏 ∈ ℝ, then 𝑎 ∈ ℝ.
2
Furthermore, observe that:
𝑏−1
𝑓 𝑎 =2 +1= 𝑏−1 +1=𝑏
2
Since every 𝑏 ∈ 𝐵 has an 𝑎 ∈ 𝐴 where 𝑓 𝑎 = 𝑏, 𝑓 is surjective.
Proof Templates
Claim: 𝑓: 𝐴 → 𝐵 is injective. Claim: 𝑓: 𝐴 → 𝐵 is surjective.

Proof:
Proof:
Let 𝑥, 𝑦 ∈ 𝐴 be arbitrary, and assume
𝑓 𝑥 = 𝑓(𝑦). Let 𝑏 ∈ 𝐵 be arbitrary.
… …
Apply algebra, logic, Find an 𝑎 ∈ 𝐴 where
creativity 𝑓 𝑎 = 𝑏 and show it works
… …
Therefore 𝑥 = 𝑦.
Since every 𝑏 ∈ 𝐵 has an 𝑎 ∈ 𝐴 where
Since 𝑓 𝑥 = 𝑓(𝑦) implies 𝑥 = 𝑦, 𝑓 is
injective. 𝑓 𝑎 = 𝑏, 𝑓 is surjective.
Bijective Functions
Definition:
A function 𝑓: 𝐴 → 𝐵 is bijective if: it is both injective and surjective.

In other words: Each element of 𝐴 is paired up with a unique element of


𝐵, and vice versa.

𝑎 1 Is
bijective!
𝑏 2
𝑐 3
Note
For a function 𝑓: 𝐴 → 𝐵, the sets 𝐴 and 𝐵 might themselves be
constructed with various set operations.

• E.g. 𝑔: ℝ × ℝ → ℝ where 𝑔 𝑥, 𝑦 = 𝑥 2 + 𝑦.
Takes as input a pair from ℝ × ℝ, i.e. two reals, and returns a real.

• E.g. ℎ: ℕ → 𝒫(ℕ) where ℎ 𝑛 = {0,1, … , 𝑛}.


Takes as input a natural number, and returns the subset of the naturals
with all elements from 0 to 𝑛 inclusive. E.g. ℎ 5 = {0,1,2,3,4,5}.
Exercise Poll Everywhere
pollev.com/anjali212
Consider the following functions: Text anjali212 to 37607
• 𝑔: ℝ × ℝ → ℝ where 𝑔 𝑥, 𝑦 = 𝑥 2 + 𝑦

• ℎ: ℕ → 𝒫(ℕ) where ℎ 𝑛 = 0,1, … , 𝑛

Which are injective? Which are surjective?


Composing Functions
Composition
Suppose you have a function 𝑔 ∶ 𝐴 → 𝐵 and a function 𝑓: 𝐵 → 𝐶.
Then the outputs of 𝑔 can be used as inputs of 𝑓.

𝐴 𝐵 𝐶
𝑔 𝑓

𝑎 𝑔(𝑎) 𝑓 𝑔 𝑎
Composition
Definition: Let 𝑔: 𝐴 → 𝐵, 𝑓: 𝐵 → 𝐶 be functions. Then the composition

function is denoted 𝑓 ∘ 𝑔 and is defined:

𝑓 ∘ 𝑔 ∶ 𝐴 → 𝐶 where 𝑓 ∘ 𝑔 𝑎 = 𝑓 𝑔 𝑎

𝐴 𝐵 𝐶
𝑔 𝑓

𝑎 𝑔(𝑎) 𝑓 𝑔 𝑎
Composition Example 1
Suppose we have:
𝑔 ∶ ℝ → ℝ where 𝑔 𝑥 = 𝑥 + 1
𝑓 ∶ ℝ → ℝ where 𝑓 𝑥 = 𝑥 2

Then we can compose 𝑓 ∘ 𝑔 and 𝑔 ∘ 𝑓.

𝑓 ∘ 𝑔 ∶ ℝ → ℝ where 𝑓 ∘ 𝑔 𝑥 = 𝑓 𝑔 𝑥 = 𝑥+1 2

𝑔 ∘ 𝑓: ℝ → ℝ where 𝑔 ∘ 𝑓 𝑥 = 𝑔 𝑓 𝑥 = 𝑥2 + 1
Composition Example 2
Suppose we have:
𝑔 ∶ ℕ → ℝ where 𝑔 𝑥 = 𝑥
𝑓 ∶ ℝ → ℝ where 𝑓 𝑥 = 𝑥 − 3

Observe: We can compose 𝑓 ∘ 𝑔 but not 𝑔 ∘ 𝑓.

𝑓∘𝑔 = 𝑥−3 E.g. 𝑓 ∘ 𝑔 9 = 𝑓 𝑔 9 =𝑓 3 =0

𝑔∘𝑓 Undefined! The range of 𝑓 is not contained in


the domain of 𝑔
𝑓: 𝐴 → 𝐵 is injective if
Claim for all 𝑥, 𝑦 ∈ 𝐴, if 𝑥 ≠ 𝑦,
then 𝑓 𝑥 ≠ 𝑓(𝑦).

For all functions 𝑔 ∶ 𝐴 → 𝐵 and 𝑓 ∶ 𝐵 → 𝐶, if 𝑔 and 𝑓 are injective, then


𝑓 ∘ 𝑔 is injective.

Proof: Let 𝑔 ∶ 𝐴 → 𝐵, 𝑓 ∶ 𝐵 → 𝐶 be arbitrary injective functions. We aim


to show that 𝑓 ∘ 𝑔 ∶ 𝐴 → 𝐶 is injective.

Let 𝑎1 , 𝑎2 ∈ 𝐴 be arbitrary. Suppose that 𝑓 ∘ 𝑔 𝑎1 = 𝑓 ∘ 𝑔 𝑎2 . Then


𝑓 𝑔 𝑎1 = 𝑓 𝑔 𝑎2 . Since 𝑓 is injective, 𝑔 𝑎1 = 𝑔(𝑎2 ). Since 𝑔 is
injective, 𝑎1 = 𝑎2 . Thus 𝑓 ∘ 𝑔 is injective.

Since 𝑓, 𝑔 were arbitrary, the claim holds.


𝑓: 𝐴 → 𝐵 is surjective if for
Claim every 𝑏 ∈ 𝐵, there exists some
𝑎 ∈ 𝐴 such that 𝑓 𝑎 = 𝑏.

For all functions 𝑔 ∶ 𝐴 → 𝐵 and 𝑓 ∶ 𝐵 → 𝐶, if 𝑔 and 𝑓 are surjective, then


𝑓 ∘ 𝑔 is surjective.

Optional practice!
Inverting Functions
Inverse
Every bijection from 𝐴 to 𝐵 also has an inverse function.

The inverse of bijection 𝑓, denoted 𝑓 −1 , is the function that assigns to


𝑏 ∈ 𝐵 a unique element 𝑎 ∈ 𝐴 such that 𝑓(𝑎) = 𝑏.

𝑓 𝑓 −1

𝑎 1 𝑎 1
𝑏 2 𝑏 2
𝑐 3 𝑐 3
Inverse
Why are inverses only defined on bijections?

Suppose 𝑓 is not injective.

𝑎
1
𝑏
2
𝑐

Then the inverse is not a function, as some elements have multiple


possible mappings.
Inverse
Why are inverses only defined on bijections?

Suppose 𝑓 is not surjective.

1
𝑎
2
𝑏
3
𝑐 4

Then the inverse is not a function, as some elements don’t have any
possible mappings.
Example
Are the following functions invertible?

𝑚 ∶ ℝ → ℝ where 𝑚 𝑥 = 2𝑥 − 5
Yes, because 𝑚 is a bijection (can prove). 𝑚−1 : ℝ → ℝ is defined
𝑥+5
−1
𝑚 𝑥 = .
2

ℎ ∶ StudentIDs → StudentNames where


ℎ 𝑥 = full name of student with ID 𝑥
Not necessarily, in the case that there are students with the same names.
Applications in Computing
Applications in Computing
Functions show up everywhere in computing, and it is often necessary to
prove that functions have some desired properties.

Define an encryption function


𝐸(𝑀, 𝑘) that encrypts the
message 𝑀 with the key 𝑘 into
some encrypted text.

Then 𝐸 must be invertible, so that


we can decrypt the message on
the other end.a
Applications in Computing
Functions show up everywhere in computing, and it is often necessary to
prove that functions have some desired properties.

Often we’ll take an input 𝑥 and


create a hash 𝐻(𝑥). For instance,
to define a HashMap.

Usually hash functions are not


injective. In some settings, it’s
important that they are surjective.
Applications in Computing
Functions show up everywhere in computing, and it is often necessary to
prove that functions have some desired properties.
Dictionaries in Python are
mappings from a set of keys to a
set of values.

If the mapping is one-to-one, can


create an inverse map in the other
direction.

If the mapping is not one-to-one,


can’t create an inverse map.

You might also like