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.

Share this article

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 ×\times. It also makes a promise about its inputs and output. If SS is a set and ⊙\mathbin{\odot} is an operation, closure means

⊙:S×S→S.\mathbin{\odot}: S\times S\to S.

The arrow says that two members of SS go in and another member of SS comes out. If the result can leave SS, then an expression that started in SS is no longer an expression entirely inside that system.

For example, the nonnegative integers

N0={0,1,2,3,…}\mathbb{N}_0=\{0,1,2,3,\ldots\}

are closed under addition: 4+7=114+7=11 is still nonnegative. But they are not closed under subtraction: 4−7=−34-7=-3 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.

Groups, rings, and fields add operation guarantees Three connected cards show that a group has one operation with closure, associativity, an identity, and inverses. A ring adds a second operation and distributivity. A field adds multiplicative inverses for every nonzero element.more algebraic guaranteesGroupone operation ⊙✓ closure: x ⊙ y stays in G✓ associativity✓ identity element✓ inverse for every xYou can combine and undo.Ringaddition + multiplication✓ addition forms a group✓ multiplication is closed✓ multiplication associates✓ distributive lawsDivision may still fail.Fielda ring with division✓ all ring guarantees✓ 1 is the multiplicative identity✓ every nonzero x has x⁻¹Real scalars live here.
Algebraic structures are contracts: each extra guarantee makes more transformations and rearrangements safe to use.
Semantic equivalent of the operation-validity map
StructureOperationsGuaranteesPractical consequence
GroupOne operation, ⊙Closure, associativity, identity, inverseCombine and undo elements under ⊙.
RingAddition and multiplicationAddition is a group; multiplication distributes over additionExpand and regroup arithmetic, but division is not automatic.
FieldAddition and multiplicationA ring where every nonzero element has a multiplicative inverseNonzero 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, (Z,+)(\mathbb{Z},+), form a group. The identity is 00, and the inverse of 77 is −7-7 because 7+(−7)=07+(-7)=0. The nonnegative integers with addition do not form a group: they have an identity, but the inverse of 77 would be −7-7, which is not in N0\mathbb{N}_0.

The real numbers with multiplication, (R,×)(\mathbb{R},\times), also fail to be a group because 00 has no multiplicative inverse. Remove zero and the result (R∖{0},×)(\mathbb{R}\setminus\{0\},\times) is an Abelian group: multiplication is associative and commutative, 11 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 11 and follows these rules:

  • its elements form an Abelian group under addition;
  • multiplication is closed and associative;
  • multiplication distributes over addition:
a(b+c)=ab+ac,(a+b)c=ac+bc.a(b+c)=ab+ac, \qquad (a+b)c=ac+bc.

The integers Z\mathbb{Z} are a ring under ordinary addition and multiplication. For example, multiplication distributes over addition because

3(4+(−2))=3⋅2=63(4+(-2)) = 3\cdot 2 = 6

and

3⋅4+3⋅(−2)=12−6=6.3\cdot4 + 3\cdot(-2)=12-6=6.

But Z\mathbb{Z} is not a field. The number 22 has no multiplicative inverse inside the integers: the equation 2q=12q=1 would require q=1/2q=1/2, 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 Q\mathbb{Q} and real numbers R\mathbb{R} are fields. The integers are not.

The word “field” is about the scalars, not the shape of a tensor. A vector in Rn\mathbb{R}^n has nn real coordinates; a matrix in Rm×n\mathbb{R}^{m\times n} has mnmn 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 44 has elements {0,1,2,3}\{0,1,2,3\}. The result of 3+2=53+2=5 is represented by 11 after wrapping around. The identity is 00, and the additive inverses are

0−1=0,1−1=3,2−1=2,3−1=1,0^{-1}=0,\qquad 1^{-1}=3,\qquad 2^{-1}=2,\qquad 3^{-1}=1,

where the superscript means inverse under modular addition. Second, check a real-vector identity that a linear layer relies on. Let

x=[2−1],y=[13],λ=1.5.x=\begin{bmatrix}2\\-1\end{bmatrix}, \qquad y=\begin{bmatrix}1\\3\end{bmatrix}, \qquad \lambda=1.5.

Then

λ(x+y)=1.5[32]=[4.53],\lambda(x+y) =1.5\begin{bmatrix}3\\2\end{bmatrix} =\begin{bmatrix}4.5\\3\end{bmatrix},

while

λx+λy=[3−1.5]+[1.54.5]=[4.53].\lambda x+\lambda y =\begin{bmatrix}3\\-1.5\end{bmatrix} +\begin{bmatrix}1.5\\4.5\end{bmatrix} =\begin{bmatrix}4.5\\3\end{bmatrix}.

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 Rn\mathbb{R}^n a vector space over the field R\mathbb{R}.

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

y=Wx+b,W∈Rm×n,x∈Rn,b,y∈Rm.y=Wx+b, \qquad W\in\mathbb{R}^{m\times n},\quad x\in\mathbb{R}^{n},\quad b,y\in\mathbb{R}^{m}.

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 R\mathbb{R}, 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 WW and xx 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:

  1. Rounding: values such as 0.1 usually have no exact binary representation, so two mathematically equal paths may differ in their last bits.
  2. Overflow: multiplying two large float32 values can produce inf, a value outside the ordinary real-number model used in the derivation.
  3. Invalid exceptional arithmetic: expressions such as inf - inf produce nan, so even familiar rearrangements need numerical checks.

These are not counterexamples to the theorem that R\mathbb{R} is a field. They are warnings that float32 is an approximation to R\mathbb{R}, 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:

  1. Name the set. Are the values real numbers, integers, modular residues, probabilities, or something else?
  2. Name each operation. Distinguish addition, scalar multiplication, elementwise multiplication, and matrix multiplication.
  3. Check closure. Does each result remain in the intended set and shape?
  4. Check the required guarantee. Is an inverse, distributive step, or commutative swap being used? Identify the rule that licenses it.
  5. 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, N0\mathbb{N}_0 is closed under addition because 4+7=114+7=11 remains in the set, but it is not closed under subtraction because 4−7=−34-7=-3 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 00. The problem is the inverse rule: the inverse of 77 under addition is −7-7, but −7∉N0-7\notin\mathbb{N}_0. 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, Z\mathbb{Z} is an additive group: every integer has an integer opposite, and addition is associative with identity 00. Integer multiplication is closed and associative, has identity 11, and distributes over addition, so the ring rules hold. A field would additionally require every nonzero integer to have an integer reciprocal. Since 2−1=1/2∉Z2^{-1}=1/2\notin\mathbb{Z}, 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 11. Since 4⋅4=164\cdot4=16 and 16≡1(mod5)16\equiv1\pmod 5, the inverse of 44 modulo 55 is 44. The verification is the product 4⋅44\cdot4 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: x+y=[3,2]Tx+y=[3,2]^{\mathsf T}. Scaling gives λ(x+y)=1.5[3,2]T=[4.5,3]T\lambda(x+y)=1.5[3,2]^{\mathsf T}=[4.5,3]^{\mathsf T}. Separately, λx=[3,−1.5]T\lambda x=[3,-1.5]^{\mathsf T} and λy=[1.5,4.5]T\lambda y=[1.5,4.5]^{\mathsf T}. Adding those vectors also gives [4.5,3]T[4.5,3]^{\mathsf T}, 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.

Sources

  1. Mathematics for Machine Learning companion website
  2. Mathematics for Machine Learning book PDF
  3. NumPy documentation: numpy.allclose
  4. PyTorch documentation: Tensor Attributes
  5. PyTorch documentation: torch.matmul