Merge remote-tracking branch 'origin/adv-expr' into adv-expr-benchmarks-2

This commit is contained in:
Iaroslav 2020-06-24 21:20:31 +07:00
commit c78663dfe7
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
5 changed files with 92 additions and 67 deletions

View File

@ -368,7 +368,7 @@ internal class AsmBuilder<T> internal constructor(
tArity: Int,
opcode: Int = Opcodes.INVOKEINTERFACE
) {
repeat(tArity) { typeStack.pop() }
repeat(tArity) { if (!typeStack.empty()) typeStack.pop() }
invokeMethodVisitor.visitMethodInsn(
opcode,

View File

@ -1,9 +1,12 @@
package scietifik.kmath.asm
import scientifik.kmath.asm.compile
import scientifik.kmath.ast.mstInField
import scientifik.kmath.ast.mstInRing
import scientifik.kmath.ast.mstInSpace
import scientifik.kmath.expressions.invoke
import scientifik.kmath.operations.ByteRing
import scientifik.kmath.operations.RealField
import kotlin.test.Test
import kotlin.test.assertEquals
@ -45,65 +48,63 @@ class TestAsmAlgebras {
assertEquals(res1, res2)
}
// @Test
// fun space() {
// val res = ByteRing.asm {
// binaryOperation(
// "+",
//
// unaryOperation(
// "+",
// 3.toByte() - (2.toByte() + (multiply(
// add(number(1), number(1)),
// 2
// ) + 1.toByte()) * 3.toByte() - 1.toByte())
// ),
//
// number(1)
// ) + symbol("x") + zero
// }("x" to 2.toByte())
//
// assertEquals(16, res)
// }
//
// @Test
// fun ring() {
// val res = ByteRing.asmInRing {
// binaryOperation(
// "+",
//
// unaryOperation(
// "+",
// (3.toByte() - (2.toByte() + (multiply(
// add(const(1), const(1)),
// 2
// ) + 1.toByte()))) * 3.0 - 1.toByte()
// ),
//
// number(1)
// ) * const(2)
// }()
//
// assertEquals(24, res)
// }
//
// @Test
// fun field() {
// val res = RealField.asmInField {
// +(3 - 2 + 2*(number(1)+1.0)
//
// unaryOperation(
// "+",
// (3.0 - (2.0 + (multiply(
// add((1.0), const(1.0)),
// 2
// ) + 1.0))) * 3 - 1.0
// )+
//
// number(1)
// ) / 2, const(2.0)) * one
// }()
//
// assertEquals(3.0, res)
// }
@Test
fun ring() {
val res1 = ByteRing.mstInRing {
binaryOperation(
"+",
unaryOperation(
"+",
(symbol("x") - (2.toByte() + (multiply(
add(number(1), number(1)),
2
) + 1.toByte()))) * 3.0 - 1.toByte()
),
number(1)
) * number(2)
}("x" to 3.toByte())
val res2 = ByteRing.mstInRing {
binaryOperation(
"+",
unaryOperation(
"+",
(symbol("x") - (2.toByte() + (multiply(
add(number(1), number(1)),
2
) + 1.toByte()))) * 3.0 - 1.toByte()
),
number(1)
) * number(2)
}.compile()("x" to 3.toByte())
assertEquals(res1, res2)
}
@Test
fun field() {
val res1 = RealField.mstInField {
+(3 - 2 + 2 * number(1) + 1.0) + binaryOperation(
"+",
(3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0
+ number(1),
1 / 2 + number(2.0) * one
)
}("x" to 2.0)
val res2 = RealField.mstInField {
+(3 - 2 + 2 * number(1) + 1.0) + binaryOperation(
"+",
(3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0
+ number(1),
1 / 2 + number(2.0) * one
)
}.compile()("x" to 2.0)
assertEquals(res1, res2)
}
}

View File

@ -16,6 +16,13 @@ class TestAsmExpressions {
assertEquals(-2.0, res)
}
@Test
fun testBinaryOperationInvocation() {
val expression = RealField.mstInSpace { -symbol("x") + number(1.0) }.compile()
val res = expression("x" to 2.0)
assertEquals(-1.0, res)
}
@Test
fun testConstProductInvocation() {
val res = RealField.mstInField { symbol("x") * 2 }("x" to 2.0)

View File

@ -1,7 +1,10 @@
package scietifik.kmath.ast
import scientifik.kmath.ast.evaluate
import scientifik.kmath.asm.compile
import scientifik.kmath.asm.expression
import scientifik.kmath.ast.mstInField
import scientifik.kmath.ast.parseMath
import scientifik.kmath.expressions.invoke
import scientifik.kmath.operations.Complex
import scientifik.kmath.operations.ComplexField
import kotlin.test.Test
@ -9,9 +12,15 @@ import kotlin.test.assertEquals
class AsmTest {
@Test
fun parsedExpression() {
fun `compile MST`() {
val mst = "2+2*(2+2)".parseMath()
val res = ComplexField.evaluate(mst)
val res = ComplexField.expression(mst)()
assertEquals(Complex(10.0, 0.0), res)
}
@Test
fun `compile MSTExpression`() {
val res = ComplexField.mstInField { number(2) + number(2) * (number(2) + number(2)) }.compile()()
assertEquals(Complex(10.0, 0.0), res)
}
}

View File

@ -1,17 +1,25 @@
package scietifik.kmath.ast
import scientifik.kmath.ast.evaluate
import scientifik.kmath.ast.mstInField
import scientifik.kmath.ast.parseMath
import scientifik.kmath.expressions.invoke
import scientifik.kmath.operations.Complex
import scientifik.kmath.operations.ComplexField
import kotlin.test.assertEquals
import kotlin.test.Test
import kotlin.test.assertEquals
internal class ParserTest {
@Test
fun parsedExpression() {
fun `evaluate MST`() {
val mst = "2+2*(2+2)".parseMath()
val res = ComplexField.evaluate(mst)
assertEquals(Complex(10.0, 0.0), res)
}
@Test
fun `evaluate MSTExpression`() {
val res = ComplexField.mstInField { number(2) + number(2) * (number(2) + number(2)) }()
assertEquals(Complex(10.0, 0.0), res)
}
}