diff --git a/kmath-ast/build.gradle.kts b/kmath-ast/build.gradle.kts new file mode 100644 index 000000000..f3d37bf65 --- /dev/null +++ b/kmath-ast/build.gradle.kts @@ -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") + } + } +} \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/MathSyntaxTree.kt b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MathSyntaxTree.kt similarity index 98% rename from kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/MathSyntaxTree.kt rename to kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MathSyntaxTree.kt index fbc055f80..b43ef705d 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/MathSyntaxTree.kt +++ b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MathSyntaxTree.kt @@ -1,4 +1,4 @@ -package scientifik.kmath.expressions +package scientifik.kmath.ast import scientifik.kmath.operations.NumericAlgebra import scientifik.kmath.operations.RealField diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/ast/asm.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/ast/asm.kt new file mode 100644 index 000000000..6c4325859 --- /dev/null +++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/ast/asm.kt @@ -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) } \ No newline at end of file diff --git a/kmath-commons/src/main/kotlin/scientifik/kmath/commons/expressions/DiffExpression.kt b/kmath-commons/src/main/kotlin/scientifik/kmath/commons/expressions/DiffExpression.kt index 8c19395d3..f6f61e08a 100644 --- a/kmath-commons/src/main/kotlin/scientifik/kmath/commons/expressions/DiffExpression.kt +++ b/kmath-commons/src/main/kotlin/scientifik/kmath/commons/expressions/DiffExpression.kt @@ -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()) } diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/Expression.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/Expression.kt index aba7357e8..df5c981f5 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/Expression.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/Expression.kt @@ -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 */ diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/functionalExpressions.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/functionalExpressions.kt index 4f38e3a71..c3861d256 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/functionalExpressions.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/functionalExpressions.kt @@ -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,12 +64,12 @@ 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) - 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) diff --git a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/bigNumbers.kt b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/bigNumbers.kt index 76ca199c5..e6f09c040 100644 --- a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/bigNumbers.kt +++ b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/operations/bigNumbers.kt @@ -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 diff --git a/settings.gradle.kts b/settings.gradle.kts index 57173250b..465ecfca8 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -44,5 +44,6 @@ include( ":kmath-dimensions", ":kmath-for-real", ":kmath-geometry", + ":kmath-ast", ":examples" )