From a1f0188b8b0b3d5577c4ba8dec31ccf72ff2834e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 16 Jun 2020 16:29:52 +0300 Subject: [PATCH] Functional expression builders --- .../FunctionalExpressionAlgebra.kt | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/FunctionalExpressionAlgebra.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/FunctionalExpressionAlgebra.kt index c8d6e8eb0..a8a26aa33 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/FunctionalExpressionAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/FunctionalExpressionAlgebra.kt @@ -36,12 +36,10 @@ internal class FunctionalConstProductExpression( /** * A context class for [Expression] construction. + * + * @param algebra The algebra to provide for Expressions built. */ -interface FunctionalExpressionAlgebra> : ExpressionAlgebra> { - /** - * The algebra to provide for Expressions built. - */ - val algebra: A +abstract class FunctionalExpressionAlgebra>(val algebra: A) : ExpressionAlgebra> { /** * Builds an Expression of constant expression which does not depend on arguments. @@ -69,8 +67,8 @@ interface FunctionalExpressionAlgebra> : ExpressionAlgebra>(override val algebra: A) : - FunctionalExpressionAlgebra, Space> { +open class FunctionalExpressionSpace>(algebra: A) : + FunctionalExpressionAlgebra(algebra), Space> { override val zero: Expression get() = const(algebra.zero) @@ -98,7 +96,7 @@ open class FunctionalExpressionSpace>(override val algebra: A) : super.binaryOperation(operation, left, right) } -open class FunctionalExpressionRing(override val algebra: A) : FunctionalExpressionSpace(algebra), +open class FunctionalExpressionRing(algebra: A) : FunctionalExpressionSpace(algebra), Ring> where A : Ring, A : NumericAlgebra { override val one: Expression get() = const(algebra.one) @@ -119,7 +117,7 @@ open class FunctionalExpressionRing(override val algebra: A) : FunctionalE super.binaryOperation(operation, left, right) } -open class FunctionalExpressionField(override val algebra: A) : +open class FunctionalExpressionField(algebra: A) : FunctionalExpressionRing(algebra), Field> where A : Field, A : NumericAlgebra { /** @@ -137,3 +135,12 @@ open class FunctionalExpressionField(override val algebra: A) : override fun binaryOperation(operation: String, left: Expression, right: Expression): Expression = super.binaryOperation(operation, left, right) } + +inline fun > A.expressionInSpace(block: FunctionalExpressionSpace.() -> Expression): Expression = + FunctionalExpressionSpace(this).block() + +inline fun > A.expressionInRing(block: FunctionalExpressionRing.() -> Expression): Expression = + FunctionalExpressionRing(this).block() + +inline fun > A.expressionInField(block: FunctionalExpressionField.() -> Expression): Expression = + FunctionalExpressionField(this).block() \ No newline at end of file