Functions
A function assigns to each element of exactly one element of . is the domain, the codomain, and is the value at . 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. on 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 is the set of values actually taken: , a subset of the codomain.
Injective, surjective, bijective
| Property | Definition | Picture |
|---|---|---|
| injective (one-to-one) | no two inputs share an output | |
| surjective (onto) | every element of is hit | |
| bijective | injective and surjective | a perfect pairing of and |
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: from is a bijection onto the even numbers , but not onto .
Composition and inverses
If and , the composition is . Apply first, then .
A bijection has an inverse with and . Only bijections have two-sided inverses: if two inputs share an output, the inverse would not know which to return; if some 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 is injective then is injective; if is surjective then is surjective.
# 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 , classify (a) , (b) , (c) , (d) .
Answer
(a) bijective; (b) injective only; (c) neither; (d) surjective only.
Solution
(a) Inverse . (b) , but 1 is never hit. (c) , and is never hit. (d) hits every , but .
Exercise 2: Composition order
With and , compute and .
Answer
8 and 7.
Solution
Composition is not commutative.
Exercise 3: Composition of injections
Prove: if and are injective, so is .
Answer
.
Solution
Assume , that is . Since is injective, . Since is injective, .
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: . Only the two constant functions miss an output.
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…
- surjective
- injective
- partial
- constant
Which functions have a two-sided inverse?
- All functions
- Injective ones
- Surjective ones
- Bijective ones
Why can't a 64-bit hash of arbitrary strings be injective?
- Hashes are random
- There are more possible strings than 64-bit values
- Strings are not numbers
- Python forbids it