TExcelTAD

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 ff from a set AA to a set BB, written f ⁣:A→Bf\colon A \to B, assigns to each element xx of AA exactly one element f(x)f(x) of BB.

  • AA is the domain: the allowed inputs.
  • BB is the codomain: where outputs are allowed to land.
  • The range (or image) is the set of outputs actually produced: {f(x)∣x∈A}\{f(x) \mid x \in A\}. It is a subset of the codomain.

"Exactly one" is the whole point. A rule that gives two outputs for some input, like "the yy with y2=xy^2 = x", is not a function. A rule may, however, give the same output for different inputs: f(x)=x2f(x) = x^2 sends both 3 and −3 to 9.

The graph of ff is the set of points (x,f(x))(x, f(x)). A curve in the plane is the graph of a function of xx exactly when every vertical line meets it at most once: the vertical line test. A circle fails it; its top half y=1−x2y = \sqrt{1 - x^2} passes.

Finding domain and range

When a function is given by a formula with no domain stated, the domain is every real xx for which the formula makes sense. The usual restrictions:

Expression Restriction
1/g(x)1 / g(x) g(x)≠0g(x) \ne 0
g(x)\sqrt{g(x)} (even roots) g(x)≥0g(x) \ge 0
log⁡g(x)\log g(x) g(x)>0g(x) > 0
tan⁡x\tan x x≠π/2+kπx \ne \pi/2 + k\pi

Example. f(x)=x−2/(x−5)f(x) = \sqrt{x - 2} / (x - 5). We need x−2≥0x - 2 \ge 0 and x−5≠0x - 5 \ne 0, so the domain is [2,5)∪(5,∞)[2, 5) \cup (5, \infty).

The range takes more thought. For g(x)=x2+1g(x) = x^2 + 1 on all of R\mathbb{R}, squares are ≥0\ge 0, so g(x)≥1g(x) \ge 1, and every value ≥1\ge 1 is reached (take x=y−1x = \sqrt{y - 1}). The range is [1,∞)[1, \infty). SymPy can do both jobs:

python · try it yourself
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 (0,1/4](0, 1/4], open on the left. Indeed hh is largest (14\frac{1}{4}) at x=0x = 0 and approaches 0 but never reaches it.

Properties to look for

Property Meaning Example
Even f(−x)=f(x)f(-x) = f(x): graph symmetric about the y-axis x2x^2, cos⁡x\cos x
Odd f(−x)=−f(x)f(-x) = -f(x): symmetric about the origin x3x^3, sin⁡x\sin x
Increasing on II a<b in I⇒f(a)<f(b)a < b \text{ in } I \Rightarrow f(a) < f(b) exe^x everywhere
One-to-one different inputs give different outputs x3x^3 yes, x2x^2 no
Periodic f(x+p)=f(x)f(x + p) = f(x) for some p>0p > 0 sin⁡x\sin x, p=2πp = 2\pi
Zeros xx with f(x)=0f(x) = 0 (x-intercepts) x2−4x^2 - 4: ±2\pm 2

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 mx+bmx + b R\mathbb{R} R\mathbb{R} (m≠0m \ne 0) slope
Quadratic ax2+bx+cax^2 + bx + c R\mathbb{R} half-line from the vertex parabolas
Polynomial anxn+⋯+a0a_n x^n + \cdots + a_0 R\mathbb{R} depends Polynomial Functions
Rational p(x)/q(x)p(x)/q(x) q(x)≠0q(x) \ne 0 depends Rational Functions
Root x\sqrt{x} [0,∞)[0, \infty) [0,∞)[0, \infty)
Exponential axa^x R\mathbb{R} (0,∞)(0, \infty) Exponential and Logarithmic
Logarithmic log⁡ax\log_a x (0,∞)(0, \infty) R\mathbb{R} same
Trigonometric sin⁡x\sin x, cos⁡x\cos x R\mathbb{R} [−1,1][-1, 1] Graphs of the Trig Functions
Absolute value ∣x∣\lvert x \rvert R\mathbb{R} [0,∞)[0, \infty) Piecewise Functions
python · try it yourself
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) -> float declares f ⁣:R→Rf\colon \mathbb{R} \to \mathbb{R}. Runtime checks (if r < 0: raise ValueError) restrict the domain further.
  • Lookup tables. A Python dict is 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 (0,1)(0, 1) tells you a returned 1.3 is a bug.
