From 516444d1bc25cd810e685a00a7098f1ff38d97d6 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 16 May 2021 20:51:46 +0300 Subject: [PATCH] Fix and test --- .../space/kscience/kmath/expressions/MST.kt | 6 +++--- .../kscience/kmath/operations/Algebra.kt | 2 +- .../kscience/kmath/operations/LogicAlgebra.kt | 6 ++++++ .../kmath/expressions/ExpressionFieldTest.kt | 3 +-- .../kmath/expressions/InterpretTest.kt | 20 ++++++++++++++++--- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MST.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MST.kt index 0e046ae5c..7533024a1 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MST.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MST.kt @@ -76,7 +76,7 @@ public fun Algebra.evaluate(node: MST): T = when (node) { } } -internal class InnerAlgebra(val algebra: Algebra, val arguments: Map) : NumericAlgebra { +internal class InnerAlgebra(val algebra: Algebra, val arguments: Map) : NumericAlgebra { override fun bindSymbolOrNull(value: String): T? = algebra.bindSymbolOrNull(value) ?: arguments[StringSymbol(value)] override fun unaryOperation(operation: String, arg: T): T = @@ -101,7 +101,7 @@ internal class InnerAlgebra(val algebra: Algebra, val arguments: Map /** * Interprets the [MST] node with this [Algebra] and optional [arguments] */ -public fun MST.interpret(algebra: Algebra, arguments: Map): T = +public fun MST.interpret(algebra: Algebra, arguments: Map): T = InnerAlgebra(algebra, arguments).evaluate(this) /** @@ -111,7 +111,7 @@ public fun MST.interpret(algebra: Algebra, arguments: Map MST.interpret(algebra: Algebra, vararg arguments: Pair): T = +public fun MST.interpret(algebra: Algebra, vararg arguments: Pair): T = interpret(algebra, mapOf(*arguments)) /** diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt index 09bda6508..aa22b8c19 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Algebra.kt @@ -84,7 +84,7 @@ public interface Algebra { * @return an operation. */ public fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = - error("Binary operation $operation not defined in $this") + error("Binary operation '$operation; not defined in $this") /** * Dynamically invokes a binary operation with the certain name. diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt index 2bef3f407..406967897 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt @@ -27,12 +27,18 @@ public interface LogicAlgebra : Algebra { else -> super.unaryOperation(operation, arg) } + override fun unaryOperationFunction(operation: String): (arg: T) -> T = { unaryOperation(operation, it) } + override fun binaryOperation(operation: String, left: T, right: T): T = when (operation) { Boolean::and.name -> left.and(right) Boolean::or.name -> left.or(right) else -> super.binaryOperation(operation, left, right) } + override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = { l, r -> + binaryOperation(operation, l, r) + } + /** * Logic 'not' */ diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt index ace67db06..4d1b00b3d 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt @@ -6,7 +6,6 @@ package space.kscience.kmath.expressions import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.invoke import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFails @@ -16,7 +15,7 @@ class ExpressionFieldTest { @Test fun testExpression() { - val expression = FunctionalExpressionField(DoubleField).invoke { + val expression = with(FunctionalExpressionField(DoubleField)) { val x by binding() x * x + 2 * x + one } diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/InterpretTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/InterpretTest.kt index 0df9fe5cf..156334b2e 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/InterpretTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/InterpretTest.kt @@ -5,18 +5,32 @@ package space.kscience.kmath.expressions +import space.kscience.kmath.expressions.Symbol.Companion.x +import space.kscience.kmath.misc.UnstableKMathAPI +import space.kscience.kmath.operations.BooleanAlgebra import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.bindSymbol import space.kscience.kmath.operations.invoke import kotlin.test.Test +import kotlin.test.assertEquals + internal class InterpretTest { @Test fun interpretation() { val expr = MstField { - val x = bindSymbol(Symbol.x) x * 2.0 + number(2.0) / x - 16.0 }.toExpression(DoubleField) - expr(Symbol.x to 2.2) + assertEquals(-10.69, expr(x to 2.2), 0.02) + } + + @Test + @UnstableKMathAPI + fun booleanAlgebra() { + val expr = MstLogicAlgebra { + x and const(true) + }.toExpression(BooleanAlgebra) + + assertEquals(true, expr(x to true)) + assertEquals(false, expr(x to false)) } }