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.

Share this article

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:

f:AB.f:A\to B.

Read it as “ff maps from AA to BB.”

  • AA is the domain, the set of allowed inputs.
  • BB is the codomain, the set in which every output is promised to live.
  • The rule tells us which element of BB is assigned to each element of AA.

The notation xAx\in A means “xx is an element of AA.” It lets us write the contract and the evaluation together:

xAf(x)B.x\in A \quad\Longrightarrow\quad f(x)\in B.
A function promises a mapping from an allowed input set to an output set. It does not promise that every output will actually occur.
Domain, function, and codomain mapThree valid numeric feature inputs map through a risk-score function to outputs between zero and one. A text input sits outside the domain and is rejected before evaluation.Domain0.10.50.9riskScoref(x)Codomain [0, 1]0.180.500.82“high” is outside the domain
Text alternative for the mapping diagram
InputValid?Output
0.1Yes0.18
0.5Yes0.50
0.9Yes0.82
“high”No—wrong input typeNot 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:

q:RR,q(x)=x2.q:\mathbb{R}\to\mathbb{R}, \qquad q(x)=x^2.

Here R\mathbb{R} means the set of real numbers. Every real input can be squared, and every result is also real, so the declaration is valid.

But q(x)q(x) is never negative. The set of values the function actually reaches is

[0,)={yR:y0}.[0,\infty)=\{y\in\mathbb{R}:y\ge 0\}.

That reached set is called the image or range. It can be smaller than the declared codomain.

ConceptFor q:RRq:\mathbb{R}\to\mathbb{R}, q(x)=x2q(x)=x^2
DomainAll real numbers
CodomainAll real numbers
ImageThe nonnegative real numbers

Changing the domain can repair an undefined rule

The expression

r(x)=1xr(x)=\frac{1}{x}

does not define a real output at x=0x=0. We can state the valid function by excluding zero:

r:R{0}R,r(x)=1x.r:\mathbb{R}\setminus\{0\}\to\mathbb{R}, \qquad r(x)=\frac{1}{x}.

The symbol \setminus means set difference. The domain is “the real numbers without zero.”

Similarly,

(x)=logx\ell(x)=\log x

has a real-valued domain restricted to positive inputs:

:(0,)R.\ell:(0,\infty)\to\mathbb{R}.

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

f:R3R2.f:\mathbb{R}^3\to\mathbb{R}^2.

The notation R3\mathbb{R}^3 means an ordered list of three real coordinates, and R2\mathbb{R}^2 means an ordered list of two real coordinates. One valid input is

x=[0.21.03.4].x=\begin{bmatrix}0.2\\-1.0\\3.4\end{bmatrix}.

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

c:Rd[0,1]K.c:\mathbb{R}^{d}\to[0,1]^K.

It accepts dd real features and returns KK numbers, each between zero and one. That codomain alone does not guarantee that the KK numbers sum to one. If the intended output is a probability distribution over KK mutually exclusive classes, the codomain needs an additional condition:

ΔK1={p[0,1]K:k=1Kpk=1}.\Delta^{K-1} = \left\{ p\in[0,1]^K: \sum_{k=1}^{K}p_k=1 \right\}.

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:

  1. What objects belong to the domain?
  2. What objects are explicitly excluded or undefined?
  3. What output set does the codomain promise?
  4. 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 R\mathbb{R} and the codomain is R\mathbb{R}. Squaring any real input is defined and returns a real number.

Because x20x^2\ge 0 for every real xx, the reached values are exactly the nonnegative reals:

image(q)=[0,).\operatorname{image}(q)=[0,\infty).

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

r(0)=10.r(0)=\frac{1}{0}.

There is no real number yy satisfying 0y=10\cdot y=1, so the division has no real value. A correct declaration excludes zero:

r:R{0}R.r:\mathbb{R}\setminus\{0\}\to\mathbb{R}.

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 R3\mathbb{R}^3 requires exactly three ordered real coordinates. The vector [1,2][1,2] belongs to R2\mathbb{R}^2, not R3\mathbb{R}^3.

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 [0,1]2[0,1]^2, 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,

0.6+0.6=1.21,0.6+0.6=1.2\ne1,

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 p0.000001p\ge0.000001, both expressions agree. For smaller positive pp, the clipped implementation returns the constant log(0.000001)\log(0.000001) instead of logp\log p. For zero or negative pp, 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.

Sources

  1. Mathematics for Machine Learning companion website
  2. Mathematics for Machine Learning book PDF