Class inheritance and generics/templates makes it possible to write code with nice abstractions very similar to the abstractions used in math, particularly in abstract algebra, where a computation can be described for an entire class of algebraic structures sharing some similar properties (eg. groups and rings). This has been described nicely in the book “From Mathematics to Generic Programming” by Alexander A. Stepanov and Daniel E. Rose.

Inspired by this book, I have implemented a library for computations on abstract algebraic structures such as groups, rings and fields. The library is called *Ruffini* (named after the italian mathematician Paolo Ruffini) and is developed in Java using generics to achieve the same kind of abstraction as in abstract algebra, eg. that you do not specify what specific algebraic structure and elements are used, but only what abstract structure it has, eg. that it is a group or a ring.

Abstract algebraic structures are defined in Ruffini by a number of interfaces extending each other, each describing operations on elements of some set represented by a generic class `E`

. The simplest such interface is a semigroup:

```
public interface Semigroup<E> {
/**
* Return the result of the product <i>ab</i> in this
* semigroup.
*
* @param a An element <i>a</i> in this algebraic structure.
* @param b Another element <i>b</i> in this algebraic
* structure.
* @return The product of the two elements, <i>ab</i>.
*/
public E multiply(E a, E b);
}
```

The semigroup is extended by a monoid interface which adds a `getIdentity()`

method and by a group interface which adds an `invert(E)`

method. Continuing like this, we end up with interfaces for more complicated structures like rings, fields and vector spaces.

Now, each of these interfaces describes functions that can be applied to a generic type `E`

(eg. that given two instances of `E`

, the multiply method above returns a new instance of type `E`

). As in abstract algebra, these functions can be used to describe complicated computations *without* specifying exactly what algebraic structure is being used.

Ruffini currently implements a number of algorithms that is defined and described for the abstract structures, including the discrete Fourier transform, the Euclidean algorithm, computing Gröbner bases, the Gram-Schmidt process and Gaussian elimination. The abstraction makes it possible to write the algorithms only once in a clear, while still being usable on any of the algebraic structures defined in the library, such as integers, rational numbers, integers modulo *n*, finite fields, polynomial rings and matrix rings. Below is the code for the Gram-Schmidt process:

```
public List<V> apply(List<V> vectors) {
List<V> U = new ArrayList<>();
Projection<V, S> projection =
new Projection<>(vectorSpace, innerProduct);
for (V v : vectors) {
for (V u : U) {
V p = projection.apply(v, u);
v = vectorSpace.subtract(v, p);
}
U.add(v);
}
return U;
}
```

Here `V`

and `S`

are the generic types for resp. the vectors and scalars of the underlying vector space, and `Projection`

is a class defining projection given a specific vector space and an inner product.

The library is can be downloaded from GitHub: https://github.com/jonas-lj/Ruffini.