Separate ast module

This commit is contained in:
Alexander Nozik 2020-06-12 10:40:59 +03:00
parent 66c5c392cb
commit 5e92d85c46
8 changed files with 58 additions and 10 deletions

View File

@ -0,0 +1,16 @@
plugins {
id("scientifik.mpp")
}
repositories{
maven("https://dl.bintray.com/hotkeytlt/maven")
}
kotlin.sourceSets {
commonMain {
dependencies {
api(project(":kmath-core"))
implementation("com.github.h0tk3y.betterParse:better-parse-multiplatform:0.4.0-alpha3")
}
}
}

View File

@ -1,4 +1,4 @@
package scientifik.kmath.expressions
package scientifik.kmath.ast
import scientifik.kmath.operations.NumericAlgebra
import scientifik.kmath.operations.RealField

View File

@ -0,0 +1,23 @@
package scientifik.kmath.ast
import scientifik.kmath.expressions.Expression
import scientifik.kmath.operations.Algebra
import scientifik.kmath.operations.NumericAlgebra
//TODO stubs for asm generation
interface AsmExpression<T>
interface AsmExpressionAlgebra<T, A : Algebra<T>> : NumericAlgebra<AsmExpression<T>> {
val algebra: A
}
fun <T> AsmExpression<T>.compile(): Expression<T> = TODO()
//TODO add converter for functional expressions
inline fun <reified T : Any, A : Algebra<T>> A.asm(
block: AsmExpressionAlgebra<T, A>.() -> AsmExpression<T>
): Expression<T> = TODO()
inline fun <reified T : Any, A : Algebra<T>> A.asm(ast: MathSyntaxTree): Expression<T> = asm { compile(ast) }

View File

@ -2,7 +2,7 @@ package scientifik.kmath.commons.expressions
import org.apache.commons.math3.analysis.differentiation.DerivativeStructure
import scientifik.kmath.expressions.Expression
import scientifik.kmath.expressions.ExpressionContext
import scientifik.kmath.expressions.ExpressionAlgebra
import scientifik.kmath.operations.ExtendedField
import scientifik.kmath.operations.Field
import kotlin.properties.ReadOnlyProperty
@ -113,7 +113,7 @@ fun DiffExpression.derivative(name: String) = derivative(name to 1)
/**
* A context for [DiffExpression] (not to be confused with [DerivativeStructure])
*/
object DiffExpressionContext : ExpressionContext<Double, DiffExpression>, Field<DiffExpression> {
object DiffExpressionAlgebra : ExpressionAlgebra<Double, DiffExpression>, Field<DiffExpression> {
override fun variable(name: String, default: Double?) =
DiffExpression { variable(name, default?.const()) }

View File

@ -14,7 +14,7 @@ operator fun <T> Expression<T>.invoke(vararg pairs: Pair<String, T>): T = invoke
/**
* A context for expression construction
*/
interface ExpressionContext<T, E> : Algebra<E> {
interface ExpressionAlgebra<T, E> : Algebra<E> {
/**
* Introduce a variable into expression context
*/

View File

@ -21,8 +21,11 @@ internal class SumExpression<T>(
override fun invoke(arguments: Map<String, T>): T = context.add(first.invoke(arguments), second.invoke(arguments))
}
internal class ProductExpression<T>(val context: Ring<T>, val first: Expression<T>, val second: Expression<T>) :
Expression<T> {
internal class ProductExpression<T>(
val context: Ring<T>,
val first: Expression<T>,
val second: Expression<T>
) : Expression<T> {
override fun invoke(arguments: Map<String, T>): T =
context.multiply(first.invoke(arguments), second.invoke(arguments))
}
@ -39,7 +42,7 @@ internal class DivExpession<T>(val context: Field<T>, val expr: Expression<T>, v
open class FunctionalExpressionSpace<T>(
val space: Space<T>
) : Space<Expression<T>>, ExpressionContext<T,Expression<T>> {
) : Space<Expression<T>>, ExpressionAlgebra<T, Expression<T>> {
override val zero: Expression<T> = ConstantExpression(space.zero)
@ -61,7 +64,7 @@ open class FunctionalExpressionSpace<T>(
open class FunctionalExpressionField<T>(
val field: Field<T>
) : Field<Expression<T>>, ExpressionContext<T,Expression<T>>, FunctionalExpressionSpace<T>(field) {
) : Field<Expression<T>>, ExpressionAlgebra<T, Expression<T>>, FunctionalExpressionSpace<T>(field) {
override val one: Expression<T>
get() = const(this.field.one)

View File

@ -1,10 +1,12 @@
package scientifik.kmath.operations
import scientifik.kmath.structures.*
import java.math.BigDecimal
import java.math.BigInteger
import java.math.MathContext
/**
* A field wrapper for Java [BigInteger]
*/
object JBigIntegerField : Field<BigInteger> {
override val zero: BigInteger = BigInteger.ZERO
override val one: BigInteger = BigInteger.ONE
@ -18,6 +20,9 @@ object JBigIntegerField : Field<BigInteger> {
override fun divide(a: BigInteger, b: BigInteger): BigInteger = a.div(b)
}
/**
* A Field wrapper for Java [BigDecimal]
*/
class JBigDecimalField(val mathContext: MathContext = MathContext.DECIMAL64) : Field<BigDecimal> {
override val zero: BigDecimal = BigDecimal.ZERO
override val one: BigDecimal = BigDecimal.ONE

View File

@ -44,5 +44,6 @@ include(
":kmath-dimensions",
":kmath-for-real",
":kmath-geometry",
":kmath-ast",
":examples"
)