Functions and relations
About this lesson
A function takes each input to exactly one output. Follow one small example, f(x) = x mod 3 from A = {1, 2, 3, 4} to B = {0, 1, 2}: feed every input through the rule, draw each result as an arrow, and see that the arrows are just ordered pairs. Then learn what makes a relation a function, what the range is, and what onto and one-to-one mean, in maths and in Python.
Inputs and outputs
A function needs two sets. A = {1, 2, 3, 4} holds the inputs; it is called the domain. B = {0, 1, 2} holds the allowed outputs; it is the codomain. We write f: A → B, read 'f maps A to B'.
>>> A = {1, 2, 3, 4}
>>> B = {0, 1, 2}
>>> len(A), len(B)
(4, 3)
>>> 3 in A, 3 in B
(True, False)Read the lesson step by step
Inputs and outputs
A function needs two sets. A = {1, 2, 3, 4} holds the inputs; it is called the domain. B = {0, 1, 2} holds the allowed outputs; it is the codomain. We write f: A → B, read 'f maps A to B'.
>>> A = {1, 2, 3, 4} >>> B = {0, 1, 2} >>> len(A), len(B) (4, 3) >>> 3 in A, 3 in B (True, False)A rule for each input
Our rule is f(x) = x mod 3: divide x by 3 and keep the remainder. Feed in 4: 4 = 1 × 3 + 1, so f(4) = 1. Python writes mod as %. We draw it as an arrow from 4 to 1.
>>> def f(x): ... return x % 3 ... >>> f(4) 1 >>> divmod(4, 3) # (quotient, remainder) (1, 1)Map every input
Do the same for every input: f(1) = 1, f(2) = 2 and f(3) = 0. Write each arrow as an ordered pair (input, output), input first. The four pairs are (1, 1), (2, 2), (3, 0) and (4, 1).
>>> def f(x): return x % 3 ... >>> [f(x) for x in [1, 2, 3, 4]] [1, 2, 0, 1] >>> [(x, f(x)) for x in [1, 2, 3, 4]] [(1, 1), (2, 2), (3, 0), (4, 1)]Relations are sets of pairs
Any set of ordered pairs (a, b), with a from A and b from B, is a relation from A to B. Each pair is one arrow. Order matters: (3, 0) is in our relation, but (0, 3) is not.
>>> R = {(1, 1), (2, 2), (3, 0), (4, 1)} >>> (3, 0) in R True >>> (0, 3) in R # order matters FalseExactly one arrow out
A relation is a function when every input has exactly one arrow leaving it: not zero, not two. Here 1, 2, 3 and 4 each have one. Two inputs may share an output: 1 and 4 both go to 1.
>>> R = {(1, 1), (2, 2), (3, 0), (4, 1)} >>> [len([b for (a, b) in R if a == x]) for x in [1, 2, 3, 4]] [1, 1, 1, 1]Not a function
Now add the pair (1, 2). The input 1 has two arrows out, so f(1) has no single answer: this relation is not a function. A relation that leaves some input with no arrow is not a function either.
>>> R = {(1, 1), (2, 2), (3, 0), (4, 1), (1, 2)} >>> sorted(b for (a, b) in R if a == 1) [1, 2]Range and onto
The range is the set of outputs that are actually hit. {f(x) for x in A} gives {0, 1, 2}, all of B, so f is onto (surjective). The codomain only lists the allowed outputs; a range can be smaller.
>>> A = {1, 2, 3, 4} >>> {x % 3 for x in A} # the range {0, 1, 2} >>> {x % 3 for x in A} == {0, 1, 2} TrueNot one-to-one
f is one-to-one (injective) when different inputs always give different outputs. Ours is not: f(1) = 1 and f(4) = 1. Seeing the output 1, you cannot tell which input made it, so f has no inverse.
>>> outs = [x % 3 for x in [1, 2, 3, 4]] >>> outs [1, 2, 0, 1] >>> len(set(outs)) == len(outs) # one-to-one? FalseA dict is a function
A Python dict stores a function as pairs: d = {x: f(x) for x in A} gives {1: 1, 2: 2, 3: 0, 4: 1}. A key can appear only once, so each input keeps exactly one output. d[4] is 1.
>>> d = {x: x % 3 for x in [1, 2, 3, 4]} >>> d {1: 1, 2: 2, 3: 0, 4: 1} >>> d[4] 1 >>> {1: 1, 1: 2} # a key cannot appear twice {1: 2}
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01With f(x) = x mod 3, what is f(7)?
7 = 2 × 3 + 1, so the remainder is 1. In Python, 7 % 3 gives 1.
02A = {1, 2} and B = {5, 6}. Which relation from A to B is a function?
Each input, 1 and 2, has exactly one arrow out. The first gives 1 two outputs, the second gives 2 none, and the last has its pairs the wrong way round (inputs must come first).
03f: {1, 2, 3, 4} → {0, 1, 2} with f(x) = x mod 3. What is the range of f?
The outputs are 1, 2, 0 and 1, so the set of outputs actually hit is {0, 1, 2}. Here the range equals the codomain, so f is onto.
04Why is f(x) = x mod 3 on {1, 2, 3, 4} not one-to-one?
One-to-one means different inputs always give different outputs. 1 and 4 both give 1, so it fails. (A smaller codomain is allowed; it just makes one-to-one impossible here.)
05What does Python print for {1: 'a', 1: 'b'}?
A dict key can appear only once, so the later value replaces the earlier one. That is why a dict always describes a function: each key has exactly one value.
Glossary
- Ordered pair
- Two items written (a, b) where the order matters: (1, 2) and (2, 1) are different pairs.
- Relation
- A set of ordered pairs (a, b) with a from a set A and b from a set B. It records which things are linked.
- Function
- A relation from A to B in which every element of A appears as the first item of exactly one pair. Written f: A → B.
- Domain
- The set of allowed inputs of a function (A in f: A → B).
- Codomain
- The set the outputs are allowed to come from (B in f: A → B).
- Range (image)
- The set of outputs that are actually produced. It is part of the codomain and can be smaller.
- Onto (surjective)
- Every element of the codomain is the output of at least one input.
- One-to-one (injective)
- Different inputs always give different outputs. A function that is both one-to-one and onto is a bijection.
Notes and sources
- f: A → B is read 'f from A to B'. If f(x) = y, then y is the image of x, and x is a preimage of y. The range is the set of all images.
- Some books, such as MIT's Mathematics for Computer Science, call any relation with at most one arrow out of each input a function, and say total function when every input has exactly one. This lesson uses the more common meaning: exactly one.
- Python's % gives a result with the sign of the divisor, so -1 % 3 is 2, which matches maths mod. In C and Java, -1 % 3 is -1.
- A function that is both one-to-one and onto is a bijection. Only a bijection has an inverse function that undoes it. Example: g(x) = x + 1 from {1, 2, 3} to {2, 3, 4}, whose inverse subtracts 1.
- In programming, 'function' means a named piece of code. A Python function only computes a mathematical function if it always returns the same output for the same input, like f here. random.random() does not.
- Rosen, Discrete Mathematics and Its Applications, 8th ed., sections 2.3 Functions and 9.1 Relations
- Hammack, Book of Proof, 3rd ed., chapters 11 Relations and 12 Functions (free)
- Lehman, Leighton and Meyer, Mathematics for Computer Science (MIT), section 4.4 Binary Relations
- MIT OpenCourseWare 6.042J Mathematics for Computer Science
- Python reference: binary arithmetic operations (%)
- Python library: mapping types, dict
- Python tutorial: sets and dictionaries (comprehensions)
Quiz Unlocks after the last step.