Skip to main content

Functions

A function is a relation ff between AA and BB where for each aAa \in A there is a exactly one bBb \in B such that (a,b)f(a,b) \in f. In other words:

((a,b)f(a,c)f)b=caA!bB(a,b)f((a,b) \in f \land (a,c) \in f) \mapsto b = c \\ \forall a \in A \exists !b \in B (a,b) \in f

We write

f:ABf : A \mapsto B

Domain & Range

For a function f:ABf : A \mapsto B, AA is the domain of ff and BB is the co-domain.

The set {f(x):xA}\{f(x): x \in A \} is the range of ff.

tip
  • AA is the possible inputs of ff
  • BB is the return type of ff
  • range is the all possible outputs from ff

Domain Problem

Consider the function f(x)=1xf(x) = \frac{1}{x}, ff is not defined for x=0x=0, but we still call it a function with domain R{0}\mathbb{R} \setminus \{0\}

f:R{0}Rf : \mathbb{R} \setminus \{0\} \mapsto \mathbb{R}

Function Examples

  • {(1,π),(2,β),(3,γ)}\{(1, \pi), (2, \beta), (3,\gamma)\}
  • {(1,π),(2,π),(3,π)}\{(1, \pi), (2, \pi), (3,\pi)\}
  • {(a,b)R2:b=a2}\{(a,b) \in \mathbb{R}^2 : b = a^2 \}

Non-Math Function Examples

  • f(x)f(x) - input a student number, output the last name of the QUT student
  • f(x)f(x) - input a bit string xx, output the MD5 hash of the bit string.

Non-Function Examples

danger
  • {(1,π),(1,γ)}\{(1, \pi), (1, \gamma)\}
  • {(a,b)R2:a=b2}\{(a,b) \in \mathbb{R}^2 : a = b^2 \}
    • let a=4a=4, then bb could be 2 or -2.
  • ,\leq, \geq

Composing Functions

Suppose we have f:ABf : A \mapsto B, g:BCg : B \mapsto C. Then we can define:

(gf)(x)=g(f(x))(g \circ f)(x) = g(f(x))

This is called gg of ff of xx.

Inverse Function

Some functions have a partner, called its inverse. Give f:ABf : A \mapsto B, the inverse f1:BAf^{-1} : B \mapsto A is a function such that:

xA(f1f(x)=x)\forall x \in A (f^{-1} \circ f(x) = x )
note
  • The range of ff must match the domain of f1f^{-1}
  • The domain of ff must match the range of f1f^{-1}

Not all functions have inverses. For example, f(x)=0f(x) = 0 has no inverse.

Functions in Python

Python has its own notion of what a function is, and it isn't the same!

However, Functions in Python that are side-effect free and deterministic are also functions in the mathematical sense.

note

What is side-effect?

  • modify the state of the program (including I/O).

What is a deterministic function?

  • Deterministic means that there is no randomness.

Function Examples in Python

# function in the mathematical sense
def f(x):
return x + 1

x = 10
# modifies state, not a function in mathematical sense
def changeX():
global x
x += 1

# not deterministic, not a function in mathematical sense
import random
def rand():
return random.randint(1,6)

Partial Functions

A partial function ff from a set AA to a set BB is a function from a subset of AA to BB.

In terms of relation, partial function ff contains at most one pair (a,b) for every aAa \in A. This is different from a function which requires exactly one pair.

Dictionaries as Partial Functions

References