Inputs, outputs and domains
Evaluate functions, compare growth rates and read a loss curve without confusing inputs and outputs.
THE IDEA
A function is a rule with a promise
A function takes an input, applies a fixed rule, and gives back exactly one output. Feed it the same input twice and you get the same output twice. Think of a machine with a slot on the left and a tray on the right. f(x) = x² is the machine that squares whatever you drop in: input 3, output 9.
The set of inputs the machine accepts is its domain, and the set of outputs it can produce is its range. Two things are easy to mix up. Different inputs may give the same output (both 7 and −7 give 49 when you square them), but one input never gives two outputs. That asymmetry is what makes something a function.
When the machine refuses
Some rules simply break for certain inputs. 1/x has no answer at x = 0, because no number times 0 equals 1. sqrt(x) has no real answer for negative numbers, because a real number multiplied by itself is never negative. log2(x) asks "which power of 2 gives x?" and no power of 2 gives 0 or a negative number. Those inputs are outside the domain. On a graph they show up as a hole or a missing stretch of the curve.
Allowed. Domain of this machine: every x except 0.
Predict first. Which input is outside the domain of f(x) = 1/(x − 3)?
Predict first. The function g(x) = x² gives g(4) = 16 and g(−4) = 16. Is that allowed?
The common mistake: ignoring the domain in code
A formula on paper just says "undefined". Code reacts, and different tools react differently. This is what Python does:
>>> 1 / 0
ZeroDivisionError: division by zero # message text can vary between versions
>>> import math
>>> math.sqrt(-4)
ValueError: math domain error
>>> math.log2(0)
ValueError: math domain errorChapter context
A function maps an input to an output under a stated rule. Graphs let us see the same rule at many inputs; formulas let us calculate a particular output exactly. Both views should describe the same quantity and units.
TRY IT YOURSELF
Part 1. Do it on the plot
Each task ticks from the real machine state and stays ticked once earned. Use the chips to change machines and the slider or box to change the input.
Allowed. Domain of this machine: every real x.
- To do: Make the 1/x machine output 0.2
- To do: Make the square-root machine output 3
- To do: Make the log2 machine output -2
- To do: Feed 1/x an input it cannot accept
- To do: Feed the square-root machine an input it cannot accept
- To do: Make the squaring machine output 49 using a negative input
Part 2. Do it in code
- Write safe_inverse(x) that returns 1 / x, but returns None when x is 0 instead of crashing.
- Write tabulate(f, xs) that returns a list of (x, y) pairs. If f raises ZeroDivisionError or ValueError for an input, store None as that y.
- Use it: Python itself marks a domain break with an error. math.sqrt(-1) raises ValueError, and so does math.log(0).
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
Is f(x)=1/x defined at zero?
Chapter reference: math, worked example & code
This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.
Math and notation
For the illustrative loss:
Theta is a tunable scalar, 1.2 is the target, and L is a nonnegative output. An exponent and its logarithm undo each other only on their valid domains.
Worked example
Evaluate L(0)=1.44, L(2)=0.64 and L(1.2)=0. The parameter 2 gives a larger input than 0 but a smaller loss. Squaring makes positive and negative distances contribute the same penalty. This is a synthetic landscape, not a claim that real models have one simple valley.
Reference code
The following example uses the Python standard library and does not call a remote service.
from math import isclose, log2
loss = lambda theta: (theta - 1.2) ** 2
assert isclose(loss(0), 1.44)
assert isclose(loss(2), 0.64)
assert loss(1.2) == 0
assert log2(8) == 3
assert sum(i*i for i in range(1, 4)) == 14
Python examples are provided for study and local execution; this page does not execute Python.
Common mistakes
The output of a loss function is not its input parameter. Larger inputs do not always produce larger outputs. Logarithms require an explicit base when comparing information quantities.