Post

# Learning Concepts in C++ Part 4: Fields

last edited on 2023-09-28

## On the path to $$\text{SE}(3)$$

As part of a project to learn how to use Concepts and Templates in C++, I set out to implement a template library for $$\text{SE}(3)$$. In my previous post, I introduced the Group concept and implemented template class RealAddition<T> that satisfied the concept AbelianGroup. While the group $$\text{SE}(3)$$ as a whole is not Abelian, it turns out that we still need the Abelian group concept to implement the Lie Algebra $$\text{se}(3)$$ of $$\text{SE}(3)$$. This is because the Lie Algebra of a Lie Group carries a vector space structure, which in turn is defined over a field, and fields have Abelian group structure under two operations (addition and multiplication).

### Axioms that Define a Field

Fields in mathematics are structures which are a generalization of certain number sets and the commonly used algebraic operations on those sets. For instance, the Rational Numbers $$\mathbb{Q}$$ and the Real Numbers $$\mathbb{R}$$ are a field under ordinary addition and multiplication. Other examples of fields exist, such as the field of rational polynomial functions. A triple $$(X, +, \cdot)$$ is a field if it satsifies:

• $$(X,+)$$ is an Abelian group
• call the identity element of this group $$0$$
• $$(X\backslash 0,\cdot)$$ is an Abelian group ($$0$$ is excluded because it has no inverse)
• call the identity element of this group $$1$$
• $\forall x,y,z \in X: x \cdot (y + z) = x \cdot y + x \cdot z$

## Field Concept Implementation

Fortunately, much of the heavy lifting in making a Field concept has already been done for us when I wrote the AbelianGroup concept in the last post. However, as we have seen before, one of the axioms we must satisfy quantifies over the set $$X$$, and so once again we have a property which cannot be checked by the compiler. As before, I will add a requirement that the multiplication operation declares it holds the distributive property with respect to a specific addition operation.

From the concept definition we have that class F must have an identifier Set which forms an Abelian group with respect to the identifiers Add and Mul. Finally, we require that Mul has a static method that acts on instances of Add. This is the trick we play to have Mul profess its distributivity to the concept Field.

Bringing this together, I provide a template class for the field of real numbers. Since we already have a RealNumbers template, then all we need to define the operations $$(+,\cdot)$$, and bundle them into a template class. For addition I will use the RealAddition template from the previous post. Multiplication is implemented similarly, but includes

Our final implementation is the template class RealNumberField, which we test by invoking the template function is_field

One of the nice things I encountered up to this point is that by thoughtfully employing templates and concepts it is possible to create expressive and parsimonious abstractions.