Dev #127

Merged
altavir merged 214 commits from dev into master 2020-08-11 08:33:21 +03:00
9 changed files with 57 additions and 6 deletions
Showing only changes of commit b57b55ec1a - Show all commits

4
kmath-ast/README.md Normal file
View File

@ -0,0 +1,4 @@
# AST based expression representation and operations
## Dynamic expression code generation
Contributed by [Iaroslav Postovalov](https://github.com/CommanderTvis).

View File

@ -11,7 +11,7 @@ sealed class MST {
/**
* A node containing unparsed string
*/
data class Singular(val value: String) : MST()
data class Symbolic(val value: String) : MST()
/**
* A node containing a number
@ -43,7 +43,7 @@ sealed class MST {
fun <T> NumericAlgebra<T>.evaluate(node: MST): T {
return when (node) {
is MST.Numeric -> number(node.value)
is MST.Singular -> symbol(node.value)
is MST.Symbolic -> symbol(node.value)
is MST.Unary -> unaryOperation(node.operation, evaluate(node.value))
is MST.Binary -> when {
node.left is MST.Numeric && node.right is MST.Numeric -> {

View File

@ -0,0 +1,47 @@
package scientifik.kmath.asm
import scientifik.kmath.ast.MST
import scientifik.kmath.ast.evaluate
import scientifik.kmath.expressions.Expression
import scientifik.kmath.operations.Field
import scientifik.kmath.operations.NumericAlgebra
import scientifik.kmath.operations.Ring
import scientifik.kmath.operations.Space
internal fun buildName(expression: AsmExpression<*>, collision: Int = 0): String {
val name = "scientifik.kmath.expressions.generated.AsmCompiledExpression_${expression.hashCode()}_$collision"
try {
Class.forName(name)
} catch (ignored: ClassNotFoundException) {
return name
}
return buildName(expression, collision + 1)
}
inline fun <reified T : Any, A : NumericAlgebra<T>, E : AsmExpressionAlgebra<T, A>> A.asm(
expressionAlgebra: E,
block: E.() -> AsmExpression<T>
): Expression<T> = expressionAlgebra.block()
inline fun <reified T : Any> NumericAlgebra<T>.asm(ast: MST): Expression<T> =
AsmExpressionAlgebra(T::class, this).evaluate(ast)
inline fun <reified T : Any, A> A.asmSpace(block: AsmExpressionSpace<T, A>.() -> AsmExpression<T>): Expression<T> where A : NumericAlgebra<T>, A : Space<T> =
AsmExpressionSpace<T, A>(T::class, this).block()
inline fun <reified T : Any, A> A.asmSpace(ast: MST): Expression<T> where A : NumericAlgebra<T>, A : Space<T> =
asmSpace { evaluate(ast) }
inline fun <reified T : Any, A> A.asmRing(block: AsmExpressionRing<T, A>.() -> AsmExpression<T>): Expression<T> where A : NumericAlgebra<T>, A : Ring<T> =
AsmExpressionRing(T::class, this).block()
inline fun <reified T : Any, A> A.asmRing(ast: MST): Expression<T> where A : NumericAlgebra<T>, A : Ring<T> =
asmRing { evaluate(ast) }
inline fun <reified T : Any, A> A.asmField(block: AsmExpressionField<T, A>.() -> AsmExpression<T>): Expression<T> where A : NumericAlgebra<T>, A : Field<T> =
AsmExpressionField(T::class, this).block()
inline fun <reified T : Any, A> A.asmField(ast: MST): Expression<T> where A : NumericAlgebra<T>, A : Field<T> =
asmRing { evaluate(ast) }

View File

@ -1,4 +1,4 @@
package scietifik.kmath.ast.asm
package scietifik.kmath.asm
import scientifik.kmath.asm.asmField
import scientifik.kmath.asm.asmRing

View File

@ -1,4 +1,4 @@
package scietifik.kmath.ast.asm
package scietifik.kmath.asm
import scientifik.kmath.asm.asmField
import scientifik.kmath.expressions.invoke

View File

@ -31,7 +31,7 @@ class ExpressionFieldTest {
@Test
fun separateContext() {
fun <T> FunctionalExpressionField<T>.expression(): Expression<T> {
fun <T> FunctionalExpressionField<T,*>.expression(): Expression<T> {
val x = variable("x")
return x * x + 2 * x + one
}
@ -42,7 +42,7 @@ class ExpressionFieldTest {
@Test
fun valueExpression() {
val expressionBuilder: FunctionalExpressionField<Double>.() -> Expression<Double> = {
val expressionBuilder: FunctionalExpressionField<Double,*>.() -> Expression<Double> = {
val x = variable("x")
x * x + 2 * x + one
}