Functions

- 7.1
Functions
P. Danziger
1
Basic Definitions
Definition 1 A binary relation from A to B is called a function if for every element a ∈ A there
is a unique element b ∈ B such that aRb.
Given a function, f , from A to B, we write f : A → B.
Given x ∈ A we denote the unique element of B which x is mapped to by f (x).
The definition of function says two things:
1. ∀ a ∈ A, ∃ b ∈ B such that aRb.
i.e. f (x) is defined for each x ∈ A
2. ∀a ∈ A, b, c ∈ B, (aRb ∧ aRc) ⇒ b = c.
i.e. f (x) is unique.
To prove that a relation is a function we must prove 1 and 2 above.
To show that a relation is not a function we must find a counterexample to either 1 or 2 above.
Example 2
For each of the following relations determine whether they are functions. Prove it.
1. A = B = Z, xRy ⇔ y = x + 3.
1) x + 3 is defined for all x ∈ Z.
2) Let a ∈ Z and let b, c ∈ Z be such that b = a + 3 and c = a + 3.
⇒ b = c. So this is a function.
2. > on N.
2R0 and 2R1, so image of 2 is not unique. Thus > is not a function.
We say that such a relation is one to many, since one element of A is taken to
many (more than one)elements of B.
3. f : R → R, f (x) = x2 .
1) x2 is defined for all x ∈ R.
2) Let x ∈ R, and let y, z ∈ R be such that y = x2 and z = x2 .
⇒ y = z.
√
4. f : R → R, f (x) = x.
√
x is not defined for x < 0, so this is not a function.
1
- 7.1
Functions
P. Danziger
√
5. f : R+ → R, f (x) = x.
√
1) x is defined for all x ∈ R+ .
√
√
2) Let x ∈ R+ , and let y, z ∈ R be such that y = x and z = x
⇒ y = z. So this is a function.
√
Note the symbol a always means the positive square root of a.
1
6. f : R+ → R, f (x) = x 2 .
1
1
Let x ∈ R+ , and let y, z ∈ R be such that y = x 2 and z = x 2
⇒ y 2 = z 2 ⇒ y = ±z. So this is not a function.
x
if x ≥ 0
7. f : R → R, f (x) = |x| =
−x if x < 0
1) |x| is defined for all x ∈ R.
2) Let x ∈ R and let y, z ∈ R be such that y = |x| and z = |x|.
If x ≥ 0 then y = x and z = x, so y = z.
If x < 0 then y = −x and z = −x, so y = z.
So f is a function.
Note If the graph of a relation satisfies the property that every vertical line cuts the graph exactly
once then the relation is a function.
We will now consider some of the properties of functions.
Definition 3 Given a function f from A to B
1. The domain is all those values x ∈ A where f (x) is defined.
dom(f ) = {a ∈ A | f (x) is defined }.
2. The co-domain of f is B.
3. The image or range of f is the set of values in y ∈ B for which there is an a ∈ A such that
f (a) = b.
im(f ) = {y ∈ B | ∃ x ∈ A such that y = f (x)}.
4. Two functions f and g are equivalent if dom(f ) = dom(g) and
∀x ∈ dom(f ), f (x) = g(x). We write f = g, or f (x) ≡ g(x).
Example 4
1. f : R → R, f (x) = x + 1
dom(f ) = R, im(f ) = R
2. f : Z → Q, f (x) = x +
1
2
dom(f ) = Z, im(f ) = {x ∈ Q | x 6∈ Z ∧ 2x ∈ Z}
3. f : R → R, f (x) = 2x .
dom(f ) = R, im(f ) = R+ .
2
- 7.1
Functions
4. f : R → R, f (x) = x + 1, g : R → R, g(x) =
P. Danziger
x2 −1
x−1
2
if x 6= 1
if x = 1
f and g are equivalent.
1.1
Inverse of a function
We can also consider inverses of functions.
1. Find the inverse of f : R → R, f (x) = x + 3.
If x and y in R, with y = x + 3, then x = y − 3.
So f −1 : R → R, f −1 (x) = x − 3.
√
2. Find the inverse of f : R+ → R, f (x) = x.
√
If x and y in R, with y = x, then x = y 2 .
So f −1 : R+ → R+ , f −1 (x) = x2 .
3. Find the inverse of f : R → R, f (x) = |x|
Let x, y ∈ R, with y = |x|, then y = ±x.
So f −1 consists of pairs of the form (x, x) and (x, −x) for every x ∈ R.
Note that though f is a function f −1 is not.
2
Useful Functions
2.1
The Logarithmic Function
(see also section 9.4)
Definition 5 Given a, x ∈ R+ , y ∈ R, with a 6= 1, and x = ay , we define the logarithmic function
as loga x = y. i.e. logarithms are the inverse operation to exponentiation:
loga : R+ → R, loga x = y ⇔ x = ay .
Theorem
6 For all a, x, y ∈ R+ , z ∈ R, with a 6= 1
loga x
1. a
=x
2. loga (az ) = z
3. loga (xz ) = z loga x
4. loga x + loga y = loga (xy)
5. loga x − loga y = loga ( xy )
Proof: Exercise
Suppose we wish to store a number n ∈ N, how many bits will it take?
We know that m bits can represent any number from 0 to 2m − 1.
Thus n can be stored in m = blog2 nc + 1 bits.
Unfortunately most calculators do not do log2 , they do either log10 , or ln = loge .
We can often estimate the result.
For example How many bits are needed to store 346?
28 < 346 < 29 , so 8 < log2 346 < 9, thus blog2 346c = 8. So we will need 9 bits.
3
- 7.1
Functions
Theorem 7 (Change of Base) Given a, b, x ∈ R+ , with a, b 6= 1, then loga x =
P. Danziger
logb x
.
logb a
Proof:
Let a, b, x ∈ N+ , and let y ∈ N be such that y = loga x.
Then x = ay . (Definition of loga )
Let n ∈ R be such that a = bn , i.e. n = logb a.
So x = ay = (bn )y , but (bn )y = bny = bn loga x . (Algebra)
Thus, taking logarithms on both sides of x = bn loga x , we get logb x = n loga x.
logb x
logb x
So logb x =
=
. n
logb a
All of these functions are arithmetic functions, we can have other kinds of functions as well.
2.2
The Identity Function
For any set X, we can define the identity function on X,
ıX : X → X, ı(x) = x.
2.3
Projections
Given sets A and B, we can define the projection from A × B onto either A or B.
p1 : A × B → A, p1 (a, b) = a
p2 : A × B → B, p2 (a, b) = b
In general given n, i ∈ N, with i ≤ n and sets A1 , A2 , . . . , An , we can define the projection onto the
ith coordinate by
pi : (A1 × A2 × . . . × An ) → Ai , pi (a1 , a2 , . . . , an ) = ai
2.4
Functions on Languages
Let Σ be an alphabet we can define functions on languages over Σ.
For example, consider ` : Σ∗ → Z, `(x) = the length of the string x.
Genreally we use Σ = {0, 1}.
Suppose we wish to encode a bit string x ∈ Σ∗ . We use an encoding function f : Σ∗ → Σ∗ . The
decoding function is then the inverse function f −1 : Σ∗ → Σ∗ . There are essentially 2 reasons to
encode a message - error detection/correction and cryptography.
2.4.1
32 bit CRC Checksum
32
Consider x ∈ Σ
. . × Σ32}, as a packet of n 32 bit words to be sent to a remote machine.
| × .{z
n
(We may padd x with zeros so that it is a multiple of 32 bits.)
Consider the function C : Σ∗ → Σ32 ,
C(x) = p1 (x) XOR p2 (x) XOR . . . XOR pn (x), where pi (x) is the ith 32 bit word of x (a projection).
When we are sending a packet of data we calculate the checksum C on the packet, and send this
as the last word in the packet.
The receiving machine also calculates the checksum on the packet as it comes in, it then checks its
4
- 7.1
Functions
P. Danziger
result against the checksum which has been sent, if they don’t match there has been an error, and
the receiving machine can request that the packet be resent.
Note that this error detection protocol is not 100% accurate. Since f is not one to one, it is possible
for two different packets to have the same checksum. However it is widely used.
2.4.2
Error correction
We wish to send a message over a noisy line.
Assume that we wish to send characters from the set {A, B, C, D, E, F, G}.
We encode each character of the message to a three digit number using the following encoding
function:
f (A) = 013, f (B) = 124, f (C) = 235, f (D) = 346,
f (E) = 450, f (F ) = 561, f (G) = 602.
Note that any two digits uniquely determine the third in a set.
If we assume that there is at most one error in each letter, then if we recieve, for example, 642, we
can correct this to 602, or G.
This is an example of an error correcting code, it can correct at most one error per character (A-G)
sent.
2.4.3
Hamming distance function
Given n ∈ N+
H : Σn × Σn → N, H(x, y) = the number of positions in which the strings x and y differ.
Example 8
Let n = 3
H(001, 101) = 1, since 001 and 101 differ in only one position.
H(001, 100) = 2, H(001, 110) = 3.
2.4.4
Encryption/Decryption
We wish to create a password for entry into a system.
n
Given n ∈ N+ , the number of characters in the password, consider the function f : N+ →
N+ , f (x1 , x2 , . . . , xn ) = x1 · x2 · . . . · xn .
Suppose that a given sequence x1 , x2 , . . . , xn satisfies f (x1 , x2 , . . . , xn ) = a, for some number a ∈ N.
a is stored in the system, we enter the sequence of numbers x1 , x2 , . . . , xn , the computer calculates
f (x1 , x2 , . . . , xn ). It then checks this against the stored answer, a, if they match we get access.
The advantage of this system is that it does not require the password itself to be stored anywhere
in the system, only the result of f on the password. Even if we could get to the stored answer, a
we would have to factor it in order to find x1 , x2 , . . . , xn .
2.5
Boolean Functions
A Boolean function is a function f : {0, 1}n → {0, 1}
Any truth table naturally defines a boolean function, with T = 1 and F = 0, n is the number of
inputs to the boolean expression.
5
- 7.1
Functions
p
1
1
0
0
2.6
q p⇒q
1
1
0
0
1
1
0
1
P. Danziger
f
f (1, 1) = 1
f (1, 0) = 0
f (0, 1) = 1
f (0, 0) = 1
Hashing functions
Suppose we have a large database which needs to be able to access a record quickly given an
identifying number x. We could define a large array A so that A[x] is the record corresponding to
x, however if x is large, or the array is sparse, this is an inefficient use of memory.
An example of such a database is student records. The record for the student is accessed by typing
in a 9 digit student number. However there are considerably less than 109 students, to define an
array of this size is both impractical and wasteful. The solution is a hashing function.
A hashing function h : N+ → N+ , takes a large number and returns a smaller one.
When looking for a space to put a record, x, we put it in h(x). If h(x) is already used (this is called
a collision) we try h(x) + 1, if this is used we try h(x) + 2 and so on until we find a free space.
When searching for a particular record, indexed by x, we look first in h(x), if the record we want
is not there we try h(x) + 1, then h(x) + 2, and so on until we find our record.
For example consider h(x) = bx/100c mod 5, 000, this allows for 5,000 spaces.
n
..
.
A[n]
..
.
1543 987-654-321
1544 986-543-763
1546
1547
1548 986-548-705
..
..
.
.
We first place 987-654-321, h(987-654-321) = 1543.
Now we wish to place 986543763, h(986-543-763) = 1543, but since this is used we go to the next
free slot, 1544.
The operation bx/100c removes the last two digits. In fact the first two digits contain information
(the year in which the student first registered), and so tend to be similar. Thus a much better idea
would be to remove the first two digits, rather than the last two.
3
One to One and Onto Functions
Definition 9 Given a function f : A → B
1. f is called one to one (1-1) or injective if
∀x1 , x2 ∈ A, f (x1 ) = f (x2 ) ⇒ x1 = x2 .
2. f is called onto or surjective if ∀y ∈ B, ∃ x ∈ A such that f (x) = y.
6
- 7.1
Functions
P. Danziger
3. If f is both one to one and onto (injective and surjective) it is called a one to one correspondence
or a bijection.
To show that a function f : A → B is 1-1
Let x1 , x2 ∈ A, with f (x1 ) = f (x2 )
Show that this implies that x1 = x2
To show that a function f : A → B is onto
Let y ∈ B.
Find an x ∈ A such that y = f (x).
To show that a function f : A → B is not 1-1
Find x1 , x2 ∈ A such that f (x1 ) = f (x2 ).
To show that a function f : A → B is not onto
Find y ∈ B such that 6 ∃ x ∈ A with f (x) = y.
Example 10
Decide whether the following functions are bijections, prove it.
1. f : Z → Z, f (x) = x + 1.
Let x1 , x2 ∈ Z, with f (x1 ) = f (x2 ).
Thus x1 + 1 = x2 + 1 ⇒ x1 = x2 . (Algebra)
Thus f is injective.
Let y ∈ Z, consider x ∈ Z such that y = x + 1, then x = y − 1.
So, given y ∈ Z, take x = y − 1 ∈ Z to get f (x) = y.
Thus f is surjective.
So f is a bijection.
2. g : Z → Z, g(n) = 3n.
Let n1 , n2 ∈ Z, with g(n1 ) = g(n2 ).
Thus 3n1 = 3n2 ⇒ n1 = n2 . (Algebra)
Thus g is 1-1.
1 is not a multiple of 3 for any integer n ∈ Z. (QRT)
Thus g is not surjective.
So g is not a bijection.
3. h : R → R, h(n) = 3n
Let x1 , x2 ∈ R, with h(x1 ) = h(x2 ).
Thus 3x1 = 3x2 ⇒ x1 = x2 . (Algebra)
Thus h is 1-1.
Let y ∈ R, consider x ∈ R such that y = 3x, then x = 13 y.
So, given y ∈ R, take x = 13 y ∈ R to get h(x) = y.
Thus h is surjective.
So h is a bijection.
7
- 7.1
Functions
P. Danziger
4. f : R → R, f (x) = x2 .
(−1)2 = 12 , so f is not 1-1.
There is no x ∈ R such that x2 = −1, so f is not onto.
f is not a bijection.
5. g : R+ → R, g(x) = x2
Let x1 , x2 ∈ R+ , with g(x1 ) = g(x2 ), i.e. x21 = x22 .
⇒ x1 = ±x2 , but x1 > 0, and x2 > 0, so x1 = x2 .
Thus g is 1-1.
There is no x ∈ R such that x2 = −1, so g is not onto.
So g is not a bijection.
6. h : R+ → R+ , h(x) = x2 .
h is 1-1 as for g above.
Let y ∈ R+ , consider x ∈ R+ so that y = h(x).
√
⇒ y = x2 ⇒ x = y.
So h is onto.
Thus h is a bijection.
Theorem 11 If f : X → Y is a bijection, then the inverse of f , f −1 : Y → X is also a bijection.
Proof:
Let f : X → Y be a bijection, and let f −1 : Y → X be its inverse.
i.e. ∀y ∈ Y, x ∈ X, f −1 (y) = x ⇔ y = f (x).
We wish to show that f −1 (x) is a bijection (1-1 and onto)
1-1 Let y1 , y2 ∈ Y, x ∈ X, with x = f −1 (y1 ) = f −1 (y2 ).
Then f (x) = y1 and f (x) = y2 , so since f is a function, y1 = y2 .
Onto Let x ∈ X. [We must find a y ∈ Y such that f −1 (y) = x.]
Take y ∈ Y to be y = f (x). 4
Composition of Functions
Definition 12 Given two functions f : X → Y and g : Y 0 → Z such that im(f ) ⊆ Y 0 , we may
define a new function g ◦ f : X → Z, (g ◦ f )(x) = g(f (x)). This is g composed with f .
Example 13
f : Z → Z, f (x) = x + 1, g : Z → Z, g(x) = x2
(f ◦ g)(x) = f (g(x)) = f (x2 ) = x2 + 1.
(g ◦ f )(x) = g(f (x)) = g(x + 1) = (x + 1)2 .
Theorem 14 For any function f : X → Y , f ◦ ıX = ıY ◦ f = f
8
- 7.1
Functions
P. Danziger
Proof:
Let x ∈ X. Since for any x ∈ X, ıX (x) = x, (f ◦ ıX )(x) = f (ıX (x)) = f (x).
Let y ∈ Y be such that y = f (x), then (ıY ◦ f )(x) = ıY (f (x)) = ıY (y) = y = f (x). Thus ıX is the identity of the operation of composition
Theorem 15 If f : X → Y is a bijection with inverse f −1 : Y → X then f ◦ f −1 = ıY and
f −1 ◦ f = ıX
Example 16
Let f : Q → Q, f (x) = 2x + 1
Exercise: Show that f is a bijection.
f −1 (x) = 12 (x − 1)
(f ◦ f −1 )(x) = f (f −1 (x)) = f ( 12 (x − 1)) = 2 12 (x − 1) + 1 = x.
(f −1 ◦ f )(x) = f −1 (f (x)) = f −1 (2x + 1) = 21 ((2x + 1) − 1) = x.
Theorem 17 If f : X → Y and g : Y → Z are both 1-1 functions, then g ◦ f is also 1-1.
Proof:
Let f : X → Y and g : Y → Z both be 1-1 functions.
Let x1 , x2 ∈ X be such that (g ◦ f )(x1 ) = (g ◦ f )(x2 )
Thus g(f (x1 )) = g(f (x2 )), but g is 1-1, so f (x1 ) = f (x2 ), but f is 1-1 so x1 = x2 . Example 18
f : R → R, f (x) = 10x , g : R → R, g(x) = 3x.
Note that both f and g are 1-1, though f is not onto.
(f ◦ g)(x) = f (g(x)) = f (3x) = 103x .
Let x1 , x2 ∈ R, with (f ◦ g)(x1 ) = (f ◦ g)(x2 ), i.e. 103x1 = 103x2 ⇒ x1 = x2 (Take logarithms on
both sides.)
Theorem 19 If f : X → Y and g : Y → Z are both onto functions, then g ◦ f is also onto.
Proof:
Let f : X → Y and g : Y → Z both be onto functions.
Thus every z ∈ Z is the image of some y ∈ Y under g, and this y is the image of some x ∈ X under
f.
So every z ∈ Z is the image of x ∈ X under g ◦ f . 9