Functions
Suppose we want to map one set to the other: given an element of set A (the input), return an element of set B (the output).
For example, suppose A={x|x is a user on our computer system} and B={x|x is a valid password}. We might want to know, given a user, what is that user's password: the input is the user (from A) and the output is that user's password (from B).
Let A and B be two sets. A function from A to B is an assignment of exactly one element from B to each element of A. We write f(a)=b if b∈B is the unique element assigned by the function f to the element a∈A. If f is a function from A to B, we write f:A→B.
It makes sense to model the password example above as a function because each user has exactly one password. Here are two other examples:
-
Suppose user root has password 123, john has password p455w0rd, and guest has password hello. Call the password function p. Then p(root)=123,p(john)=p455w0rd,p(guest)=hello. We can also visualize p as follows:
-
Consider a function g that assigns a grade to each student in the class:
Functions can be specified in several ways:
- writing out each pair explicitly: p(root)=123,…
- a diagram, as in the last two examples
- a formula: f(x)=2x2+1
Consider the function f:A→B. We call A the domain of f and B the codomain of f. Furthermore, if f(a)=b, then b is the image of a and a is the preimage of b. The set of all images of elements of A is called the range of f. For example:
-
In the grades example above:
- domain: {Tim,Jo,Lee,Tom,Mark}
- codomain: {A,B,C,D,F}
- range: {A,B,C,D}
- A is the image of Tim
- Tim is a preimage of A
-
Let f:Z→Z be defined by f(x)=x2.
- domain: Z
- codomain: Z
- range: {x|x is a non-negative perfect square}
If S is a subset of the domain, we can also look at its image: the subset of B that consists of the images of the elements in S: f(S)={f(s)|s∈S}. In the grades example above, g({Tim,Jo,Lee})={A,B}.