Functions
A function is a rule that assigns to every input exactly one output. Programmers write functions every day, and the mathematical idea is the same one with sharper vocabulary: domain, codomain, range, graph. This lesson defines them and tours the classic families of functions you will meet throughout pre-calculus and calculus.
Definition
A function from a set to a set , written , assigns to each element of exactly one element of .
- is the domain: the allowed inputs.
- is the codomain: where outputs are allowed to land.
- The range (or image) is the set of outputs actually produced: . It is a subset of the codomain.
"Exactly one" is the whole point. A rule that gives two outputs for some input, like "the with ", is not a function. A rule may, however, give the same output for different inputs: sends both 3 and −3 to 9.
The graph of is the set of points . A curve in the plane is the graph of a function of exactly when every vertical line meets it at most once: the vertical line test. A circle fails it; its top half passes.
Finding domain and range
When a function is given by a formula with no domain stated, the domain is every real for which the formula makes sense. The usual restrictions:
| Expression | Restriction |
|---|---|
| (even roots) | |
Example. . We need and , so the domain is .
The range takes more thought. For on all of , squares are , so , and every value is reached (take ). The range is . SymPy can do both jobs:
import sympy as sp
from sympy.calculus.util import continuous_domain, function_range
x = sp.Symbol("x", real=True)
f = sp.sqrt(x - 2) / (x - 5)
g = x**2 + 1
h = 1 / (x**2 + 4)
print("domain of f:", continuous_domain(f, x, sp.S.Reals))
print("range of g :", function_range(g, x, sp.S.Reals))
print("range of h :", function_range(h, x, sp.S.Reals))
print("f(11) =", f.subs(x, 11))domain of f: Union(Interval.Ropen(2, 5), Interval.open(5, oo))
range of g : Interval(1, oo)
range of h : Interval.Lopen(0, 1/4)
f(11) = 1/2
Interval.Lopen(0, 1/4) means , open on the left. Indeed is largest () at and approaches 0 but never reaches it.
Properties to look for
| Property | Meaning | Example |
|---|---|---|
| Even | : graph symmetric about the y-axis | , |
| Odd | : symmetric about the origin | , |
| Increasing on | everywhere | |
| One-to-one | different inputs give different outputs | yes, no |
| Periodic | for some | , |
| Zeros | with (x-intercepts) | : |
One-to-one functions are exactly those with inverses, which is the subject of Composition and Inverse Functions.
The classic families
These shapes come back again and again. Each has its own lesson in this section.
| Family | Typical formula | Domain | Range | Lesson |
|---|---|---|---|---|
| Linear | () | slope | ||
| Quadratic | half-line from the vertex | parabolas | ||
| Polynomial | depends | Polynomial Functions | ||
| Rational | depends | Rational Functions | ||
| Root | ||||
| Exponential | Exponential and Logarithmic | |||
| Logarithmic | same | |||
| Trigonometric | , | Graphs of the Trig Functions | ||
| Absolute value | Piecewise Functions |
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 601)
xp = np.linspace(0.01, 3, 300)
fig, axes = plt.subplots(2, 4, figsize=(9, 4.5))
panels = [("2x − 1", x, 2 * x - 1), ("x² − 2", x, x**2 - 2), ("x³ − 2x", x, x**3 - 2 * x),
("√x", xp, np.sqrt(xp)), ("2ˣ", x, 2.0**x), ("ln x", xp, np.log(xp)),
("sin x", x, np.sin(x)), ("|x|", x, np.abs(x))]
for ax, (name, xs, ys) in zip(axes.flat, panels):
ax.plot(xs, ys, color="purple")
ax.axhline(0, color="gray", lw=0.6); ax.axvline(0, color="gray", lw=0.6)
ax.set_title(name, fontsize=10); ax.set_ylim(-4, 4); ax.tick_params(labelsize=7)
fig.tight_layout()
print("panels drawn:", len(panels))panels drawn: 8
The calculus course's function lessons and types of functions pick up from here with limits and rates of change in mind.
In software
- Pure functions are mathematical functions: same input, same output, no side effects. They are easy to test, cache (memoize) and run in parallel. A function that reads the clock or a global variable is not a function in the mathematical sense, because the same input can give different outputs.
- Type signatures are domain and codomain.
def area(r: float) -> floatdeclares . Runtime checks (if r < 0: raise ValueError) restrict the domain further. - Lookup tables. A Python
dictis a function with a finite domain (its keys). Inverting a dict only works if it is one-to-one. - Range checks in tests. Knowing that the range of a sigmoid is tells you a returned 1.3 is a bug.
from functools import lru_cache
calls = 0
@lru_cache(maxsize=None)
def slow_square(n: int) -> int: # pure: safe to cache
global calls
calls += 1
return n * n
print([slow_square(n) for n in (3, -3, 3, 4, 3)], "computed", calls, "times")
grade = {"A": 4, "B": 3, "C": 2, "D": 1, "F": 0} # a function on a finite domain
print("domain:", sorted(grade), " range:", sorted(set(grade.values())))
print("one-to-one?", len(set(grade.values())) == len(grade))[9, 9, 9, 16, 9] computed 3 times
domain: ['A', 'B', 'C', 'D', 'F'] range: [0, 1, 2, 3, 4]
one-to-one? True
Exercises
Exercise 1: Function or not?
Which of these are functions of ? (a) , (b) , (c) , (d) .
Answer
(a) and (c) are functions; (b) and (d) are not.
Solution
(b) gives , two values for : a circle fails the vertical line test. (d) gives for , also two values. (a) and (c) give exactly one for each .
Exercise 2: Domains
Find the domain of and of .
Answer
: all reals except , . : .
Solution
import sympy as sp
from sympy.calculus.util import continuous_domain
x = sp.Symbol("x", real=True)
print(continuous_domain(1 / (x**2 - 9), x, sp.S.Reals))
print(continuous_domain(sp.sqrt(6 - 2*x), x, sp.S.Reals))Union(Interval.open(-oo, -3), Interval.open(-3, 3), Interval.open(3, oo))
Interval(-oo, 3)
Exercise 3: Range
Find the range of and of .
Answer
: . : .
Solution
, so , with 5 reached at ; takes every smaller value. For , runs over , so runs over and over .
Exercise 4: Even, odd or neither
Classify , and .
Answer
even, odd, neither.
Solution
import sympy as sp
x = sp.Symbol("x")
for name, e in [("f", x**4 - 3*x**2), ("g", x**3 + x), ("h", x**2 + x)]:
neg = e.subs(x, -x)
print(name, "even" if sp.expand(neg - e) == 0 else "odd" if sp.expand(neg + e) == 0 else "neither")f even
g odd
h neither
Exercise 5: Is this pure?
Which of these Python functions are functions in the mathematical sense (same input, always same output)? (a) lambda x: x * 2, (b) lambda x: x + random.random(), (c) lambda xs: sorted(xs)[0], (d) a function that returns x + counter and then increments a global counter.
Answer
(a) and (c) are; (b) and (d) are not.
Solution
(b) depends on a random number and (d) on hidden state that changes between calls, so the same input can produce different outputs. (c) always returns the minimum of its input list.
Test yourself
What is the domain of ?
- All real numbers
Which statement about on is true?
- It is not a function because
- Its range is
- It is a function, but not one-to-one
- It is odd
The vertical line test checks whether…
- a function is one-to-one
- a curve is the graph of a function of x
- a function is continuous
- a function is increasing