diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt index 42c7e9377..ee9833623 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt @@ -1,20 +1,7 @@ package scientifik.kmath.operations -/** - * A general interface representing linear context of some kind. - * The context defines sum operation for its elements and multiplication by real value. - * One must note that in some cases context is a singleton class, but in some cases it - * works as a context for operations inside it. - * - * TODO do we need non-commutative context? - */ -interface Space { - /** - * Neutral element for sum operation - */ - val zero: T - +interface SpaceOperations { /** * Addition operation for two context elements */ @@ -35,21 +22,39 @@ interface Space { operator fun Number.times(b: T) = b * this } -/** - * The same as {@link Space} but with additional multiplication operation - */ -interface Ring : Space { - /** - * neutral operation for multiplication - */ - val one: T +/** + * A general interface representing linear context of some kind. + * The context defines sum operation for its elements and multiplication by real value. + * One must note that in some cases context is a singleton class, but in some cases it + * works as a context for operations inside it. + * + * TODO do we need non-commutative context? + */ +interface Space : SpaceOperations { + /** + * Neutral element for sum operation + */ + val zero: T +} + +interface RingOperations : SpaceOperations { /** * Multiplication for two field elements */ fun multiply(a: T, b: T): T operator fun T.times(b: T): T = multiply(this, b) +} + +/** + * The same as {@link Space} but with additional multiplication operation + */ +interface Ring : Space, RingOperations { + /** + * neutral operation for multiplication + */ + val one: T // operator fun T.plus(b: Number) = this.plus(b * one) // operator fun Number.plus(b: T) = b + this @@ -59,11 +64,17 @@ interface Ring : Space { } /** - * Four operations algebra + * All ring operations but without neutral elements */ -interface Field : Ring { +interface FieldOperations : RingOperations { fun divide(a: T, b: T): T operator fun T.div(b: T): T = divide(this, b) +} + +/** + * Four operations algebra + */ +interface Field : Ring, FieldOperations { operator fun Number.div(b: T) = this * divide(one, b) }