Math for AI course · Lesson 26 of 180
Groups, Rings, and Fields: Why AI Operations Need Rules
See how closure, inverses, and distributivity decide which tensor operations are legal, then test the ideas with NumPy.
An AI model can add two vectors, scale a weight matrix, and multiply a batch through a layer because the numbers in those tensors obey a useful collection of rules. Those rules are easy to take for granted. They are also the reason a linear derivation can be rearranged without changing its meaning.
Learning outcome: Distinguish closure, inverse, and distributive assumptions at an intuitive level, and use them to explain why common tensor operations are modeled over the real-number field.
The prerequisite is Lesson 10: Reading a derivation line by line, where the first broken algebraic step became a debugging clue. Here, the question is slightly earlier: which algebraic steps are legal in the first place? The official Mathematics for Machine Learning companion maps this lesson to Section 2.4 of the book PDF. The definitions, examples, code, visual, and AI interpretation below are original.
Before an operation, ask where its result lives
An operation is more than a symbol such as or . It also makes a promise about its inputs and output. If is a set and is an operation, closure means
The arrow says that two members of go in and another member of comes out. If the result can leave , then an expression that started in is no longer an expression entirely inside that system.
For example, the nonnegative integers
are closed under addition: is still nonnegative. But they are not closed under subtraction: leaves the set. Closure is always relative to a particular operation; the same set may be closed under one operation and not another.
Operation-validity map
Each structure adds a guarantee
Read from left to right: a field keeps every guarantee shown for a ring and adds one more rule about division by nonzero scalars.
| Structure | Operations | Guarantees | Practical consequence |
|---|---|---|---|
| Group | One operation, ⊙ | Closure, associativity, identity, inverse | Combine and undo elements under ⊙. |
| Ring | Addition and multiplication | Addition is a group; multiplication distributes over addition | Expand and regroup arithmetic, but division is not automatic. |
| Field | Addition and multiplication | A ring where every nonzero element has a multiplicative inverse | Nonzero scalars can be divided out in linear combinations. |
The map is a compact promise about what each name buys us. It does not say that every set with a familiar symbol is automatically a group, ring, or field. We still have to check the relevant rules.
Groups keep one operation navigable
The integers with addition, , form a group. The identity is , and the inverse of is because . The nonnegative integers with addition do not form a group: they have an identity, but the inverse of would be , which is not in .
The real numbers with multiplication, , also fail to be a group because has no multiplicative inverse. Remove zero and the result is an Abelian group: multiplication is associative and commutative, is the identity, and every nonzero real number has a reciprocal.
The inverse therefore answers a concrete question: can this operation be undone while staying inside the same set? That is useful in transformations, but it is only one operation at a time. A group under addition does not automatically license division.
Rings add a second operation
A ring keeps addition and multiplication together. In the convention used here, a ring has a multiplicative identity and follows these rules:
- its elements form an Abelian group under addition;
- multiplication is closed and associative;
- multiplication distributes over addition:
The integers are a ring under ordinary addition and multiplication. For example, multiplication distributes over addition because
and
But is not a field. The number has no multiplicative inverse inside the integers: the equation would require , which is not an integer. A ring lets us add, multiply, and distribute; it does not promise that every nonzero element can be divided out.
Fields make scalar algebra complete
A field is a commutative ring in which every nonzero element has a multiplicative inverse. The rational numbers and real numbers are fields. The integers are not.
The word “field” is about the scalars, not the shape of a tensor. A vector in has real coordinates; a matrix in has real coordinates. Their entries come from a field even though the vectors and matrices themselves are different kinds of objects.
This distinction matters when a derivation moves a scalar across a sum or divides by a nonzero coefficient. In a field, those moves have the required algebraic support. In a ring, some are valid and others may not be.
Worked example: verify the rules with small numbers
Use two tiny tests. First, addition modulo has elements . The result of is represented by after wrapping around. The identity is , and the additive inverses are
where the superscript means inverse under modular addition. Second, check a real-vector identity that a linear layer relies on. Let
Then
while
The two paths agree because scalar multiplication distributes over vector addition. The identity is not a coincidence tied to these particular values; it is one of the rules that makes a vector space over the field .
Why tensor operations assume a field
Most neural-network calculations use vectors, matrices, and higher-dimensional tensors whose entries are floating-point approximations to real numbers. A dense layer, for example, has the form
The matrix-vector product expands into additions and multiplications of real scalars. The bias addition then uses the same additive structure. Because the scalars are modeled as elements of , the usual regrouping and distribution rules explain why equivalent implementations produce the same mathematical result.
That is also why shape checks and algebraic checks are different questions:
- Shape compatibility: the inner dimension of and agrees, so each dot product has the right number of terms.
- Scalar validity: the entries use an arithmetic system in which addition, multiplication, and scalar combinations have the expected rules.
PyTorch makes both layers of the contract visible. A tensor carries a dtype,
device, and layout, and torch.matmul changes behavior according to whether
its inputs are vectors, matrices, or batched matrices; see the Tensor
Attributes documentation
and the torch.matmul reference.
The library’s shape rules do not replace the field model. They implement a
particular tensor operation whose scalar arithmetic is usually intended to
approximate real arithmetic.
This is the algebraic reason a training system can form linear combinations of features, weights, and updates. Calculus adds another layer: an operation must also be differentiable, or have a defined subgradient or backward rule, for automatic differentiation to use it. A field makes the forward arithmetic coherent; it does not guarantee that every function is differentiable.
Boundary case: machine floats only imitate the real field
The real numbers form a field in mathematics. A finite computer dtype is a bounded, rounded set of bit patterns, so it cannot satisfy every real-number rule exactly. Three practical effects follow:
- Rounding: values such as
0.1usually have no exact binary representation, so two mathematically equal paths may differ in their last bits. - Overflow: multiplying two large
float32values can produceinf, a value outside the ordinary real-number model used in the derivation. - Invalid exceptional arithmetic: expressions such as
inf - infproducenan, so even familiar rearrangements need numerical checks.
These are not counterexamples to the theorem that is a field.
They are warnings that float32 is an approximation to , with a
finite range and implementation-defined rounding behavior. In practice, use
appropriate tolerances, monitor non-finite values, and treat the field rules as
the mathematical contract that the implementation approximates.
There is a second common source of confusion in array code. In NumPy, x * y
means elementwise multiplication, while A @ x means matrix multiplication.
Both can be valid operations, but they have different mathematical meanings
and shape requirements. A successful broadcast is not proof that the operation
matches the model’s intended algebra.
A small algebra audit for model code
When a derivation or tensor expression looks suspicious, audit it in this order:
- Name the set. Are the values real numbers, integers, modular residues, probabilities, or something else?
- Name each operation. Distinguish addition, scalar multiplication, elementwise multiplication, and matrix multiplication.
- Check closure. Does each result remain in the intended set and shape?
- Check the required guarantee. Is an inverse, distributive step, or commutative swap being used? Identify the rule that licenses it.
- Check machine behavior. Look for rounding, overflow,
nan,inf, and tolerance-sensitive comparisons.
This audit turns “the formula should work” into a testable claim. The next lesson uses the same idea to decide whether a set of representations is a vector space. There, closure under vector addition and scalar multiplication will become the central test.
Check your understanding
Question 1
Why is closure always stated relative to a particular operation?
Show the step-by-step solution
Closure asks whether the result of one named operation stays in the set. For example, is closed under addition because remains in the set, but it is not closed under subtraction because leaves it. The set did not change; the operation did.
Question 2
Does (N₀, +) form a group? Identify the first group rule that fails.
Show the step-by-step solution
The set has closure under addition, associativity, and identity . The problem is the inverse rule: the inverse of under addition is , but . Therefore the structure is not a group, even though several other group rules hold.
Question 3
Why is Z a ring but not a field under ordinary addition and multiplication?
Show the step-by-step solution
First, is an additive group: every integer has an integer opposite, and addition is associative with identity . Integer multiplication is closed and associative, has identity , and distributes over addition, so the ring rules hold. A field would additionally require every nonzero integer to have an integer reciprocal. Since , the field requirement fails.
Question 4
In arithmetic modulo 5, what is the multiplicative inverse of 4, and how can you verify it?
Show the step-by-step solution
Test the nonzero residues until a product is congruent to . Since and , the inverse of modulo is . The verification is the product followed by reducing the result by the modulus.
Question 5
For x = [2, -1]ᵀ, y = [1, 3]ᵀ, and λ = 1.5, compute λ(x + y) and λx + λy.
Show the step-by-step solution
Add first: . Scaling gives . Separately, and . Adding those vectors also gives , which verifies distributivity for this example.
Question 6
A NumPy program uses A * x where the derivation requires A @ x. What should you inspect before accepting the result?
Show the step-by-step solution
Inspect the intended operation and the shapes. In NumPy, * multiplies
entries element by element, possibly using broadcasting, while @ performs a
matrix product and requires compatible inner dimensions. A broadcasted result
can therefore be numerically successful but semantically wrong. Compare the
code with the model equation and check that the output shape matches the
derived vector or matrix.
Question 7
Why can a real-valued tensor identity disagree in the last bits even when the algebra is valid over R?
Show the step-by-step solution
The derivation treats entries as exact real numbers, but a machine floating
point dtype stores a bounded set of rounded approximations. Decimal values such
as 0.1 may already be inexact, and different evaluation orders can round at
different steps. The identity remains mathematically valid; the implementation
should compare results with a scale-aware tolerance, monitor nan and inf,
and avoid interpreting bit-level disagreement as an algebraic failure.