Fix and test
This commit is contained in:
parent
15d874fb06
commit
516444d1bc
@ -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 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]
|
||||
*/
|
||||
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)
|
||||
|
||||
/**
|
||||
@ -111,7 +111,7 @@ public fun <T : Any> MST.interpret(algebra: Algebra<T>, arguments: Map<Symbol, T
|
||||
* @param algebra the algebra that provides operations.
|
||||
* @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))
|
||||
|
||||
/**
|
||||
|
@ -84,7 +84,7 @@ public interface Algebra<T> {
|
||||
* @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.
|
||||
|
@ -27,12 +27,18 @@ public interface LogicAlgebra<T : Any> : Algebra<T> {
|
||||
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'
|
||||
*/
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user