Kone: Algebraic
This module provides contexts of basic algebraic structures, their basic implementations, and basic utilities.
The main topic of the module is about algebraic "contexts". The term "context" is described in "Contexts" module. Also, it depends on contexts from "Relations" module.
Applying the dependencies
- Gradle Kotlin DSL
- Gradle Groovy DSL
- Maven
dependencies {
implementation("dev.lounres:kone.algebraic:0.0.0-experiment")
}
dependencies {
implementation 'dev.lounres:kone.algebraic:0.0.0-experiment'
}
<dependency>
<groupId>dev.lounres</groupId>
<artifactId>kone.algebraic</artifactId>
<version>0.0.0-experiment</version>
</dependency>
Concept of algebraic contexts (a.k.a. algebraic structures)
In the same way any context exists to bring operations we could use further, there are algebraic contexts that describe how to manipulate with numbers. In mathematics, they are called algebraic structures.
The problem is that there are a lot of different kinds of "numbers". At first, we could be working with integers, then we could switch to rational numbers, then we could use matrices or polynomials and so on. There is a whole zoo of such constructions and there is no end to them. But if we want to use them interchangeably, we have to provide interface (abstraction) that characterises them. That's how terms like ring, field, semiring, Euclidean ring and so on appeared. And the structures are represented in Kone library as corresponding interfaces.
Comment on the interfaces' declarations
Let's consider ring definition. Despite it consisting of only three operators (binary "plus", unary "minus", and binary "times", all of which operate on the underlying set of numbers) there are other frequent operations:
- addition, subtraction, and multiplication of a number (of the ring) and integer,
- positive integer power of a number (of the ring),
- checks in equality to zero and one.
These operations could be implemented outside the interface via extension functions.
For example, multiplication of a number and integer can be achieved via exponentiation by squaring algorithm.
But this algorithm takes unnecessary time when there are a lot of cases when this operation could be replaced with native one.
For example, in ring of integers that uses Int
as an integer representative,
one could delegate multiplication of two integers to built-in operation instead of exponentiation by squaring algorithm.
That's why these interfaces actually consist of a lot of similar operations.
But just in case, most of these operations have default implementations via, yet again, exponentiation by squaring algorithm.
So if you need to implement one of these contexts, don't be scared! There are actually not so many operations to implement!
Default contexts
This module also already provides default contexts for basic interpretations of basic number types:
IntContext
,UIntContext
,DoubleContext
, and so on for primitive types.BigIntegerContext
forjava.math.BigInteger
.
Several more contexts are placed in "Algebraic Extra" module.
There are plans for implementing "safe" contexts for primitive types. They are going to throw exceptions on overflows, appearences of NaNs and other unfavourable situations.
Examples
Examples are available here.