Fix MSTAlgebra delegation

This commit is contained in:
Alexander Nozik 2020-06-16 10:27:54 +03:00
parent 96550922cd
commit 3d5036c982
2 changed files with 37 additions and 21 deletions

View File

@ -1,4 +1,3 @@
@file:Suppress("DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE")
package scientifik.kmath.ast package scientifik.kmath.ast
@ -7,9 +6,11 @@ import scientifik.kmath.operations.*
object MSTAlgebra : NumericAlgebra<MST> { object MSTAlgebra : NumericAlgebra<MST> {
override fun symbol(value: String): MST = MST.Symbolic(value) override fun symbol(value: String): MST = MST.Symbolic(value)
override fun unaryOperation(operation: String, arg: MST): MST = MST.Unary(operation, arg) override fun unaryOperation(operation: String, arg: MST): MST =
MST.Unary(operation, arg)
override fun binaryOperation(operation: String, left: MST, right: MST): MST = MST.Binary(operation, left, right) override fun binaryOperation(operation: String, left: MST, right: MST): MST =
MST.Binary(operation, left, right)
override fun number(value: Number): MST = MST.Numeric(value) override fun number(value: Number): MST = MST.Numeric(value)
} }
@ -17,17 +18,28 @@ object MSTAlgebra : NumericAlgebra<MST> {
object MSTSpace : Space<MST>, NumericAlgebra<MST> by MSTAlgebra { object MSTSpace : Space<MST>, NumericAlgebra<MST> by MSTAlgebra {
override val zero: MST = number(0.0) override val zero: MST = number(0.0)
override fun add(a: MST, b: MST): MST = binaryOperation(SpaceOperations.PLUS_OPERATION, a, b) override fun add(a: MST, b: MST): MST =
binaryOperation(SpaceOperations.PLUS_OPERATION, a, b)
override fun multiply(a: MST, k: Number): MST = binaryOperation(RingOperations.TIMES_OPERATION, a, number(k)) override fun multiply(a: MST, k: Number): MST =
binaryOperation(RingOperations.TIMES_OPERATION, a, number(k))
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 MSTRing : Ring<MST>, Space<MST> by MSTSpace { object MSTRing : Ring<MST>, Space<MST> by MSTSpace {
override val one: MST = number(1.0) override val one: MST = number(1.0)
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 =
MSTSpace.binaryOperation(operation, left, right)
} }
object MSTField : Field<MST>, Ring<MST> by MSTRing { object MSTField : Field<MST>, Ring<MST> by MSTRing {
override fun divide(a: MST, b: MST): MST = binaryOperation(FieldOperations.DIV_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 =
MSTRing.binaryOperation(operation, left, right)
} }

View File

@ -4,11 +4,10 @@ import scientifik.kmath.asm.internal.AsmBuilder
import scientifik.kmath.asm.internal.hasSpecific import scientifik.kmath.asm.internal.hasSpecific
import scientifik.kmath.asm.internal.tryInvokeSpecific import scientifik.kmath.asm.internal.tryInvokeSpecific
import scientifik.kmath.ast.MST import scientifik.kmath.ast.MST
import scientifik.kmath.ast.MSTField import scientifik.kmath.ast.MSTExpression
import scientifik.kmath.ast.MSTRing
import scientifik.kmath.ast.MSTSpace
import scientifik.kmath.expressions.Expression import scientifik.kmath.expressions.Expression
import scientifik.kmath.operations.* import scientifik.kmath.operations.Algebra
import scientifik.kmath.operations.NumericAlgebra
import kotlin.reflect.KClass import kotlin.reflect.KClass
@ -88,16 +87,21 @@ fun <T : Any> MST.compileWith(type: KClass<T>, algebra: Algebra<T>): Expression<
inline fun <reified T : Any> Algebra<T>.compile(mst: MST): Expression<T> = mst.compileWith(T::class, this) inline fun <reified T : Any> Algebra<T>.compile(mst: MST): Expression<T> = mst.compileWith(T::class, this)
inline fun <reified T : Any, A : Algebra<T>, E : Algebra<MST>> A.asm( /**
mstAlgebra: E, * Optimize performance of an [MSTExpression] using ASM codegen
block: E.() -> MST */
): Expression<T> = mstAlgebra.block().compileWith(T::class, this) inline fun <reified T : Any> MSTExpression<T>.compile(): Expression<T> = mst.compileWith(T::class, algebra)
inline fun <reified T : Any, A : Space<T>> A.asmInSpace(block: MSTSpace.() -> MST): Expression<T> = //inline fun <reified T : Any, A : Algebra<T>, E : Algebra<MST>> A.asm(
MSTSpace.block().compileWith(T::class, this) // mstAlgebra: E,
// block: E.() -> MST
inline fun <reified T : Any, A : Ring<T>> A.asmInRing(block: MSTRing.() -> MST): Expression<T> = //): Expression<T> = mstAlgebra.block().compileWith(T::class, this)
MSTRing.block().compileWith(T::class, this) //
//inline fun <reified T : Any, A : Space<T>> A.asmInSpace(block: MSTSpace.() -> MST): Expression<T> =
inline fun <reified T : Any, A : Field<T>> A.asmInField(block: MSTField.() -> MST): Expression<T> = // MSTSpace.block().compileWith(T::class, this)
MSTField.block().compileWith(T::class, this) //
//inline fun <reified T : Any, A : Ring<T>> A.asmInRing(block: MSTRing.() -> MST): Expression<T> =
// MSTRing.block().compileWith(T::class, this)
//
//inline fun <reified T : Any, A : Field<T>> A.asmInField(block: MSTField.() -> MST): Expression<T> =
// MSTField.block().compileWith(T::class, this)