Extended documentation, code refactoring, API consistency changes #125

Merged
CommanderTvis merged 12 commits from more-docs into dev 2020-08-09 18:48:27 +03:00
20 changed files with 189 additions and 134 deletions
Showing only changes of commit 6114099e68 - Show all commits

View File

@ -1,101 +1,115 @@
# Algebra and algebra elements
# Algebraic Structures and Algebraic Elements
The mathematical operations in `kmath` are generally separated from mathematical objects.
This means that in order to perform an operation, say `+`, one needs two objects of a type `T` and
and algebra context which defines appropriate operation, say `Space<T>`. Next one needs to run actual operation
in the context:
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:
```kotlin
val a: T
val b: T
val space: Space<T>
import scientifik.kmath.operations.*
val c = space.run{a + b}
val a: T = ...
val b: T = ...
val space: Space<T> = ...
val c = space { a + b }
```
From the first glance, this distinction seems to be a needless complication, but in fact one needs
to remember that in mathematics, one could define different operations on the same objects. For example,
one could use different types of geometry for vectors.
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.
## Algebra hierarchy
## Algebraic Structures
Mathematical contexts have the following hierarchy:
**Space** <- **Ring** <- **Field**
**Algebra** ← **Space****Ring** ← **Field**
All classes follow abstract mathematical constructs.
[Space](http://mathworld.wolfram.com/Space.html) defines `zero` element, addition operation and multiplication by constant,
[Ring](http://mathworld.wolfram.com/Ring.html) adds multiplication and unit `one` element,
[Field](http://mathworld.wolfram.com/Field.html) adds division operation.
These interfaces follow real algebraic structures:
Typical case of `Field` is the `RealField` which works on doubles. And typical case of `Space` is a `VectorSpace`.
- [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.
In some cases algebra context could hold additional operation like `exp` or `sin`, in this case it inherits appropriate
interface. Also a context could have an operation which produces an element outside of its context. For example
`Matrix` `dot` operation produces a matrix with new dimensions which can be incompatible with initial matrix in
terms of linear operations.
A typical implementation of `Field<T>` is the `RealField` which works on doubles, and `VectorSpace` for `Space<T>`.
## Algebra element
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.
In order to achieve more familiar behavior (where you apply operations directly to mathematical objects), without involving contexts
`kmath` introduces special type objects called `MathElement`. A `MathElement` is basically some object coupled to
## Algebraic Element
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
a mathematical context. For example `Complex` is the pair of real numbers representing real and imaginary parts,
but it also holds reference to the `ComplexField` singleton which allows to perform direct operations on `Complex`
but it also holds reference to the `ComplexField` singleton, which allows performing direct operations on `Complex`
numbers without explicit involving the context like:
```kotlin
import scientifik.kmath.operations.*
// Using elements
val c1 = Complex(1.0, 1.0)
val c2 = Complex(1.0, -1.0)
val c3 = c1 + c2 + 3.0.toComplex()
//or with field notation:
val c4 = ComplexField.run{c1 + i - 2.0}
// Using context
val c4 = ComplexField { c1 + i - 2.0 }
```
Both notations have their pros and cons.
The hierarchy for algebra elements follows the hierarchy for the corresponding algebra.
The hierarchy for algebraic elements follows the hierarchy for the corresponding algebraic structures.
**MathElement** <- **SpaceElement** <- **RingElement** <- **FieldElement**
**MathElement** **SpaceElement****RingElement** ← **FieldElement**
**MathElement** is the generic common ancestor of the class with context.
`MathElement<C>` is the generic common ancestor of the class with context.
One important distinction between algebra elements and algebra contexts is that algebra element has three type parameters:
One major distinction between algebraic elements and algebraic contexts is that elements have three type
parameters:
1. The type of elements, field operates on.
2. The self-type of the element returned from operation (must be algebra element).
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).
3. The type of the algebra over first type-parameter.
The middle type is needed in case algebra members do not store context. For example, it is not possible to add
a context to regular `Double`. The element performs automatic conversions from context types and back.
One should used context operations in all important places. The performance of element operations is not guaranteed.
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.
## Spaces and fields
## Spaces and Fields
An obvious first choice of mathematical objects to implement in a context-oriented style are algebraic elements like spaces,
rings and fields. Those are located in the `scientifik.kmath.operations.Algebra.kt` file. Alongside common contexts, the file includes definitions for algebra elements like `FieldElement`. A `FieldElement` object
stores a reference to the `Field` which contains additive and multiplicative operations, meaning
it has one fixed context attached and does not require explicit external context. So those `MathElements` can be operated without context:
KMath submits both contexts and elements for builtin algebraic structures:
```kotlin
import scientifik.kmath.operations.*
val c1 = Complex(1.0, 2.0)
val c2 = ComplexField.i
val c3 = c1 + c2
// or
val c3 = ComplexField { c1 + c2 }
```
`ComplexField` also features special operations to mix complex and real numbers, for example:
Also, `ComplexField` features special operations to mix complex and real numbers, for example:
```kotlin
import scientifik.kmath.operations.*
val c1 = Complex(1.0, 2.0)
val c2 = ComplexField.run{ c1 - 1.0} // Returns: [re:0.0, im: 2.0]
val c3 = ComplexField.run{ c1 - i*2.0}
val c2 = ComplexField { c1 - 1.0 } // Returns: Complex(re=0.0, im=2.0)
val c3 = ComplexField { c1 - i * 2.0 }
```
**Note**: In theory it is possible to add behaviors directly to the context, but currently kotlin syntax 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.
**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.
## Nested fields
Contexts allow one to build more complex structures. For example, it is possible to create a `Matrix` from complex elements like so:
Contexts allow one to build more complex structures. For example, it is possible to create a `Matrix` from complex
elements like so:
```kotlin
val element = NDElement.complex(shape = intArrayOf(2, 2)) { index: IntArray ->
@ -103,8 +117,8 @@ val element = NDElement.complex(shape = intArrayOf(2,2)){ index: IntArray ->
}
```
The `element` in this example is a member of the `Field` of 2-d structures, each element of which is a member of its own
`ComplexField`. The important thing is one does not need to create a special n-d class to hold complex
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
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

View File

@ -1,4 +1,5 @@
# Buffers
Buffer is one of main building blocks of kmath. It is a basic interface allowing random-access read and write (with `MutableBuffer`).
There are different types of buffers:
@ -12,4 +13,5 @@ Some kmath features require a `BufferFactory` class to operate properly. A gener
buffer for given reified type (for types with custom memory buffer it still better to use their own `MemoryBuffer.create()` factory).
## Buffer performance
One should avoid using default boxing buffer wherever it is possible. Try to use primitive buffers or memory buffers instead

View File

@ -1,22 +1,34 @@
# Local coding conventions
# Coding Conventions
Kmath and other `scientifik` projects use general [kotlin code conventions](https://kotlinlang.org/docs/reference/coding-conventions.html), but with a number of small changes and clarifications.
KMath code follows general [Kotlin conventions](https://kotlinlang.org/docs/reference/coding-conventions.html), but
with a number of small changes and clarifications.
## Utility class names
File name should coincide with a name of one of the classes contained in the file or start with small letter and describe its contents.
## Utility Class Naming
The code convention [here](https://kotlinlang.org/docs/reference/coding-conventions.html#source-file-names) says that file names should start with capital letter even if file does not contain classes. Yet starting utility classes and aggregators with a small letter seems to be a good way to clearly visually separate those files.
Filename should coincide with a name of one of the classes contained in the file or start with small letter and
describe its contents.
The code convention [here](https://kotlinlang.org/docs/reference/coding-conventions.html#source-file-names) says that
file names should start with a capital letter even if file does not contain classes. Yet starting utility classes and
aggregators with a small letter seems to be a good way to visually separate those files.
This convention could be changed in future in a non-breaking way.
## Private variable names
Private variable names could start with underscore `_` in case the private mutable variable is shadowed by the public read-only value with the same meaning.
## Private Variable Naming
Code convention do not permit underscores in names, but is is sometimes useful to "underscore" the fact that public and private versions define the same entity. It is allowed only for private variables.
Private variables' names may start with underscore `_` for of the private mutable variable is shadowed by the public
read-only value with the same meaning.
This rule does not permit underscores in names, but it is sometimes useful to "underscore" the fact that public and
private versions draw up the same entity. It is allowed only for private variables.
This convention could be changed in future in a non-breaking way.
## Functions and properties one-liners
Use one-liners when they occupy single code window line both for functions and properties with getters like `val b: String get() = "fff"`. The same should be done with multiline expressions when they could be cleanly separated.
## Functions and Properties One-liners
There is not general consensus whenever use `fun a() = {}` or `fun a(){return}`. Yet from reader perspective one-lines seem to better show that the property or function is easily calculated.
Use one-liners when they occupy single code window line both for functions and properties with getters like
`val b: String get() = "fff"`. The same should be performed with multiline expressions when they could be
cleanly separated.
There is no universal consensus whenever use `fun a() = ...` or `fun a() { return ... }`. Yet from reader outlook
one-lines seem to better show that the property or function is easily calculated.

View File

@ -1,6 +1,6 @@
## Basic linear algebra layout
Kmath support for linear algebra organized in a context-oriented way. Meaning that operations are in most cases declared
KMath support for linear algebra organized in a context-oriented way. Meaning that operations are in most cases declared
in context classes, and are not the members of classes that store data. This allows more flexible approach to maintain multiple
back-ends. The new operations added as extensions to contexts instead of being member functions of data structures.

View File

@ -1,4 +1,4 @@
# Nd-structure generation and operations
# ND-structure generation and operations
**TODO**

View File

@ -1,4 +1,4 @@
# Abstract syntax tree expression representation and operations (`kmath-ast`)
# Abstract Syntax Tree Expression Representation and Operations (`kmath-ast`)
This subproject implements the following features:
@ -38,7 +38,7 @@ This subproject implements the following features:
> ```
>
## Dynamic expression code generation with ObjectWeb ASM
## Dynamic Expression Code Generation with ObjectWeb ASM
`kmath-ast` JVM module supports runtime code generation to eliminate overhead of tree traversal. Code generator builds
a special implementation of `Expression<T>` with implemented `invoke` function.
@ -75,7 +75,7 @@ public final class AsmCompiledExpression_1073786867_0 implements Expression<Doub
### Example Usage
This API is an extension to MST and MstExpression, so you may optimize as both of them:
This API extends MST and MstExpression, so you may optimize as both of them:
```kotlin
RealField.mstInField { symbol("x") + 2 }.compile()

40
kmath-core/README.md Normal file
View File

@ -0,0 +1,40 @@
# The Core Module (`kmath-ast`)
The core features of KMath:
- Algebraic structures: contexts and elements.
- ND structures.
- Buffers.
- Functional Expressions.
- Domains.
- Automatic differentiation.
> #### Artifact:
> This module is distributed in the artifact `scientifik:kmath-core:0.1.4-dev-8`.
>
> **Gradle:**
>
> ```gradle
> repositories {
> maven { url 'https://dl.bintray.com/mipt-npm/scientifik' }
> maven { url 'https://dl.bintray.com/mipt-npm/dev' }
> maven { url https://dl.bintray.com/hotkeytlt/maven' }
> }
>
> dependencies {
> implementation 'scientifik:kmath-core:0.1.4-dev-8'
> }
> ```
> **Gradle Kotlin DSL:**
>
> ```kotlin
> repositories {
> maven("https://dl.bintray.com/mipt-npm/scientifik")
> maven("https://dl.bintray.com/mipt-npm/dev")
> maven("https://dl.bintray.com/hotkeytlt/maven")
> }
>
> dependencies {``
> implementation("scientifik:kmath-core:0.1.4-dev-8")
> }
> ```

View File

@ -1,11 +1,7 @@
plugins {
id("scientifik.mpp")
}
plugins { id("scientifik.mpp") }
kotlin.sourceSets {
commonMain {
dependencies {
api(project(":kmath-memory"))
}
dependencies { api(project(":kmath-memory")) }
}
}

View File

@ -58,7 +58,8 @@ interface NumericAlgebra<T> : Algebra<T> {
inline operator fun <A : Algebra<*>, R> A.invoke(block: A.() -> R): R = run(block)
/**
* Represents semigroup, i.e. algebraic structure with associative binary operation called "addition".
* Represents semispace, i.e. algebraic structure with associative binary operation called "addition" as well as
* multiplication by scalars.
*
* In KMath groups are called spaces, and also define multiplication of element by [Number].
*
@ -174,10 +175,8 @@ interface SpaceOperations<T> : Algebra<T> {
}
/**
* Represents group, i.e. algebraic structure with associative binary operation called "addition" and its neutral
* element.
*
* In KMath groups are called spaces, and also define multiplication of element by [Number].
* Represents linear space, i.e. algebraic structure with associative binary operation called "addition" and its neutral
* element as well as multiplication by scalars.
*
* @param T the type of element of this group.
*/

View File

@ -22,7 +22,6 @@ import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
/**
* A not-necessary-Markov chain of some type
* @param R - the chain element type
@ -71,7 +70,7 @@ class MarkovChain<out R : Any>(private val seed: suspend () -> R, private val ge
private var value: R? = null
fun value() = value
fun value(): R? = value
override suspend fun next(): R {
mutex.withLock {
@ -97,12 +96,11 @@ class StatefulChain<S, out R>(
private val forkState: ((S) -> S),
private val gen: suspend S.(R) -> R
) : Chain<R> {
private val mutex = Mutex()
private val mutex: Mutex = Mutex()
private var value: R? = null
fun value() = value
fun value(): R? = value
override suspend fun next(): R {
mutex.withLock {
@ -112,9 +110,7 @@ class StatefulChain<S, out R>(
}
}
override fun fork(): Chain<R> {
return StatefulChain(forkState(state), seed, forkState, gen)
}
override fun fork(): Chain<R> = StatefulChain(forkState(state), seed, forkState, gen)
}
/**
@ -163,7 +159,8 @@ fun <T, R> Chain<T>.collect(mapper: suspend (Chain<T>) -> R): Chain<R> = object
fun <T, S, R> Chain<T>.collectWithState(state: S, stateFork: (S) -> S, mapper: suspend S.(Chain<T>) -> R): Chain<R> =
object : Chain<R> {
override suspend fun next(): R = state.mapper(this@collectWithState)
override fun fork(): Chain<R> = this@collectWithState.fork().collectWithState(stateFork(state), stateFork, mapper)
override fun fork(): Chain<R> =
this@collectWithState.fork().collectWithState(stateFork(state), stateFork, mapper)
}
/**

View File

@ -4,7 +4,8 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.flow.*
val Dispatchers.Math: CoroutineDispatcher get() = Dispatchers.Default
val Dispatchers.Math: CoroutineDispatcher
get() = Default
/**
* An imitator of [Deferred] which holds a suspended function block and dispatcher
@ -42,7 +43,7 @@ fun <T, R> Flow<T>.async(
}
@FlowPreview
fun <T, R> AsyncFlow<T>.map(action: (T) -> R) =
fun <T, R> AsyncFlow<T>.map(action: (T) -> R): AsyncFlow<R> =
AsyncFlow(deferredFlow.map { input ->
//TODO add function composition
LazyDeferred(input.dispatcher) {
@ -82,9 +83,9 @@ suspend fun <T> AsyncFlow<T>.collect(concurrency: Int, collector: FlowCollector<
@ExperimentalCoroutinesApi
@FlowPreview
suspend fun <T> AsyncFlow<T>.collect(concurrency: Int, action: suspend (value: T) -> Unit): Unit {
suspend fun <T> AsyncFlow<T>.collect(concurrency: Int, action: suspend (value: T) -> Unit) {
collect(concurrency, object : FlowCollector<T> {
override suspend fun emit(value: T) = action(value)
override suspend fun emit(value: T): Unit = action(value)
})
}
@ -98,5 +99,3 @@ fun <T, R> Flow<T>.mapParallel(
flow { emit(transform(value)) }
}.flowOn(dispatcher)
}

View File

@ -11,7 +11,7 @@ import scientifik.kmath.structures.asBuffer
/**
* Create a [Flow] from buffer
*/
fun <T> Buffer<T>.asFlow() = iterator().asFlow()
fun <T> Buffer<T>.asFlow(): Flow<T> = iterator().asFlow()
/**
* Flat map a [Flow] of [Buffer] into continuous [Flow] of elements

View File

@ -5,7 +5,6 @@ import kotlinx.coroutines.sync.withLock
import scientifik.kmath.structures.Buffer
import scientifik.kmath.structures.MutableBuffer
import scientifik.kmath.structures.VirtualBuffer
import kotlin.reflect.KClass
/**
* Thread-safe ring buffer
@ -16,8 +15,7 @@ class RingBuffer<T>(
private var startIndex: Int = 0,
size: Int = 0
) : Buffer<T> {
private val mutex = Mutex()
private val mutex: Mutex = Mutex()
override var size: Int = size
private set
@ -28,7 +26,7 @@ class RingBuffer<T>(
return buffer[startIndex.forward(index)] as T
}
fun isFull() = size == buffer.size
fun isFull(): Boolean = size == buffer.size
/**
* Iterator could provide wrong results if buffer is changed in initialization (iteration is safe)

View File

@ -6,7 +6,7 @@ import kotlin.sequences.Sequence
/**
* Represent a chain as regular iterator (uses blocking calls)
*/
operator fun <R> Chain<R>.iterator() = object : Iterator<R> {
operator fun <R> Chain<R>.iterator(): Iterator<R> = object : Iterator<R> {
override fun hasNext(): Boolean = true
override fun next(): R = runBlocking { next() }

View File

@ -8,10 +8,9 @@ class LazyNDStructure<T>(
override val shape: IntArray,
val function: suspend (IntArray) -> T
) : NDStructure<T> {
private val cache: MutableMap<IntArray, Deferred<T>> = hashMapOf()
private val cache = HashMap<IntArray, Deferred<T>>()
fun deferred(index: IntArray) = cache.getOrPut(index) {
fun deferred(index: IntArray): Deferred<T> = cache.getOrPut(index) {
scope.async(context = Dispatchers.Math) {
function(index)
}
@ -42,21 +41,21 @@ class LazyNDStructure<T>(
result = 31 * result + cache.hashCode()
return result
}
}
fun <T> NDStructure<T>.deferred(index: IntArray) =
fun <T> NDStructure<T>.deferred(index: IntArray): Deferred<T> =
if (this is LazyNDStructure<T>) this.deferred(index) else CompletableDeferred(get(index))
suspend fun <T> NDStructure<T>.await(index: IntArray) =
suspend fun <T> NDStructure<T>.await(index: IntArray): T =
if (this is LazyNDStructure<T>) this.await(index) else get(index)
/**
* PENDING would benifit from KEEP-176
* PENDING would benefit from KEEP-176
*/
fun <T, R> NDStructure<T>.mapAsyncIndexed(scope: CoroutineScope, function: suspend (T, index: IntArray) -> R) =
LazyNDStructure(scope, shape) { index -> function(get(index), index) }
fun <T, R> NDStructure<T>.mapAsyncIndexed(
scope: CoroutineScope,
function: suspend (T, index: IntArray) -> R
): LazyNDStructure<R> = LazyNDStructure(scope, shape) { index -> function(get(index), index) }
fun <T, R> NDStructure<T>.mapAsync(scope: CoroutineScope, function: suspend (T) -> R) =
fun <T, R> NDStructure<T>.mapAsync(scope: CoroutineScope, function: suspend (T) -> R): LazyNDStructure<R> =
LazyNDStructure(scope, shape) { index -> function(get(index)) }

View File

@ -15,8 +15,7 @@ import kotlin.test.Test
@InternalCoroutinesApi
@FlowPreview
class BufferFlowTest {
val dispatcher = Executors.newFixedThreadPool(4).asCoroutineDispatcher()
val dispatcher: CoroutineDispatcher = Executors.newFixedThreadPool(4).asCoroutineDispatcher()
@Test
@Timeout(2000)

View File

@ -22,14 +22,14 @@ class RingBufferTest {
fun windowed() {
val flow = flow {
var i = 0
while(true){
emit(i++)
}
while (true) emit(i++)
}
val windowed = flow.windowed(10)
runBlocking {
val first = windowed.take(1).single()
val res = windowed.take(15).map { it -> it.asSequence().average() }.toList()
val res = windowed.take(15).map { it.asSequence().average() }.toList()
assertEquals(0.0, res[0])
assertEquals(4.5, res[9])
assertEquals(9.5, res[14])