Functions, Domain and Range
A function is a rule that takes an input and gives back exactly one output. Calculus is the study of how functions change, so before anything else you need to be fluent in what a function is, which inputs it accepts (its domain) and which outputs it can produce (its range).
The definition
A function from a set to a set assigns to each element of exactly one element of .
- is the input or independent variable.
- (often written ) is the output or dependent variable.
- The domain is the set of allowed inputs.
- The range is the set of outputs that actually occur.
The key word is exactly one. The rule " is a number whose square is " is not a function, because gives both and . On a graph, this is the vertical line test: a curve is the graph of a function only if no vertical line crosses it more than once. A circle fails the test; the upper half of a circle passes.
A Python function is the same idea in code, as long as it is pure (no randomness, no hidden state): the same argument always returns the same value.
def f(x):
return x**2 + 1
for x in [-2, -1, 0, 1, 2, 3]:
print(f"f({x:2}) = {f(x)}")f(-2) = 5
f(-1) = 2
f( 0) = 1
f( 1) = 2
f( 2) = 5
f( 3) = 10
Different inputs may share an output (); that is allowed. One input with two outputs is not.
Finding the domain
If no domain is stated, the domain is every real number for which the formula makes sense. In real-number calculus three things go wrong:
| Problem | Rule | Example | Domain |
|---|---|---|---|
| Division by zero | all except 3 | ||
| Even root of a negative | |||
| Log of a non-positive number |
When several restrictions apply, all must hold at once. For you need and , so the domain is .
Interval notation is the usual shorthand: square brackets include an endpoint, round brackets exclude it, and always gets a round bracket.
| Inequality | Interval |
|---|---|
| all reals |
SymPy can find domains and ranges for you:
import sympy as sp
from sympy.calculus.util import continuous_domain, function_range
x = sp.symbols('x', real=True)
R = sp.S.Reals
print(continuous_domain(1 / (x - 3), x, R))
print(continuous_domain(sp.sqrt(x + 2), x, R))
print(continuous_domain(sp.log(5 - x), x, R))
print(continuous_domain(sp.sqrt(x) / (x - 4), x, R))
print(function_range(x**2 + 1, x, R))Union(Interval.open(-oo, 3), Interval.open(3, oo))
Interval(-2, oo)
Interval.open(-oo, 5)
Union(Interval.Ropen(0, 4), Interval.open(4, oo))
Interval(1, oo)
Interval.Ropen(0, 4) means "open on the right", that is .
Finding the range
The range is usually harder than the domain. Two approaches:
- Reason about the formula. is never negative, so , and every value from 1 upward is reached. The range of is .
- Solve for . For : . That works for every except 0, so the range is all reals except 0.
A plot is a good sanity check:
import numpy as np
import matplotlib.pyplot as plt
xs = np.linspace(-2, 10, 400)
ys = np.sqrt(xs + 2)
plt.figure(figsize=(6, 4))
plt.plot(xs, ys)
plt.scatter([-2], [0], color="C3", zorder=3, label="starts at (−2, 0)")
plt.xlabel("x"); plt.ylabel("√(x + 2)")
plt.grid(alpha=0.3); plt.legend()
plt.title("Domain [−2, ∞), range [0, ∞)")
print("smallest output:", ys.min(), " output at x = 10:", round(ys[-1], 4))smallest output: 0.0 output at x = 10: 3.4641
Ways to describe a function
| Form | Example | Good for |
|---|---|---|
| Formula | exact work, calculus | |
| Table | hourly temperature readings | measured data |
| Graph | a stock chart | seeing the shape |
| Code | def area(r): return math.pi * r * r |
computing |
| Piecewise | rules that change by region |
In software
- Pure functions in programming match the mathematical definition, which is why they are easy to test, cache (memoise) and run in parallel.
- Domain errors are real bugs.
math.sqrt(-1)raisesValueError: math domain errorandmath.log(0)raises too; NumPy returnsnanor-infwith a warning. Validating inputs is checking the domain. - Type signatures such as
def f(x: float) -> floatdescribe the domain and codomain, though not the exact range. - Lookup tables in games and embedded code store a function as a table of precomputed outputs.
Exercises
Exercise 1: Evaluate
For , find , and .
Answer
, , .
Solution
Exercise 2: Domains by hand
Give the domain of (a) , (b) , (c) .
Answer
(a) all reals except −3 and 3; (b) ; (c) .
Solution
- (a) at , so exclude both.
- (b) gives .
- (c) needs and the denominator needs .
Exercise 3: Range by solving for
Find the range of .
Answer
All reals except 2.
Solution
import sympy as sp
from sympy.calculus.util import function_range
x = sp.symbols('x', real=True)
f = (2*x + 1) / (x - 1)
print(function_range(f, x, sp.Interval.open(1, sp.oo)))
print(function_range(f, x, sp.Interval.open(-sp.oo, 1)))Interval.open(2, oo)
Interval.open(-oo, 2)
Together the two branches cover everything except 2.
Exercise 4: A safe square root
Write a Python function safe_sqrt(x) that returns None outside the domain of and the root otherwise. Test it on −4, 0 and 2.25.
Answer
None, 0.0, 1.5.
Solution
import math
def safe_sqrt(x):
if x < 0:
return None # outside the domain [0, ∞)
return math.sqrt(x)
for v in [-4, 0, 2.25]:
print(v, safe_sqrt(v))-4 None
0 0.0
2.25 1.5
Test yourself
What is the domain of ?
Which of these is NOT a function of ?
What is the range of ?
- all reals