2020-08-08 11:51:04 +03:00
|
|
|
# Algebraic Structures and Algebraic Elements
|
2019-01-05 20:15:36 +03:00
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
The mathematical operations in KMath are generally separated from mathematical objects. This means that to perform an
|
|
|
|
operation, say `+`, one needs two objects of a type `T` and an algebra context, which draws appropriate operation up,
|
|
|
|
say `Space<T>`. Next one needs to run the actual operation in the context:
|
2019-01-05 20:15:36 +03:00
|
|
|
|
|
|
|
```kotlin
|
2021-02-18 11:17:28 +03:00
|
|
|
import space.kscience.kmath.operations.*
|
2019-01-05 20:15:36 +03:00
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
val a: T = ...
|
|
|
|
val b: T = ...
|
|
|
|
val space: Space<T> = ...
|
|
|
|
|
|
|
|
val c = space { a + b }
|
2019-01-05 20:15:36 +03:00
|
|
|
```
|
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
At first glance, this distinction seems to be a needless complication, but in fact one needs to remember that in
|
|
|
|
mathematics, one could draw up different operations on same objects. For example, one could use different types of
|
|
|
|
geometry for vectors.
|
2019-01-05 20:15:36 +03:00
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
## Algebraic Structures
|
2019-01-05 20:15:36 +03:00
|
|
|
|
|
|
|
Mathematical contexts have the following hierarchy:
|
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
**Algebra** ← **Space** ← **Ring** ← **Field**
|
|
|
|
|
|
|
|
These interfaces follow real algebraic structures:
|
2019-01-05 20:15:36 +03:00
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
- [Space](https://mathworld.wolfram.com/VectorSpace.html) defines addition, its neutral element (i.e. 0) and scalar
|
|
|
|
multiplication;
|
|
|
|
- [Ring](http://mathworld.wolfram.com/Ring.html) adds multiplication and its neutral element (i.e. 1);
|
|
|
|
- [Field](http://mathworld.wolfram.com/Field.html) adds division operation.
|
2019-01-05 20:15:36 +03:00
|
|
|
|
2021-03-16 21:17:26 +03:00
|
|
|
A typical implementation of `Field<T>` is the `DoubleField` which works on doubles, and `VectorSpace` for `Space<T>`.
|
2019-01-05 20:15:36 +03:00
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
In some cases algebra context can hold additional operations like `exp` or `sin`, and then it inherits appropriate
|
|
|
|
interface. Also, contexts may have operations, which produce elements outside of the context. For example, `Matrix.dot`
|
|
|
|
operation produces a matrix with new dimensions, which can be incompatible with initial matrix in terms of linear
|
|
|
|
operations.
|
2019-01-05 20:15:36 +03:00
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
## Algebraic Element
|
2019-01-05 20:15:36 +03:00
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
To achieve more familiar behavior (where you apply operations directly to mathematical objects), without involving
|
|
|
|
contexts KMath submits special type objects called `MathElement`. A `MathElement` is basically some object coupled to
|
2019-01-05 20:15:36 +03:00
|
|
|
a mathematical context. For example `Complex` is the pair of real numbers representing real and imaginary parts,
|
2020-08-08 11:51:04 +03:00
|
|
|
but it also holds reference to the `ComplexField` singleton, which allows performing direct operations on `Complex`
|
2019-01-05 20:15:36 +03:00
|
|
|
numbers without explicit involving the context like:
|
|
|
|
|
|
|
|
```kotlin
|
2021-02-18 11:17:28 +03:00
|
|
|
import space.kscience.kmath.operations.*
|
2020-08-08 11:51:04 +03:00
|
|
|
|
|
|
|
// Using elements
|
|
|
|
val c1 = Complex(1.0, 1.0)
|
|
|
|
val c2 = Complex(1.0, -1.0)
|
|
|
|
val c3 = c1 + c2 + 3.0.toComplex()
|
|
|
|
|
|
|
|
// Using context
|
|
|
|
val c4 = ComplexField { c1 + i - 2.0 }
|
2019-01-05 20:15:36 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
Both notations have their pros and cons.
|
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
The hierarchy for algebraic elements follows the hierarchy for the corresponding algebraic structures.
|
2019-01-05 20:15:36 +03:00
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
**MathElement** ← **SpaceElement** ← **RingElement** ← **FieldElement**
|
2019-01-05 20:15:36 +03:00
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
`MathElement<C>` is the generic common ancestor of the class with context.
|
2019-01-05 20:15:36 +03:00
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
One major distinction between algebraic elements and algebraic contexts is that elements have three type
|
|
|
|
parameters:
|
2019-01-05 20:15:36 +03:00
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
1. The type of elements, the field operates on.
|
|
|
|
2. The self-type of the element returned from operation (which has to be an algebraic element).
|
2019-01-05 20:15:36 +03:00
|
|
|
3. The type of the algebra over first type-parameter.
|
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
The middle type is needed for of algebra members do not store context. For example, it is impossible to add a context
|
|
|
|
to regular `Double`. The element performs automatic conversions from context types and back. One should use context
|
|
|
|
operations in all performance-critical places. The performance of element operations is not guaranteed.
|
2019-02-20 15:24:51 +03:00
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
## Spaces and Fields
|
2019-02-20 15:24:51 +03:00
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
KMath submits both contexts and elements for builtin algebraic structures:
|
2019-02-20 15:24:51 +03:00
|
|
|
|
|
|
|
```kotlin
|
2021-02-18 11:17:28 +03:00
|
|
|
import space.kscience.kmath.operations.*
|
2020-08-08 11:51:04 +03:00
|
|
|
|
2019-02-20 15:24:51 +03:00
|
|
|
val c1 = Complex(1.0, 2.0)
|
|
|
|
val c2 = ComplexField.i
|
2020-08-08 11:51:04 +03:00
|
|
|
|
2019-02-20 15:24:51 +03:00
|
|
|
val c3 = c1 + c2
|
2020-08-08 11:51:04 +03:00
|
|
|
// or
|
|
|
|
val c3 = ComplexField { c1 + c2 }
|
2019-02-20 15:24:51 +03:00
|
|
|
```
|
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
Also, `ComplexField` features special operations to mix complex and real numbers, for example:
|
2019-02-20 15:24:51 +03:00
|
|
|
|
|
|
|
```kotlin
|
2021-02-18 11:17:28 +03:00
|
|
|
import space.kscience.kmath.operations.*
|
2020-08-08 11:51:04 +03:00
|
|
|
|
2019-02-20 15:24:51 +03:00
|
|
|
val c1 = Complex(1.0, 2.0)
|
2020-08-08 11:51:04 +03:00
|
|
|
val c2 = ComplexField { c1 - 1.0 } // Returns: Complex(re=0.0, im=2.0)
|
|
|
|
val c3 = ComplexField { c1 - i * 2.0 }
|
2019-02-20 15:24:51 +03:00
|
|
|
```
|
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
**Note**: In theory it is possible to add behaviors directly to the context, but as for now Kotlin does not support
|
|
|
|
that. Watch [KT-10468](https://youtrack.jetbrains.com/issue/KT-10468) and
|
|
|
|
[KEEP-176](https://github.com/Kotlin/KEEP/pull/176) for updates.
|
2019-02-20 15:24:51 +03:00
|
|
|
|
|
|
|
## Nested fields
|
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
Contexts allow one to build more complex structures. For example, it is possible to create a `Matrix` from complex
|
|
|
|
elements like so:
|
2019-02-20 15:24:51 +03:00
|
|
|
|
|
|
|
```kotlin
|
2020-08-08 11:51:04 +03:00
|
|
|
val element = NDElement.complex(shape = intArrayOf(2, 2)) { index: IntArray ->
|
2019-02-20 15:24:51 +03:00
|
|
|
Complex(index[0].toDouble() - index[1].toDouble(), index[0].toDouble() + index[1].toDouble())
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-08-08 11:51:04 +03:00
|
|
|
The `element` in this example is a member of the `Field` of 2D structures, each element of which is a member of its own
|
|
|
|
`ComplexField`. It is important one does not need to create a special n-d class to hold complex
|
2019-02-20 15:24:51 +03:00
|
|
|
numbers and implement operations on it, one just needs to provide a field for its elements.
|
|
|
|
|
|
|
|
**Note**: Fields themselves do not solve the problem of JVM boxing, but it is possible to solve with special contexts like
|
|
|
|
`MemorySpec`.
|