TExcelTAD
Home › Discrete Mathematics › Functions

Functions

A function f:A→Bf: A \to B assigns to each element of AA exactly one element of BB. AA is the domain, BB the codomain, and f(a)f(a) is the value at aa. Functions in mathematics are like pure functions in code: same input, same output, no side effects. This lesson covers the vocabulary that counting and cardinality depend on: injective, surjective, bijective, composition and inverses.

Total and partial functions

A function is total if it is defined on every element of its domain and partial if it may be undefined for some. f(x)=1/xf(x) = 1/x on R\mathbb{R} is partial (undefined at 0). In code, a function that raises an exception or never returns on some inputs is partial. A Python dict is a finite function from its keys to its values.

The image (or range) of ff is the set of values actually taken: f(A)={f(a)∣a∈A}f(A) = \{f(a) \mid a \in A\}, a subset of the codomain.

Injective, surjective, bijective

Property Definition Picture
injective (one-to-one) f(a1)=f(a2)→a1=a2f(a_1) = f(a_2) \to a_1 = a_2 no two inputs share an output
surjective (onto) ∀b∈B. ∃a∈A. f(a)=b\forall b \in B.\ \exists a \in A.\ f(a) = b every element of BB is hit
bijective injective and surjective a perfect pairing of AA and BB
python · try it yourself
def describe(f, A, B):
    image = [f(a) for a in A]
    inj = len(set(image)) == len(image)
    surj = set(image) == set(B)
    return f"injective={inj}, surjective={surj}, bijective={inj and surj}"

A = range(6)
print("x -> x mod 3 on 0..5 to 0..2:", describe(lambda x: x % 3, A, range(3)))
print("x -> 2x on 0..5 to 0..11:   ", describe(lambda x: 2 * x, A, range(12)))
print("x -> 5 - x on 0..5 to 0..5: ", describe(lambda x: 5 - x, A, range(6)))
x -> x mod 3 on 0..5 to 0..2: injective=False, surjective=True, bijective=False
x -> 2x on 0..5 to 0..11:    injective=True, surjective=False, bijective=False
x -> 5 - x on 0..5 to 0..5:  injective=True, surjective=True, bijective=True

The properties depend on the codomain as well as the formula: x↦2xx \mapsto 2x from {0,…,5}\{0, \ldots, 5\} is a bijection onto the even numbers {0,2,…,10}\{0, 2, \ldots, 10\}, but not onto {0,…,11}\{0, \ldots, 11\}.

Composition and inverses

If f:A→Bf: A \to B and g:B→Cg: B \to C, the composition g∘f:A→Cg \circ f: A \to C is (g∘f)(a)=g(f(a))(g \circ f)(a) = g(f(a)). Apply ff first, then gg.

A bijection ff has an inverse f−1:B→Af^{-1}: B \to A with f−1(f(a))=af^{-1}(f(a)) = a and f(f−1(b))=bf(f^{-1}(b)) = b. Only bijections have two-sided inverses: if two inputs share an output, the inverse would not know which to return; if some bb is never hit, the inverse would have nothing to return.

Useful facts, each proved directly from the definitions:

  • The composition of two injections is an injection; of two surjections, a surjection; of two bijections, a bijection.
  • If g∘fg \circ f is injective then ff is injective; if g∘fg \circ f is surjective then gg is surjective.
python · try it yourself
# Caesar shift on 0..25 is a bijection; its inverse shifts back
shift = lambda k: (lambda x: (x + k) % 26)
f, f_inv = shift(3), shift(-3)
word = [ord(c) - 65 for c in "HELLO"]
enc = [f(x) for x in word]
print("encrypted:", "".join(chr(x + 65) for x in enc))
print("decrypted:", "".join(chr(f_inv(x) + 65) for x in enc))
print("f_inv o f is the identity:", all(f_inv(f(x)) == x for x in range(26)))
encrypted: KHOOR
decrypted: HELLO
f_inv o f is the identity: True

In software

Hash functions map an enormous domain (all strings) into a small codomain (64-bit numbers), so they cannot be injective: collisions must exist, and hash tables are built to handle them. Encryption must be a bijection on its message space, or decryption would be ambiguous. Database keys are injective by design: a primary key maps each row to a unique value. Lossless compression is injective, which is why no compressor can shrink every file (pigeonhole principle).

Exercises

Exercise 1: Classify

For f:Z→Zf: \mathbb{Z} \to \mathbb{Z}, classify (a) f(x)=x+7f(x) = x + 7, (b) f(x)=3xf(x) = 3x, (c) f(x)=x2f(x) = x^2, (d) f(x)=⌊x/2⌋f(x) = \lfloor x / 2 \rfloor.

Answer

(a) bijective; (b) injective only; (c) neither; (d) surjective only.

Solution

(a) Inverse x−7x - 7. (b) 3x1=3x2→x1=x23x_1 = 3x_2 \to x_1 = x_2, but 1 is never hit. (c) (−1)2=12(-1)^2 = 1^2, and −1-1 is never hit. (d) ⌊2y/2⌋=y\lfloor 2y / 2 \rfloor = y hits every yy, but f(0)=f(1)=0f(0) = f(1) = 0.

Exercise 2: Composition order

With f(x)=x+1f(x) = x + 1 and g(x)=2xg(x) = 2x, compute (g∘f)(3)(g \circ f)(3) and (f∘g)(3)(f \circ g)(3).

Answer

8 and 7.

Solution
(g∘f)(3)=g(4)=8,(f∘g)(3)=f(6)=7(g \circ f)(3) = g(4) = 8, \qquad (f \circ g)(3) = f(6) = 7

Composition is not commutative.

Exercise 3: Composition of injections

Prove: if f:A→Bf: A \to B and g:B→Cg: B \to C are injective, so is g∘fg \circ f.

Answer

g(f(a1))=g(f(a2))⇒f(a1)=f(a2)⇒a1=a2g(f(a_1)) = g(f(a_2)) \Rightarrow f(a_1) = f(a_2) \Rightarrow a_1 = a_2.

Solution

Assume (g∘f)(a1)=(g∘f)(a2)(g \circ f)(a_1) = (g \circ f)(a_2), that is g(f(a1))=g(f(a2))g(f(a_1)) = g(f(a_2)). Since gg is injective, f(a1)=f(a2)f(a_1) = f(a_2). Since ff is injective, a1=a2a_1 = a_2. ■\blacksquare

Exercise 4: Count functions (code)

How many functions are there from a 3-element set to a 2-element set, and how many of them are surjective? Enumerate them.

Answer

8 functions, 6 surjective.

Solution

A function is a choice of output for each of the 3 inputs: 23=82^3 = 8. Only the two constant functions miss an output.

python · try it yourself
from itertools import product

funcs = list(product([0, 1], repeat=3))       # (f(a), f(b), f(c))
onto = [f for f in funcs if set(f) == {0, 1}]
print(len(funcs), len(onto))
8 6

Test yourself

A function where no two inputs share an output is…

  1. surjective
  2. injective
  3. partial
  4. constant

Which functions have a two-sided inverse?

  1. All functions
  2. Injective ones
  3. Surjective ones
  4. Bijective ones

Why can't a 64-bit hash of arbitrary strings be injective?

  1. Hashes are random
  2. There are more possible strings than 64-bit values
  3. Strings are not numbers
  4. Python forbids it