python · try it yourself
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 xx? (a) y=3x2−1y = 3x^2 - 1, (b) x2+y2=4x^2 + y^2 = 4, (c) y=∣x−2∣y = \lvert x - 2 \rvert, (d) x=y2x = y^2.

Answer

(a) and (c) are functions; (b) and (d) are not.

Solution

(b) gives y=±4−x2y = \pm\sqrt{4 - x^2}, two values for ∣x∣<2\lvert x \rvert < 2: a circle fails the vertical line test. (d) gives y=±xy = \pm\sqrt{x} for x>0x > 0, also two values. (a) and (c) give exactly one yy for each xx.

Exercise 2: Domains

Find the domain of f(x)=1/(x2−9)f(x) = 1/(x^2 - 9) and of g(x)=6−2xg(x) = \sqrt{6 - 2x}.

Answer

ff: all reals except ±3\pm 3, (−∞,−3)∪(−3,3)∪(3,∞)(-\infty, -3) \cup (-3, 3) \cup (3, \infty). gg: (−∞,3](-\infty, 3].

Solution
f:x2−9≠0⇒x≠±3g:6−2x≥0⇒x≤3\begin{aligned} f&: x^2 - 9 \ne 0 \quad\Rightarrow\quad x \ne \pm 3 \\ g&: 6 - 2x \ge 0 \quad\Rightarrow\quad x \le 3 \end{aligned}
python · try it yourself
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 f(x)=5−(x+1)2f(x) = 5 - (x + 1)^2 and of g(x)=3+2sin⁡xg(x) = 3 + 2\sin x.

Answer

ff: (−∞,5](-\infty, 5]. gg: [1,5][1, 5].

Solution

(x+1)2≥0(x + 1)^2 \ge 0, so f(x)≤5f(x) \le 5, with 5 reached at x=−1x = -1; ff takes every smaller value. For gg, sin⁡x\sin x runs over [−1,1][-1, 1], so 2sin⁡x2\sin x runs over [−2,2][-2, 2] and gg over [1,5][1, 5].

Exercise 4: Even, odd or neither

Classify f(x)=x4−3x2f(x) = x^4 - 3x^2, g(x)=x3+xg(x) = x^3 + x and h(x)=x2+xh(x) = x^2 + x.

Answer

ff even, gg odd, hh neither.

Solution
f(−x)=x4−3x2=f(x)eveng(−x)=−x3−x=−g(x)oddh(−x)=x2−x, neither h(x) nor −h(x)neither\begin{aligned} f(-x) &= x^4 - 3x^2 = f(x) && \text{even} \\ g(-x) &= -x^3 - x = -g(x) && \text{odd} \\ h(-x) &= x^2 - x, \text{ neither } h(x) \text{ nor } -h(x) && \text{neither} \end{aligned}
python · try it yourself
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 f(x)=ln⁡(x−4)f(x) = \ln(x - 4)?

  1. x≥4x \ge 4
  2. x>4x > 4
  3. x≠4x \ne 4
  4. All real numbers

Which statement about f(x)=x2f(x) = x^2 on R\mathbb{R} is true?

  1. It is not a function because f(2)=f(−2)f(2) = f(-2)
  2. Its range is R\mathbb{R}
  3. It is a function, but not one-to-one
  4. It is odd

The vertical line test checks whether…

  1. a function is one-to-one
  2. a curve is the graph of a function of x
  3. a function is continuous
  4. a function is increasing