Functions
Contents
Functions#
Basic definitions#
Let us give a somewhat informal definition of a function.
Let \(\mathcal{D}\) and \(\mathcal{C}\) be non-empty sets and let \(f\) be a rule by which every element \(x \in \mathcal{D}\) is assigned a unique element \(y \in \mathcal{C}\). The ordered triple \((\mathcal{D}, \mathcal{C}, f)\) is called a function (also map or mapping). We write \(f: \mathcal{D} \to \mathcal{C}\).
Note
What that means is that the following three functions are all different despite their assignment \(f\) being the same:
Functions \((\mathcal{D}_1, \mathcal{C}_1, f_1)\) and \((\mathcal{D}_2, \mathcal{C}_2, f_2)\) are equal iff \(\mathcal{D}_1 = \mathcal{D}_2, \mathcal{C}_1 = \mathcal{C}_2, f_1 = f_2\).
\(\mathcal{D(f)}\) is called the domain of the function \(f\)
\(\mathcal{C(f)}\) is called the codomain of the function \(f\)
\(f(x)\) is the value (sometimes called output) of \(f\) at point \(x\)
Example:
The figure on the left shows a function where \( a \mapsto f(a) = 1, b \mapsto f(b) = 2, c \mapsto f(c)=2 \). The figure on the right does not represent a function since \(c\) is assigned both \(2\) and \(4\).
Graph#
The graph of the function is the set
Therefore, \( G(f) \subseteq \mathcal{D} \times \mathcal{C} = \{ (d, c) : d \in \mathcal{D}, c \in \mathcal{C} \} \) .
From above examples we have the following graphs:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 2, figsize=(8, 3), sharey=True)
ax[0].scatter(['a', 'b', 'c'], y = [1, 2, 2])
ax[0].set_ylabel('$f(x)$')
ax[1].scatter(['a', 'b', 'c', 'c'], y = [1, 2, 2, 4], zorder=10)
ax[1].plot(['c', 'c'], [0, 4], '--k', alpha=0.3)
ax[1].set_title('Not a graph of a function!')
for i in [0, 1]:
ax[i].set_yticks([1, 2, 3, 4])
ax[i].spines['right'].set_visible(False)
ax[i].spines['top'].set_visible(False)
ax[i].set_ylim(0.8, 4.2)
ax[i].set_xlabel('x')
plt.show()
The graph on the right is not a graph of a function because two points lie on the vertical line drawn through \(c\), i.e. \(c\) is assigned two different values \(f(c)\). This is a contradiction to our definition of a function. Sometimes this is called the vertical line test.
Restriction#
Let \(f : \mathcal{D} \to \mathcal{C}\) be a function and let \(A \subseteq \mathcal{D}\). We can define a restriction of \(f\) to the domain of \(A\) as \(\left.f\right|_A : A \to \mathcal{C}\) with \(\left.f\right|_A (x) = f(x), \forall x \in A\).
Image#
Let \(f : \mathcal{D} \to \mathcal{C}\) be a function and let \(A \subseteq \mathcal{D}\). The image of a subset \(A\) under \(f\) is the set \(f(A) \subseteq \mathcal{C}\) such that:
If \(A = \mathcal{D}\), i.e. the entire domain, we call the set \(f(D)\) the image or range of \(f\). In other words, the image is a set of all values that \(f(x)\) can take for all \(x\) in the domain.
From our example of a function above (left figure), let us take \(A = \{ a, c \}\). The image of a subset \(A\) is then:
Similarly for the entire domain:
Inverse image#
Let \(f : \mathcal{D} \to \mathcal{C}\) be a function and let \(E \subseteq \mathcal{C}\). The inverse image (or preimage) of \(E\) under function \(f\) is the set
If we take \(E = \mathcal{C}\), then \(f^{-1}(\mathcal{C}) = \mathcal{D}\). In other words, we are looking for the values \(x \in \mathcal{D}\) for which \(f(x)\) takes specified values.
Again from the example above, let us consider \( E_1 = \{ 1 \}, E_2 = \{ 2 \}, E_3 = \{ 1, 2 \} \):
Or some may appreciate a more applied example: let \(f: \mathbb{R} \to \mathbb{R}\) be a function with \(f(x) = x^2\), then
The last point means that there are no \(x \in \mathbb{R}\) such that \(f(x) = x^2\) is a negative number.
Or a more applied example: consider \(f: \mathbb{R} \to \mathbb{R}\) where \(f(x) = x - 5\) (we can simply write \(x \mapsto x - 5\)) and \(A = (-3, 1]\). Let us find the preimage \(f^{-1}(A)\) graphically and by calculating it.
This agrees with our findings on the graph.
Real functions#
If \(\mathcal{D}\) and \(\mathcal{C}\) are subsets of \(\mathbb{R}\) then we call a function \(f : \mathcal{D} \to \mathcal{C}\) a real-valued function of a real variable or, simply, a real function.
The largest subset of \(\mathbb{R}\) for which the function \(f\) is defined is called the natural domain.
Examples. We give further examples in the notebook elementary_functions.
Function |
Natural domain |
Image |
---|---|---|
\( f(x) = x^2 \) |
\( \mathbb{R} \) |
\( [0, + \infty) \) |
\( f(x)=\sqrt{x} \) |
\( [0, + \infty) \) |
\( [0, + \infty) \) |
\( f(x) = \frac{1}{x} \) |
\( \mathbb{R} \backslash \{ 0 \} \) |
\( (- \infty, 0) \cup (0, + \infty) \) |
Monotonic functions#
Many useful properties of real functions can easily be seen on the graph of the function. One of those is whether a function is monotonic, i.e. whether it is always increasing or decreasing on the whole domain. For a function \(f : A \to \mathbb{R}, A \subseteq \mathbb{R}\), we say that it is:
increasing on \(A\) if \( (x_1 < x_2) \Rightarrow (f(x_1) \leq f(x_2)) \) for all \( x_1, x_2 \in A \)
strictly increasing on \(A\) if \( (x_1 < x_2) \Rightarrow (f(x_1) < f(x_2)) \) for all \( x_1, x_2 \in A \)
decreasing on \(A\) if \( (x_1 < x_2) \Rightarrow (f(x_1) \geq f(x_2)) \) for all \( x_1, x_2 \in A \)
strictly decreasing on \(A\) if \( (x_1 < x_2) \Rightarrow (f(x_1) > f(x_2)) \) for all \( x_1, x_2 \in A \)
import numpy as np
def f(x):
if x <= 0:
y = x**3 + 1
elif x <= 1:
y = 1
else:
y = (x-1)**3 + 1
return y
titles = [['Increasing', 'Strictly increasing'],
['Decreasing', 'Strictly decreasing']]
x1 = np.linspace(-2, 3, 50)
x2 = np.linspace(-1.3, 1.3, 50)
fig, ax = plt.subplots(2, 2)
ax[0, 0].plot(x1, list(map(f, x1)))
ax[0, 1].plot(x2, np.tan(x2))
ax[1, 0].plot(x1, -np.array(list(map(f, x1))))
ax[1, 1].plot(x2, -np.tan(x2))
for i in range(2):
for j in range(2):
ax[i, j].spines['right'].set_visible(False)
ax[i, j].spines['top'].set_visible(False)
ax[i, j].set_title(titles[i][j])
ax[i, j].set_xticks([])
ax[i, j].set_yticks([])
plt.show()
Even and odd functions#
Even and odd functions satisfy particular symmetry relations. A function \(f : A \to \mathbb{R}, A \subseteq \mathbb{R}\) is:
even on \(A\) if \(f(-x) = f(x)\) for all \(x \in A\)
odd on \(A\) if \(f(-x) = -f(x)\) for all \(x \in A\)
The names ‘even’ and ‘odd’ originate from the power function \(f(x) = x^n\), which is even for even \(n\) and odd for odd \(n\). For example, \(f(x)=x^2\) and \(f(x)=x^3\) below:
x = np.linspace(-2, 2, 100)
y1 = x**2
y2 = x**3 / 2
fig, ax = plt.subplots(1, 2)
ax[0].plot(x, y1)
ax[1].plot(x, y2)
ax[0].plot([-1.4, 1.4], [1.4**2, 1.4**2], '--k')
ax[0].plot([-1.8, 1.8], [1.8**2, 1.8**2], '--k')
ax[1].plot([-1.4, 1.4], [-1.4**3 / 2, 1.4**3 /2], '--k')
ax[1].plot([-1.8, 1.8], [-1.8**3 / 2, 1.8**3 /2], '--k')
ax[0].set_title(r'$f(x) = x^2$')
ax[1].set_title(r'$f(x) = x^3$')
for i in range(2):
ax[i].spines['left'].set_position('zero')
ax[i].spines['bottom'].set_position('zero')
ax[i].spines['right'].set_visible(False)
ax[i].spines['top'].set_visible(False)
ax[i].set_yticks([])
ax[i].set_xticks([])
plt.show()