Math for AI course · Lesson 3 of 180
Domain and Codomain: The Input Contract Every AI Function Needs
Learn how domains and codomains specify valid model inputs and promised output types, then diagnose shape, range, and undefined-operation failures.
Lesson 2 described a function as a rule assigning one output to every allowed input. The word allowed carries more weight than it first appears to.
A text classifier cannot safely receive an arbitrary block of bytes when it expects token IDs. A matrix multiplication cannot accept incompatible shapes. A logarithm cannot produce a real value for a negative input. The rule is only part of a function’s contract; the valid input set and promised output set are the other parts.
By the end of this lesson, you will be able to write a function with its domain and codomain, distinguish a codomain from the values actually reached, and explain common AI input failures as violations of a mathematical contract.
The arrow carries type information
A compact function declaration looks like this:
Read it as “ maps from to .”
- is the domain, the set of allowed inputs.
- is the codomain, the set in which every output is promised to live.
- The rule tells us which element of is assigned to each element of .
The notation means “ is an element of .” It lets us write the contract and the evaluation together:
| Input | Valid? | Output |
|---|---|---|
| 0.1 | Yes | 0.18 |
| 0.5 | Yes | 0.50 |
| 0.9 | Yes | 0.82 |
| “high” | No—wrong input type | Not evaluated |
The diagram’s text input “high” is not a failed numerical prediction. It is
not an allowed input to this numeric function, so evaluation should not begin.
A codomain is a promise, not a list of observed outputs
Consider the squaring function on real numbers:
Here means the set of real numbers. Every real input can be squared, and every result is also real, so the declaration is valid.
But is never negative. The set of values the function actually reaches is
That reached set is called the image or range. It can be smaller than the declared codomain.
| Concept | For , |
|---|---|
| Domain | All real numbers |
| Codomain | All real numbers |
| Image | The nonnegative real numbers |
Changing the domain can repair an undefined rule
The expression
does not define a real output at . We can state the valid function by excluding zero:
The symbol means set difference. The domain is “the real numbers without zero.”
Similarly,
has a real-valued domain restricted to positive inputs:
These are not fussy decorations. They tell an implementation what must be checked before evaluation.
AI domains include shape and structure
So far, domains have looked like intervals of numbers. Model inputs are often vectors or matrices. Their shapes belong to the contract.
Let
The notation means an ordered list of three real coordinates, and means an ordered list of two real coordinates. One valid input is
A two-coordinate vector is outside the declared domain. Its entries may all be real, but the object has the wrong shape.
This is the mathematical version of a familiar tensor error:
def project_three_features(x):
if x.shape != (3,):
raise ValueError("expected exactly three features")
return [x[0] + x[1], x[2] - x[1]]
The check protects the domain. The function returns two numbers, matching its codomain shape.
For token-based models, a complete domain can be stricter still. Inputs may need:
- integer token IDs rather than arbitrary real values;
- IDs within a fixed vocabulary range;
- a maximum sequence length;
- a batch and attention-mask shape that agree; and
- an image, audio, or text encoding expected by this particular model.
Saying “the model accepts a tensor” hides these constraints. Good mathematical and software interfaces make them visible.
Output promises constrain downstream code
Suppose a classifier is declared as
It accepts real features and returns numbers, each between zero and one. That codomain alone does not guarantee that the numbers sum to one. If the intended output is a probability distribution over mutually exclusive classes, the codomain needs an additional condition:
This set is called a probability simplex. We will study it later. For now, notice how the richer codomain communicates a stronger promise to downstream code.
A checklist for reading model functions
When you encounter a new mathematical mapping, ask four questions:
- What objects belong to the domain?
- What objects are explicitly excluded or undefined?
- What output set does the codomain promise?
- Does the rule actually reach the entire codomain, or only a smaller image?
These questions turn many vague “shape mismatch,” “invalid probability,” and “undefined loss” errors into precise contract violations. The next lesson will use sets to describe collections of data and events more systematically.
Check your understanding
Question 1
For q(x) = x² declared as q: R → R, identify the domain, codomain, and image.
Show the step-by-step solution
The declaration says the domain is and the codomain is . Squaring any real input is defined and returns a real number.
Because for every real , the reached values are exactly the nonnegative reals:
The image is therefore a proper subset of the codomain.
Question 2
Why is 0 outside the real-valued domain of r(x) = 1/x?
Show the step-by-step solution
Substituting zero asks for
There is no real number satisfying , so the division has no real value. A correct declaration excludes zero:
Question 3
A function expects an element of R³. Is [1, 2] valid if both entries are real? Explain.
Show the step-by-step solution
No. Membership in requires exactly three ordered real coordinates. The vector belongs to , not .
The individual value type is correct, but the shape is not. Both value type and shape are part of the domain contract.
Question 4
A classifier returns [0.6, 0.6]. The entries are each in [0, 1]. Why might this still violate its codomain?
Show the step-by-step solution
If the classifier promises only , the vector satisfies that weak contract. But if it promises a probability distribution over two mutually exclusive classes, the values must also sum to one.
Here,
so the vector is not in the probability simplex. Whether it violates the codomain depends on which output set was actually declared.
Question 5
An implementation replaces log(p) with log(max(p, 0.000001)). Is it computing exactly the same function for every real p?
Show the step-by-step solution
No. For inputs , both expressions agree. For smaller positive , the clipped implementation returns the constant instead of . For zero or negative , it also produces a finite value where the original real logarithm is undefined.
Clipping can be a deliberate numerical safeguard, but it changes the function outside the agreement region. A precise explanation should state that convention and its reason.