Very experimental WASM code generation by MST in contexts of Int and Real #158
@ -1,8 +1,9 @@
|
|||||||
package kscience.kmath.ast
|
package kscience.kmath.ast
|
||||||
|
|
||||||
import kscience.kmath.expressions.invoke
|
import kscience.kmath.expressions.invoke
|
||||||
|
import kscience.kmath.operations.IntRing
|
||||||
import kscience.kmath.operations.RealField
|
import kscience.kmath.operations.RealField
|
||||||
import kscience.kmath.operations.invoke
|
import kscience.kmath.wasm.compile
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
@ -11,27 +12,28 @@ import kotlin.time.measureTime
|
|||||||
internal class Test {
|
internal class Test {
|
||||||
@Test
|
@Test
|
||||||
fun int() {
|
fun int() {
|
||||||
val res = IntWasmBuilder(MstRing { number(100000000) + number(10000000) }).instance()
|
val res = IntRing.mstInRing { number(100000000) + number(10000000) }.compile()()
|
||||||
assertEquals(110000000, res)
|
assertEquals(110000000, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun real() {
|
fun real() {
|
||||||
val res = RealWasmBuilder(MstExtendedField { number(100000000) + number(2).pow(10) }).instance()
|
val res = RealField.mstInExtendedField { number(100000000) + number(2).pow(10) }.compile()()
|
||||||
assertEquals(100001024.0, res)
|
assertEquals(100001024.0, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun argsPassing() {
|
fun argsPassing() {
|
||||||
val res = RealWasmBuilder(MstExtendedField { symbol("y") + symbol("x").pow(10) })
|
val res = RealField
|
||||||
.instance("x" to 2.0, "y" to 100000000.0)
|
.mstInExtendedField { symbol("y") + symbol("x").pow(10) }
|
||||||
|
.compile()("x" to 2.0, "y" to 100000000.0)
|
||||||
|
|
||||||
assertEquals(100001024.0, res)
|
assertEquals(100001024.0, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun powFunction() {
|
fun powFunction() {
|
||||||
val expr = RealWasmBuilder(MstExtendedField { symbol("x").pow(1.0 / 6.0) }).instance
|
val expr = RealField.mstInExtendedField { symbol("x").pow(1.0 / 6.0) }.compile()
|
||||||
assertEquals(0.9730585187140817, expr("x" to 0.8488554755054833))
|
assertEquals(0.9730585187140817, expr("x" to 0.8488554755054833))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,20 +44,12 @@ internal class Test {
|
|||||||
var rng = Random(0)
|
var rng = Random(0)
|
||||||
var sum1 = 0.0
|
var sum1 = 0.0
|
||||||
var sum2 = 0.0
|
var sum2 = 0.0
|
||||||
|
val e2 = RealField.mstInExtendedField { symbol("x").pow(1.0 / 6.0) }
|
||||||
measureTime {
|
val e1 = e2.compile()
|
||||||
val res = RealWasmBuilder(MstExtendedField { symbol("x").pow(1.0 / 6.0) }).instance
|
measureTime { repeat(times) { sum1 += e1("x" to rng.nextDouble()) } }.also(::println)
|
||||||
repeat(times) { sum1 += res("x" to rng.nextDouble()) }
|
|
||||||
}.also(::println)
|
|
||||||
|
|
||||||
println("MST")
|
println("MST")
|
||||||
rng = Random(0)
|
rng = Random(0)
|
||||||
|
measureTime { repeat(times) { sum2 += e2("x" to rng.nextDouble()) } }.also(::println)
|
||||||
measureTime {
|
|
||||||
val res = RealField.mstInExtendedField { symbol("x").pow(1.0 / 6.0) }
|
|
||||||
repeat(times) { sum2 += res("x" to rng.nextDouble()) }
|
|
||||||
}.also(::println)
|
|
||||||
|
|
||||||
assertEquals(sum1, sum2)
|
assertEquals(sum1, sum2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user