-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
TestSymbolic.kt
47 lines (39 loc) · 1.43 KB
/
TestSymbolic.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package ai.hypergraph.kotlingrad.evaluation
import ai.hypergraph.kotlingrad.*
import ai.hypergraph.kotlingrad.api.*
import io.kotest.core.spec.style.StringSpec
import io.kotest.property.*
import javax.script.*
import javax.script.ScriptContext.ENGINE_SCOPE
@Suppress("NonAsciiCharacters")
class TestSymbolic : StringSpec({
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
val x by SVar(DReal)
val y by SVar(DReal)
val z by SVar(DReal)
fun ktf(f: SFun<DReal>, vararg kgBnds: Pair<SVar<DReal>, Number>) =
engine.run {
try {
val bnds = kgBnds.associate { it.first.name to it.second.toDouble() }
setBindings(SimpleBindings(bnds), ENGINE_SCOPE)
val expr = "import kotlin.math.*; $f"
eval(expr)
} catch (e: Exception) {
System.err.println("Failed to evaluate expression: $f")
throw e
}
}
"test symbolic evaluation".config(enabled = false) {
checkAll(10, TestExpressionGenerator(DReal)) { f ->
checkAll(3, DoubleGenerator, DoubleGenerator, DoubleGenerator) { ẋ, ẏ, ż ->
f(x to ẋ, y to ẏ, z to ż) shouldBeAbout ktf(f, x to ẋ, y to ẏ, z to ż)
}
}
}
})
class TestExpressionGenerator<X : RealNumber<X, *>>(proto: X) : Arb<SFun<X>>() {
val expGen = ExpressionGenerator<X>()
override fun sample(rs: RandomSource): Sample<SFun<X>> =
Sample(expGen.randomBiTree())
override fun edgecase(rs: RandomSource) = null
}