Fix MSTField and MSTRing invalid unary operation, update according ASM tests

This commit is contained in:
Iaroslav 2020-06-26 20:57:47 +07:00
parent 2df97ca4c3
commit 0ee1d31571
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
2 changed files with 29 additions and 31 deletions

View File

@ -34,43 +34,40 @@ object MSTSpace : Space<MST>, NumericAlgebra<MST> {
}
object MSTRing : Ring<MST>, NumericAlgebra<MST> {
override fun number(value: Number): MST = MST.Numeric(value)
override fun symbol(value: String): MST = MST.Symbolic(value)
override val zero: MST = MSTSpace.number(0.0)
override val one: MST = number(1.0)
override fun add(a: MST, b: MST): MST =
MSTAlgebra.binaryOperation(SpaceOperations.PLUS_OPERATION, a, b)
override fun number(value: Number): MST = MST.Numeric(value)
override fun symbol(value: String): MST = MST.Symbolic(value)
override fun add(a: MST, b: MST): MST = binaryOperation(SpaceOperations.PLUS_OPERATION, a, b)
override fun multiply(a: MST, k: Number): MST =
MSTAlgebra.binaryOperation(RingOperations.TIMES_OPERATION, a, MSTSpace.number(k))
binaryOperation(RingOperations.TIMES_OPERATION, a, MSTSpace.number(k))
override fun multiply(a: MST, b: MST): MST =
binaryOperation(RingOperations.TIMES_OPERATION, a, b)
override fun multiply(a: MST, b: MST): MST = binaryOperation(RingOperations.TIMES_OPERATION, a, b)
override fun binaryOperation(operation: String, left: MST, right: MST): MST =
MSTAlgebra.binaryOperation(operation, left, right)
override fun unaryOperation(operation: String, arg: MST): MST = MSTAlgebra.unaryOperation(operation, arg)
}
object MSTField : Field<MST>{
override fun symbol(value: String): MST = MST.Symbolic(value)
override fun number(value: Number): MST = MST.Numeric(value)
object MSTField : Field<MST> {
override val zero: MST = MSTSpace.number(0.0)
override val one: MST = number(1.0)
override fun add(a: MST, b: MST): MST =
MSTAlgebra.binaryOperation(SpaceOperations.PLUS_OPERATION, a, b)
override fun symbol(value: String): MST = MST.Symbolic(value)
override fun number(value: Number): MST = MST.Numeric(value)
override fun add(a: MST, b: MST): MST = binaryOperation(SpaceOperations.PLUS_OPERATION, a, b)
override fun multiply(a: MST, k: Number): MST =
MSTAlgebra.binaryOperation(RingOperations.TIMES_OPERATION, a, MSTSpace.number(k))
binaryOperation(RingOperations.TIMES_OPERATION, a, MSTSpace.number(k))
override fun multiply(a: MST, b: MST): MST =
binaryOperation(RingOperations.TIMES_OPERATION, a, b)
override fun divide(a: MST, b: MST): MST =
binaryOperation(FieldOperations.DIV_OPERATION, a, b)
override fun multiply(a: MST, b: MST): MST = binaryOperation(RingOperations.TIMES_OPERATION, a, b)
override fun divide(a: MST, b: MST): MST = binaryOperation(FieldOperations.DIV_OPERATION, a, b)
override fun binaryOperation(operation: String, left: MST, right: MST): MST =
MSTAlgebra.binaryOperation(operation, left, right)
override fun unaryOperation(operation: String, arg: MST): MST = MSTAlgebra.unaryOperation(operation, arg)
}

View File

@ -7,39 +7,40 @@ import scientifik.kmath.operations.RealField
import kotlin.test.Test
import kotlin.test.assertEquals
internal class TestSpecialization {
internal class TestAsmSpecialization {
@Test
fun testUnaryPlus() {
val expr = RealField.mstInField { unaryOperation("+", symbol("x")) }.compile()
val res = expr("x" to 2.0)
assertEquals(2.0, res)
assertEquals(2.0, expr("x" to 2.0))
}
@Test
fun testUnaryMinus() {
val expr = RealField.mstInField { unaryOperation("-", symbol("x")) }.compile()
val res = expr("x" to 2.0)
assertEquals(-2.0, res)
assertEquals(-2.0, expr("x" to 2.0))
}
@Test
fun testAdd() {
val expr = RealField.mstInField { binaryOperation("+", symbol("x"), symbol("x")) }.compile()
val res = expr("x" to 2.0)
assertEquals(4.0, res)
assertEquals(4.0, expr("x" to 2.0))
}
@Test
fun testSine() {
val expr = RealField.mstInField { unaryOperation("sin", symbol("x")) }.compile()
assertEquals(0.0, expr("x" to 0.0))
}
@Test
fun testMinus() {
val expr = RealField.mstInField { binaryOperation("-", symbol("x"), symbol("x")) }.compile()
val res = expr("x" to 2.0)
assertEquals(0.0, res)
assertEquals(0.0, expr("x" to 2.0))
}
@Test
fun testDivide() {
val expr = RealField.mstInField { binaryOperation("/", symbol("x"), symbol("x")) }.compile()
val res = expr("x" to 2.0)
assertEquals(1.0, res)
assertEquals(1.0, expr("x" to 2.0))
}
}