forked from kscience/kmath
Separate ast module
This commit is contained in:
parent
66c5c392cb
commit
5e92d85c46
16
kmath-ast/build.gradle.kts
Normal file
16
kmath-ast/build.gradle.kts
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package scientifik.kmath.expressions
|
package scientifik.kmath.ast
|
||||||
|
|
||||||
import scientifik.kmath.operations.NumericAlgebra
|
import scientifik.kmath.operations.NumericAlgebra
|
||||||
import scientifik.kmath.operations.RealField
|
import scientifik.kmath.operations.RealField
|
23
kmath-ast/src/jvmMain/kotlin/scientifik/kmath/ast/asm.kt
Normal file
23
kmath-ast/src/jvmMain/kotlin/scientifik/kmath/ast/asm.kt
Normal 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) }
|
@ -2,7 +2,7 @@ package scientifik.kmath.commons.expressions
|
|||||||
|
|
||||||
import org.apache.commons.math3.analysis.differentiation.DerivativeStructure
|
import org.apache.commons.math3.analysis.differentiation.DerivativeStructure
|
||||||
import scientifik.kmath.expressions.Expression
|
import scientifik.kmath.expressions.Expression
|
||||||
import scientifik.kmath.expressions.ExpressionContext
|
import scientifik.kmath.expressions.ExpressionAlgebra
|
||||||
import scientifik.kmath.operations.ExtendedField
|
import scientifik.kmath.operations.ExtendedField
|
||||||
import scientifik.kmath.operations.Field
|
import scientifik.kmath.operations.Field
|
||||||
import kotlin.properties.ReadOnlyProperty
|
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])
|
* 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?) =
|
override fun variable(name: String, default: Double?) =
|
||||||
DiffExpression { variable(name, default?.const()) }
|
DiffExpression { variable(name, default?.const()) }
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ operator fun <T> Expression<T>.invoke(vararg pairs: Pair<String, T>): T = invoke
|
|||||||
/**
|
/**
|
||||||
* A context for expression construction
|
* A context for expression construction
|
||||||
*/
|
*/
|
||||||
interface ExpressionContext<T, E> : Algebra<E> {
|
interface ExpressionAlgebra<T, E> : Algebra<E> {
|
||||||
/**
|
/**
|
||||||
* Introduce a variable into expression context
|
* Introduce a variable into expression context
|
||||||
*/
|
*/
|
||||||
|
@ -21,8 +21,11 @@ internal class SumExpression<T>(
|
|||||||
override fun invoke(arguments: Map<String, T>): T = context.add(first.invoke(arguments), second.invoke(arguments))
|
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>) :
|
internal class ProductExpression<T>(
|
||||||
Expression<T> {
|
val context: Ring<T>,
|
||||||
|
val first: Expression<T>,
|
||||||
|
val second: Expression<T>
|
||||||
|
) : Expression<T> {
|
||||||
override fun invoke(arguments: Map<String, T>): T =
|
override fun invoke(arguments: Map<String, T>): T =
|
||||||
context.multiply(first.invoke(arguments), second.invoke(arguments))
|
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>(
|
open class FunctionalExpressionSpace<T>(
|
||||||
val space: Space<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)
|
override val zero: Expression<T> = ConstantExpression(space.zero)
|
||||||
|
|
||||||
@ -61,12 +64,12 @@ open class FunctionalExpressionSpace<T>(
|
|||||||
|
|
||||||
open class FunctionalExpressionField<T>(
|
open class FunctionalExpressionField<T>(
|
||||||
val field: Field<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>
|
override val one: Expression<T>
|
||||||
get() = const(this.field.one)
|
get() = const(this.field.one)
|
||||||
|
|
||||||
fun const(value: Double): Expression<T> = const(field.run { one*value})
|
fun const(value: Double): Expression<T> = const(field.run { one * value })
|
||||||
|
|
||||||
override fun multiply(a: Expression<T>, b: Expression<T>): Expression<T> = ProductExpression(field, a, b)
|
override fun multiply(a: Expression<T>, b: Expression<T>): Expression<T> = ProductExpression(field, a, b)
|
||||||
|
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package scientifik.kmath.operations
|
package scientifik.kmath.operations
|
||||||
|
|
||||||
import scientifik.kmath.structures.*
|
|
||||||
import java.math.BigDecimal
|
import java.math.BigDecimal
|
||||||
import java.math.BigInteger
|
import java.math.BigInteger
|
||||||
import java.math.MathContext
|
import java.math.MathContext
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A field wrapper for Java [BigInteger]
|
||||||
|
*/
|
||||||
object JBigIntegerField : Field<BigInteger> {
|
object JBigIntegerField : Field<BigInteger> {
|
||||||
override val zero: BigInteger = BigInteger.ZERO
|
override val zero: BigInteger = BigInteger.ZERO
|
||||||
override val one: BigInteger = BigInteger.ONE
|
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)
|
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> {
|
class JBigDecimalField(val mathContext: MathContext = MathContext.DECIMAL64) : Field<BigDecimal> {
|
||||||
override val zero: BigDecimal = BigDecimal.ZERO
|
override val zero: BigDecimal = BigDecimal.ZERO
|
||||||
override val one: BigDecimal = BigDecimal.ONE
|
override val one: BigDecimal = BigDecimal.ONE
|
||||||
|
@ -44,5 +44,6 @@ include(
|
|||||||
":kmath-dimensions",
|
":kmath-dimensions",
|
||||||
":kmath-for-real",
|
":kmath-for-real",
|
||||||
":kmath-geometry",
|
":kmath-geometry",
|
||||||
|
":kmath-ast",
|
||||||
":examples"
|
":examples"
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user