Feature/booleans #341

Merged
altavir merged 13 commits from feature/booleans into dev 2021-05-19 03:36:37 +03:00
5 changed files with 28 additions and 9 deletions
Showing only changes of commit 516444d1bc - Show all commits

View File

@ -76,7 +76,7 @@ public fun <T> Algebra<T>.evaluate(node: MST): T = when (node) {
} }
} }
internal class InnerAlgebra<T : Any>(val algebra: Algebra<T>, val arguments: Map<Symbol, T>) : NumericAlgebra<T> { internal class InnerAlgebra<T>(val algebra: Algebra<T>, val arguments: Map<Symbol, T>) : NumericAlgebra<T> {
override fun bindSymbolOrNull(value: String): T? = algebra.bindSymbolOrNull(value) ?: arguments[StringSymbol(value)] override fun bindSymbolOrNull(value: String): T? = algebra.bindSymbolOrNull(value) ?: arguments[StringSymbol(value)]
override fun unaryOperation(operation: String, arg: T): T = override fun unaryOperation(operation: String, arg: T): T =
@ -101,7 +101,7 @@ internal class InnerAlgebra<T : Any>(val algebra: Algebra<T>, val arguments: Map
/** /**
* Interprets the [MST] node with this [Algebra] and optional [arguments] * Interprets the [MST] node with this [Algebra] and optional [arguments]
*/ */
public fun <T : Any> MST.interpret(algebra: Algebra<T>, arguments: Map<Symbol, T>): T = public fun <T> MST.interpret(algebra: Algebra<T>, arguments: Map<Symbol, T>): T =
InnerAlgebra(algebra, arguments).evaluate(this) InnerAlgebra(algebra, arguments).evaluate(this)
/** /**
@ -111,7 +111,7 @@ public fun <T : Any> MST.interpret(algebra: Algebra<T>, arguments: Map<Symbol, T
* @param algebra the algebra that provides operations. * @param algebra the algebra that provides operations.
* @return the value of expression. * @return the value of expression.
*/ */
public fun <T : Any> MST.interpret(algebra: Algebra<T>, vararg arguments: Pair<Symbol, T>): T = public fun <T> MST.interpret(algebra: Algebra<T>, vararg arguments: Pair<Symbol, T>): T =
interpret(algebra, mapOf(*arguments)) interpret(algebra, mapOf(*arguments))
/** /**

View File

@ -84,7 +84,7 @@ public interface Algebra<T> {
* @return an operation. * @return an operation.
*/ */
public fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = 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. * Dynamically invokes a binary operation with the certain name.

View File

@ -27,12 +27,18 @@ public interface LogicAlgebra<T : Any> : Algebra<T> {
else -> super.unaryOperation(operation, arg) 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) { override fun binaryOperation(operation: String, left: T, right: T): T = when (operation) {
Boolean::and.name -> left.and(right) Boolean::and.name -> left.and(right)
Boolean::or.name -> left.or(right) Boolean::or.name -> left.or(right)
else -> super.binaryOperation(operation, left, 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' * Logic 'not'
*/ */

View File

@ -6,7 +6,6 @@
package space.kscience.kmath.expressions package space.kscience.kmath.expressions
import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.invoke
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertFails import kotlin.test.assertFails
@ -16,7 +15,7 @@ class ExpressionFieldTest {
@Test @Test
fun testExpression() { fun testExpression() {
val expression = FunctionalExpressionField(DoubleField).invoke { val expression = with(FunctionalExpressionField(DoubleField)) {
val x by binding() val x by binding()
x * x + 2 * x + one x * x + 2 * x + one
} }

View File

@ -5,18 +5,32 @@
package space.kscience.kmath.expressions 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.DoubleField
import space.kscience.kmath.operations.bindSymbol
import space.kscience.kmath.operations.invoke import space.kscience.kmath.operations.invoke
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals
internal class InterpretTest { internal class InterpretTest {
@Test @Test
fun interpretation() { fun interpretation() {
val expr = MstField { val expr = MstField {
val x = bindSymbol(Symbol.x)
x * 2.0 + number(2.0) / x - 16.0 x * 2.0 + number(2.0) / x - 16.0
}.toExpression(DoubleField) }.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))
} }
} }