From f0fbebd770f186a6a2031923cc17688abbb9e4a2 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 12 Oct 2020 20:26:03 +0700 Subject: [PATCH 01/19] Add adapters of scalar functions to MST and vice versa --- build.gradle.kts | 1 + kmath-ast-kotlingrad/build.gradle.kts | 8 +++ .../kmath/ast/kotlingrad/ScalarsAdapters.kt | 64 +++++++++++++++++++ kmath-prob/build.gradle.kts | 4 +- kmath-viktor/build.gradle.kts | 4 +- settings.gradle.kts | 6 +- 6 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 kmath-ast-kotlingrad/build.gradle.kts create mode 100644 kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt diff --git a/build.gradle.kts b/build.gradle.kts index 05e2d5979..0da2212a7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,6 +12,7 @@ allprojects { maven("https://dl.bintray.com/kotlin/kotlin-eap") maven("https://dl.bintray.com/kotlin/kotlinx") maven("https://dl.bintray.com/hotkeytlt/maven") + maven("https://jitpack.io") } group = "kscience.kmath" diff --git a/kmath-ast-kotlingrad/build.gradle.kts b/kmath-ast-kotlingrad/build.gradle.kts new file mode 100644 index 000000000..0fe6e6b93 --- /dev/null +++ b/kmath-ast-kotlingrad/build.gradle.kts @@ -0,0 +1,8 @@ +plugins { + id("ru.mipt.npm.jvm") +} + +dependencies { + api("com.github.breandan:kotlingrad:0.3.2") + api(project(":kmath-ast")) +} diff --git a/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt b/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt new file mode 100644 index 000000000..70c8c09f8 --- /dev/null +++ b/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt @@ -0,0 +1,64 @@ +package kscience.kmath.ast.kotlingrad + +import edu.umontreal.kotlingrad.experimental.* +import kscience.kmath.ast.MST +import kscience.kmath.ast.MstExtendedField +import kscience.kmath.operations.* + +/** + * Maps [SFun] objects to [MST]. Some unsupported operations like [Derivative] are bound and converted then. + * + * @receiver a scalar function. + * @return the [MST]. + */ +public fun > SFun.mst(): MST = MstExtendedField { + when (this@mst) { + is SVar -> symbol(name) + is SConst -> number(doubleValue) + is Sum -> left.mst() + right.mst() + is Prod -> left.mst() * right.mst() + is Power -> power(left.mst(), (right() as SConst<*>).doubleValue) + is Negative -> -input.mst() + is Log -> ln(left.mst()) / ln(right.mst()) + is Sine -> sin(input.mst()) + is Cosine -> cos(input.mst()) + is Tangent -> tan(input.mst()) + is DProd -> this@mst().mst() + is SComposition -> this@mst().mst() + is VSumAll -> this@mst().mst() + is Derivative -> this@mst().mst() + } +} + +/** + * Maps [MST] objects to [SFun]. Unsupported operations throw [IllegalStateException]. + * + * @receiver an [MST]. + * @return the scalar function. + */ +public fun > MST.sfun(proto: X): SFun { + return when (this) { + is MST.Numeric -> SConst(value) + is MST.Symbolic -> SVar(proto, value) + + is MST.Unary -> when (operation) { + SpaceOperations.PLUS_OPERATION -> value.sfun(proto) + SpaceOperations.MINUS_OPERATION -> Negative(value.sfun(proto)) + TrigonometricOperations.SIN_OPERATION -> Sine(value.sfun(proto)) + TrigonometricOperations.COS_OPERATION -> Cosine(value.sfun(proto)) + TrigonometricOperations.TAN_OPERATION -> Tangent(value.sfun(proto)) + PowerOperations.SQRT_OPERATION -> Power(value.sfun(proto), SConst(0.5)) + ExponentialOperations.EXP_OPERATION -> Power(value.sfun(proto), E()) + ExponentialOperations.LN_OPERATION -> Log(value.sfun(proto)) + else -> error("Unary operation $operation not defined in $this") + } + + is MST.Binary -> when (operation) { + SpaceOperations.PLUS_OPERATION -> Sum(left.sfun(proto), right.sfun(proto)) + SpaceOperations.MINUS_OPERATION -> Sum(left.sfun(proto), Negative(right.sfun(proto))) + RingOperations.TIMES_OPERATION -> Prod(left.sfun(proto), right.sfun(proto)) + FieldOperations.DIV_OPERATION -> Prod(left.sfun(proto), Power(right.sfun(proto), Negative(One()))) + else -> error("Binary operation $operation not defined in $this") + } + } +} diff --git a/kmath-prob/build.gradle.kts b/kmath-prob/build.gradle.kts index 4c9663e5f..186aff944 100644 --- a/kmath-prob/build.gradle.kts +++ b/kmath-prob/build.gradle.kts @@ -1,4 +1,6 @@ -plugins { id("ru.mipt.npm.mpp") } +plugins { + id("ru.mipt.npm.mpp") +} kotlin.sourceSets { commonMain { diff --git a/kmath-viktor/build.gradle.kts b/kmath-viktor/build.gradle.kts index 6fe8ad878..3e5c5912c 100644 --- a/kmath-viktor/build.gradle.kts +++ b/kmath-viktor/build.gradle.kts @@ -1,4 +1,6 @@ -plugins { id("ru.mipt.npm.jvm") } +plugins { + id("ru.mipt.npm.jvm") +} description = "Binding for https://github.com/JetBrains-Research/viktor" diff --git a/settings.gradle.kts b/settings.gradle.kts index 7ece3f25c..5fd072e1a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -7,7 +7,6 @@ pluginManagement { maven("https://dl.bintray.com/mipt-npm/kscience") maven("https://dl.bintray.com/mipt-npm/dev") maven("https://dl.bintray.com/kotlin/kotlinx") - maven("https://dl.bintray.com/kotlin/kotlin-dev/") } val toolsVersion = "0.6.1-dev-1.4.20-M1" @@ -25,11 +24,11 @@ pluginManagement { } rootProject.name = "kmath" + include( ":kmath-memory", ":kmath-core", ":kmath-functions", -// ":kmath-io", ":kmath-coroutines", ":kmath-histograms", ":kmath-commons", @@ -40,5 +39,6 @@ include( ":kmath-geometry", ":kmath-ast", ":examples", - ":kmath-ejml" + ":kmath-ejml", + ":kmath-ast-kotlingrad" ) From 84f7535fdd7d59103e2c846eb0413bb2bb482cb7 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 12 Oct 2020 20:36:05 +0700 Subject: [PATCH 02/19] Add pow support --- .../main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt b/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt index 70c8c09f8..741a2534c 100644 --- a/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt +++ b/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt @@ -58,6 +58,7 @@ public fun > MST.sfun(proto: X): SFun { SpaceOperations.MINUS_OPERATION -> Sum(left.sfun(proto), Negative(right.sfun(proto))) RingOperations.TIMES_OPERATION -> Prod(left.sfun(proto), right.sfun(proto)) FieldOperations.DIV_OPERATION -> Prod(left.sfun(proto), Power(right.sfun(proto), Negative(One()))) + PowerOperations.POW_OPERATION -> Power(left.sfun(proto), SConst((right as MST.Numeric).value)) else -> error("Binary operation $operation not defined in $this") } } From 5de9d69237118889b74d4a5074b48db609db92ae Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 12 Oct 2020 21:06:15 +0700 Subject: [PATCH 03/19] Add more fine-grained converters from MST to SVar and SConst --- .../kmath/ast/kotlingrad/ScalarsAdapters.kt | 70 ++++++++++++------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt b/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt index 741a2534c..3a1191e6a 100644 --- a/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt +++ b/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt @@ -8,8 +8,8 @@ import kscience.kmath.operations.* /** * Maps [SFun] objects to [MST]. Some unsupported operations like [Derivative] are bound and converted then. * - * @receiver a scalar function. - * @return the [MST]. + * @receiver the scalar function. + * @return a node. */ public fun > SFun.mst(): MST = MstExtendedField { when (this@mst) { @@ -30,36 +30,52 @@ public fun > SFun.mst(): MST = MstExtendedField { } } +/** + * Maps [MST.Numeric] to [SConst] directly. + * + * @receiver the node. + * @return a new constant. + */ +public fun > MST.Numeric.sconst(): SConst = SConst(value) + +/** + * Maps [MST.Symbolic] to [SVar] directly. + * + * @receiver the node. + * @param proto the prototype instance. + * @return a new variable. + */ +public fun > MST.Symbolic.svar(proto: X): SVar = SVar(proto, value) + /** * Maps [MST] objects to [SFun]. Unsupported operations throw [IllegalStateException]. * - * @receiver an [MST]. - * @return the scalar function. + * @receiver the node. + * @param proto the prototype instance. + * @return a scalar function. */ -public fun > MST.sfun(proto: X): SFun { - return when (this) { - is MST.Numeric -> SConst(value) - is MST.Symbolic -> SVar(proto, value) +public fun > MST.sfun(proto: X): SFun = when (this) { + is MST.Numeric -> sconst() + is MST.Symbolic -> svar(proto) - is MST.Unary -> when (operation) { - SpaceOperations.PLUS_OPERATION -> value.sfun(proto) - SpaceOperations.MINUS_OPERATION -> Negative(value.sfun(proto)) - TrigonometricOperations.SIN_OPERATION -> Sine(value.sfun(proto)) - TrigonometricOperations.COS_OPERATION -> Cosine(value.sfun(proto)) - TrigonometricOperations.TAN_OPERATION -> Tangent(value.sfun(proto)) - PowerOperations.SQRT_OPERATION -> Power(value.sfun(proto), SConst(0.5)) - ExponentialOperations.EXP_OPERATION -> Power(value.sfun(proto), E()) - ExponentialOperations.LN_OPERATION -> Log(value.sfun(proto)) - else -> error("Unary operation $operation not defined in $this") - } + is MST.Unary -> when (operation) { + SpaceOperations.PLUS_OPERATION -> value.sfun(proto) + SpaceOperations.MINUS_OPERATION -> Negative(value.sfun(proto)) + TrigonometricOperations.SIN_OPERATION -> Sine(value.sfun(proto)) + TrigonometricOperations.COS_OPERATION -> Cosine(value.sfun(proto)) + TrigonometricOperations.TAN_OPERATION -> Tangent(value.sfun(proto)) + PowerOperations.SQRT_OPERATION -> Power(value.sfun(proto), SConst(0.5)) + ExponentialOperations.EXP_OPERATION -> Power(value.sfun(proto), E()) + ExponentialOperations.LN_OPERATION -> Log(value.sfun(proto)) + else -> error("Unary operation $operation not defined in $this") + } - is MST.Binary -> when (operation) { - SpaceOperations.PLUS_OPERATION -> Sum(left.sfun(proto), right.sfun(proto)) - SpaceOperations.MINUS_OPERATION -> Sum(left.sfun(proto), Negative(right.sfun(proto))) - RingOperations.TIMES_OPERATION -> Prod(left.sfun(proto), right.sfun(proto)) - FieldOperations.DIV_OPERATION -> Prod(left.sfun(proto), Power(right.sfun(proto), Negative(One()))) - PowerOperations.POW_OPERATION -> Power(left.sfun(proto), SConst((right as MST.Numeric).value)) - else -> error("Binary operation $operation not defined in $this") - } + is MST.Binary -> when (operation) { + SpaceOperations.PLUS_OPERATION -> Sum(left.sfun(proto), right.sfun(proto)) + SpaceOperations.MINUS_OPERATION -> Sum(left.sfun(proto), Negative(right.sfun(proto))) + RingOperations.TIMES_OPERATION -> Prod(left.sfun(proto), right.sfun(proto)) + FieldOperations.DIV_OPERATION -> Prod(left.sfun(proto), Power(right.sfun(proto), Negative(One()))) + PowerOperations.POW_OPERATION -> Power(left.sfun(proto), SConst((right as MST.Numeric).value)) + else -> error("Binary operation $operation not defined in $this") } } From 31c71e0fad0fa8a0c8837cca44e10ea12a9fdf21 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 12 Oct 2020 21:21:08 +0700 Subject: [PATCH 04/19] Add comments on mapping of MST-to-SFun converting --- .../kmath/ast/kotlingrad/ScalarsAdapters.kt | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt b/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt index 3a1191e6a..cfd1f2702 100644 --- a/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt +++ b/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt @@ -3,10 +3,29 @@ package kscience.kmath.ast.kotlingrad import edu.umontreal.kotlingrad.experimental.* import kscience.kmath.ast.MST import kscience.kmath.ast.MstExtendedField +import kscience.kmath.ast.MstExtendedField.unaryMinus import kscience.kmath.operations.* /** * Maps [SFun] objects to [MST]. Some unsupported operations like [Derivative] are bound and converted then. + * [Power] operation is limited to constant right-hand side arguments. + * + * Detailed mapping is: + * + * - [SVar] -> [MstExtendedField.symbol]; + * - [SConst] -> [MstExtendedField.number]; + * - [Sum] -> [MstExtendedField.add]; + * - [Prod] -> [MstExtendedField.multiply]; + * - [Power] -> [MstExtendedField.power] (limited); + * - [Negative] -> [MstExtendedField.unaryMinus]; + * - [Log] -> [MstExtendedField.ln] (left) / [MstExtendedField.ln] (right); + * - [Sine] -> [MstExtendedField.sin]; + * - [Cosine] -> [MstExtendedField.cos]; + * - [Tangent] -> [MstExtendedField.tan]; + * - [DProd] is vector operation, and it is requested to be evaluated; + * - [SComposition] is also requested to be evaluated eagerly; + * - [VSumAll] is requested to be evaluated; + * - [Derivative] is requested to be evaluated. * * @receiver the scalar function. * @return a node. @@ -50,6 +69,13 @@ public fun > MST.Symbolic.svar(proto: X): SVar = SVar(proto, valu /** * Maps [MST] objects to [SFun]. Unsupported operations throw [IllegalStateException]. * + * Detailed mapping is: + * + * - [MST.Numeric] -> [SConst]; + * - [MST.Symbolic] -> [SVar]; + * - [MST.Unary] -> [Negative], [Sine], [Cosine], [Tangent], [Power], [Log]; + * - [MST.Binary] -> [Sum], [Prod], [Power]. + * * @receiver the node. * @param proto the prototype instance. * @return a scalar function. From 57bdee49368208c67eea5259b39223f37fcae386 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 12 Oct 2020 22:34:05 +0700 Subject: [PATCH 05/19] Add test, update MstAlgebra a bit to return concrete types --- .../kmath/ast/kotlingrad/ScalarsAdapters.kt | 2 +- .../kmath/ast/kotlingrad/AdaptingTests.kt | 66 ++++++++++ .../kotlin/kscience/kmath/ast/MstAlgebra.kt | 113 +++++++++--------- 3 files changed, 125 insertions(+), 56 deletions(-) create mode 100644 kmath-ast-kotlingrad/src/test/kotlin/kscience/kmath/ast/kotlingrad/AdaptingTests.kt diff --git a/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt b/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt index cfd1f2702..16c96646a 100644 --- a/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt +++ b/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt @@ -36,7 +36,7 @@ public fun > SFun.mst(): MST = MstExtendedField { is SConst -> number(doubleValue) is Sum -> left.mst() + right.mst() is Prod -> left.mst() * right.mst() - is Power -> power(left.mst(), (right() as SConst<*>).doubleValue) + is Power -> power(left.mst(), (right as SConst<*>).doubleValue) is Negative -> -input.mst() is Log -> ln(left.mst()) / ln(right.mst()) is Sine -> sin(input.mst()) diff --git a/kmath-ast-kotlingrad/src/test/kotlin/kscience/kmath/ast/kotlingrad/AdaptingTests.kt b/kmath-ast-kotlingrad/src/test/kotlin/kscience/kmath/ast/kotlingrad/AdaptingTests.kt new file mode 100644 index 000000000..94d25e411 --- /dev/null +++ b/kmath-ast-kotlingrad/src/test/kotlin/kscience/kmath/ast/kotlingrad/AdaptingTests.kt @@ -0,0 +1,66 @@ +package kscience.kmath.ast.kotlingrad + +import edu.umontreal.kotlingrad.experimental.* +import kscience.kmath.asm.compile +import kscience.kmath.ast.MstAlgebra +import kscience.kmath.ast.MstExpression +import kscience.kmath.ast.parseMath +import kscience.kmath.expressions.invoke +import kscience.kmath.operations.RealField +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.test.fail + +internal class AdaptingTests { + private val proto: DReal = DoublePrecision.prototype + + @Test + fun symbol() { + val c1 = MstAlgebra.symbol("x") + assertTrue(c1.svar(proto).name == "x") + val c2 = "kitten".parseMath().sfun(proto) + if (c2 is SVar) assertTrue(c2.name == "kitten") else fail() + } + + @Test + fun number() { + val c1 = MstAlgebra.number(12354324) + assertTrue(c1.sconst().doubleValue == 12354324.0) + val c2 = "0.234".parseMath().sfun(proto) + if (c2 is SConst) assertTrue(c2.doubleValue == 0.234) else fail() + val c3 = "1e-3".parseMath().sfun(proto) + if (c3 is SConst) assertEquals(0.001, c3.value) else fail() + } + + @Test + fun simpleFunctionShape() { + val linear = "2*x+16".parseMath().sfun(proto) + if (linear !is Sum) fail() + if (linear.left !is Prod) fail() + if (linear.right !is SConst) fail() + } + + @Test + fun simpleFunctionDerivative() { + val x = MstAlgebra.symbol("x").svar(proto) + val quadratic = "x^2-4*x-44".parseMath().sfun(proto) + val actualDerivative = MstExpression(RealField, quadratic.d(x).mst()).compile() + val expectedDerivative = MstExpression(RealField, "2*x-4".parseMath()).compile() + assertEquals(actualDerivative("x" to 123.0), expectedDerivative("x" to 123.0)) + } + + @Test + fun moreComplexDerivative() { + val x = MstAlgebra.symbol("x").svar(proto) + val composition = "-sqrt(sin(x^2)-cos(x)^2-16*x)".parseMath().sfun(proto) + val actualDerivative = MstExpression(RealField, composition.d(x).mst()).compile() + + val expectedDerivative = MstExpression( + RealField, + "-(2*x*cos(x^2)+2*sin(x)*cos(x)-16)/(2*sqrt(sin(x^2)-16*x-cos(x)^2))".parseMath() + ).compile() + + assertEquals(actualDerivative("x" to 0.1), expectedDerivative("x" to 0.1)) + } +} diff --git a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstAlgebra.kt b/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstAlgebra.kt index 64a820b20..6ee6ab9af 100644 --- a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstAlgebra.kt +++ b/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstAlgebra.kt @@ -6,14 +6,14 @@ import kscience.kmath.operations.* * [Algebra] over [MST] nodes. */ public object MstAlgebra : NumericAlgebra { - override fun number(value: Number): MST = MST.Numeric(value) + override fun number(value: Number): MST.Numeric = MST.Numeric(value) - override fun symbol(value: String): MST = MST.Symbolic(value) + override fun symbol(value: String): MST.Symbolic = MST.Symbolic(value) - override fun unaryOperation(operation: String, arg: MST): MST = + override fun unaryOperation(operation: String, arg: MST): MST.Unary = MST.Unary(operation, arg) - override fun binaryOperation(operation: String, left: MST, right: MST): MST = + override fun binaryOperation(operation: String, left: MST, right: MST): MST.Binary = MST.Binary(operation, left, right) } @@ -21,97 +21,100 @@ public object MstAlgebra : NumericAlgebra { * [Space] over [MST] nodes. */ public object MstSpace : Space, NumericAlgebra { - override val zero: MST = number(0.0) + override val zero: MST.Numeric by lazy { number(0.0) } - override fun number(value: Number): MST = MstAlgebra.number(value) - override fun symbol(value: String): MST = MstAlgebra.symbol(value) - 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 number(value: Number): MST.Numeric = MstAlgebra.number(value) + override fun symbol(value: String): MST.Symbolic = MstAlgebra.symbol(value) + override fun add(a: MST, b: MST): MST.Binary = binaryOperation(SpaceOperations.PLUS_OPERATION, a, b) + override fun multiply(a: MST, k: Number): MST.Binary = binaryOperation(RingOperations.TIMES_OPERATION, a, number(k)) - override fun binaryOperation(operation: String, left: MST, right: MST): MST = + override fun binaryOperation(operation: String, left: MST, right: MST): MST.Binary = MstAlgebra.binaryOperation(operation, left, right) - override fun unaryOperation(operation: String, arg: MST): MST = MstAlgebra.unaryOperation(operation, arg) + override fun unaryOperation(operation: String, arg: MST): MST.Unary = MstAlgebra.unaryOperation(operation, arg) } /** * [Ring] over [MST] nodes. */ public object MstRing : Ring, NumericAlgebra { - override val zero: MST + override val zero: MST.Numeric get() = MstSpace.zero - override val one: MST = number(1.0) - override fun number(value: Number): MST = MstSpace.number(value) - override fun symbol(value: String): MST = MstSpace.symbol(value) - override fun add(a: MST, b: MST): MST = MstSpace.add(a, b) + override val one: MST.Numeric by lazy { number(1.0) } - override fun multiply(a: MST, k: Number): MST = MstSpace.multiply(a, k) + override fun number(value: Number): MST.Numeric = MstSpace.number(value) + override fun symbol(value: String): MST.Symbolic = MstSpace.symbol(value) + override fun add(a: MST, b: MST): MST.Binary = MstSpace.add(a, b) + override fun multiply(a: MST, k: Number): MST.Binary = MstSpace.multiply(a, k) + override fun multiply(a: MST, b: MST): MST.Binary = 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 = + override fun binaryOperation(operation: String, left: MST, right: MST): MST.Binary = MstSpace.binaryOperation(operation, left, right) - override fun unaryOperation(operation: String, arg: MST): MST = MstAlgebra.unaryOperation(operation, arg) + override fun unaryOperation(operation: String, arg: MST): MST.Unary = MstSpace.unaryOperation(operation, arg) } /** * [Field] over [MST] nodes. */ public object MstField : Field { - public override val zero: MST + public override val zero: MST.Numeric get() = MstRing.zero - public override val one: MST + public override val one: MST.Numeric get() = MstRing.one - public override fun symbol(value: String): MST = MstRing.symbol(value) - public override fun number(value: Number): MST = MstRing.number(value) - public override fun add(a: MST, b: MST): MST = MstRing.add(a, b) - public override fun multiply(a: MST, k: Number): MST = MstRing.multiply(a, k) - public override fun multiply(a: MST, b: MST): MST = MstRing.multiply(a, b) - public override fun divide(a: MST, b: MST): MST = binaryOperation(FieldOperations.DIV_OPERATION, a, b) + public override fun symbol(value: String): MST.Symbolic = MstRing.symbol(value) + public override fun number(value: Number): MST.Numeric = MstRing.number(value) + public override fun add(a: MST, b: MST): MST.Binary = MstRing.add(a, b) + public override fun multiply(a: MST, k: Number): MST.Binary = MstRing.multiply(a, k) + public override fun multiply(a: MST, b: MST): MST.Binary = MstRing.multiply(a, b) + public override fun divide(a: MST, b: MST): MST.Binary = binaryOperation(FieldOperations.DIV_OPERATION, a, b) - public override fun binaryOperation(operation: String, left: MST, right: MST): MST = + public override fun binaryOperation(operation: String, left: MST, right: MST): MST.Binary = MstRing.binaryOperation(operation, left, right) - override fun unaryOperation(operation: String, arg: MST): MST = MstRing.unaryOperation(operation, arg) + override fun unaryOperation(operation: String, arg: MST): MST.Unary = MstRing.unaryOperation(operation, arg) } /** * [ExtendedField] over [MST] nodes. */ public object MstExtendedField : ExtendedField { - override val zero: MST + override val zero: MST.Numeric get() = MstField.zero - override val one: MST + override val one: MST.Numeric get() = MstField.one - override fun symbol(value: String): MST = MstField.symbol(value) - override fun sin(arg: MST): MST = unaryOperation(TrigonometricOperations.SIN_OPERATION, arg) - override fun cos(arg: MST): MST = unaryOperation(TrigonometricOperations.COS_OPERATION, arg) - override fun tan(arg: MST): MST = unaryOperation(TrigonometricOperations.TAN_OPERATION, arg) - override fun asin(arg: MST): MST = unaryOperation(TrigonometricOperations.ASIN_OPERATION, arg) - override fun acos(arg: MST): MST = unaryOperation(TrigonometricOperations.ACOS_OPERATION, arg) - override fun atan(arg: MST): MST = unaryOperation(TrigonometricOperations.ATAN_OPERATION, arg) - override fun sinh(arg: MST): MST = unaryOperation(HyperbolicOperations.SINH_OPERATION, arg) - override fun cosh(arg: MST): MST = unaryOperation(HyperbolicOperations.COSH_OPERATION, arg) - override fun tanh(arg: MST): MST = unaryOperation(HyperbolicOperations.TANH_OPERATION, arg) - override fun asinh(arg: MST): MST = unaryOperation(HyperbolicOperations.ASINH_OPERATION, arg) - override fun acosh(arg: MST): MST = unaryOperation(HyperbolicOperations.ACOSH_OPERATION, arg) - override fun atanh(arg: MST): MST = unaryOperation(HyperbolicOperations.ATANH_OPERATION, arg) - override fun add(a: MST, b: MST): MST = MstField.add(a, b) - override fun multiply(a: MST, k: Number): MST = MstField.multiply(a, k) - override fun multiply(a: MST, b: MST): MST = MstField.multiply(a, b) - override fun divide(a: MST, b: MST): MST = MstField.divide(a, b) - override fun power(arg: MST, pow: Number): MST = binaryOperation(PowerOperations.POW_OPERATION, arg, number(pow)) - override fun exp(arg: MST): MST = unaryOperation(ExponentialOperations.EXP_OPERATION, arg) - override fun ln(arg: MST): MST = unaryOperation(ExponentialOperations.LN_OPERATION, arg) + override fun symbol(value: String): MST.Symbolic = MstField.symbol(value) + override fun number(value: Number): MST.Numeric = MstField.number(value) + override fun sin(arg: MST): MST.Unary = unaryOperation(TrigonometricOperations.SIN_OPERATION, arg) + override fun cos(arg: MST): MST.Unary = unaryOperation(TrigonometricOperations.COS_OPERATION, arg) + override fun tan(arg: MST): MST.Unary = unaryOperation(TrigonometricOperations.TAN_OPERATION, arg) + override fun asin(arg: MST): MST.Unary = unaryOperation(TrigonometricOperations.ASIN_OPERATION, arg) + override fun acos(arg: MST): MST.Unary = unaryOperation(TrigonometricOperations.ACOS_OPERATION, arg) + override fun atan(arg: MST): MST.Unary = unaryOperation(TrigonometricOperations.ATAN_OPERATION, arg) + override fun sinh(arg: MST): MST.Unary = unaryOperation(HyperbolicOperations.SINH_OPERATION, arg) + override fun cosh(arg: MST): MST.Unary = unaryOperation(HyperbolicOperations.COSH_OPERATION, arg) + override fun tanh(arg: MST): MST.Unary = unaryOperation(HyperbolicOperations.TANH_OPERATION, arg) + override fun asinh(arg: MST): MST.Unary = unaryOperation(HyperbolicOperations.ASINH_OPERATION, arg) + override fun acosh(arg: MST): MST.Unary = unaryOperation(HyperbolicOperations.ACOSH_OPERATION, arg) + override fun atanh(arg: MST): MST.Unary = unaryOperation(HyperbolicOperations.ATANH_OPERATION, arg) + override fun add(a: MST, b: MST): MST.Binary = MstField.add(a, b) + override fun multiply(a: MST, k: Number): MST.Binary = MstField.multiply(a, k) + override fun multiply(a: MST, b: MST): MST.Binary = MstField.multiply(a, b) + override fun divide(a: MST, b: MST): MST.Binary = MstField.divide(a, b) - override fun binaryOperation(operation: String, left: MST, right: MST): MST = + override fun power(arg: MST, pow: Number): MST.Binary = + binaryOperation(PowerOperations.POW_OPERATION, arg, number(pow)) + + override fun exp(arg: MST): MST.Unary = unaryOperation(ExponentialOperations.EXP_OPERATION, arg) + override fun ln(arg: MST): MST.Unary = unaryOperation(ExponentialOperations.LN_OPERATION, arg) + + override fun binaryOperation(operation: String, left: MST, right: MST): MST.Binary = MstField.binaryOperation(operation, left, right) - override fun unaryOperation(operation: String, arg: MST): MST = MstField.unaryOperation(operation, arg) + override fun unaryOperation(operation: String, arg: MST): MST.Unary = MstField.unaryOperation(operation, arg) } From 54069fd37ebc77dc8d40a2d46c3bc290583f23cc Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 12 Oct 2020 22:42:34 +0700 Subject: [PATCH 06/19] Add example of new AST API --- examples/build.gradle.kts | 3 +- .../ast/ExpressionsInterpretersBenchmark.kt | 148 ++++++++++-------- .../kscience/kmath/ast/KotlingradSupport.kt | 22 +++ 3 files changed, 103 insertions(+), 70 deletions(-) create mode 100644 examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 900da966b..968b372c3 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -19,7 +19,8 @@ repositories { sourceSets.register("benchmarks") dependencies { -// implementation(project(":kmath-ast")) + implementation(project(":kmath-ast")) + implementation(project(":kmath-ast-kotlingrad")) implementation(project(":kmath-core")) implementation(project(":kmath-coroutines")) implementation(project(":kmath-commons")) diff --git a/examples/src/main/kotlin/kscience/kmath/ast/ExpressionsInterpretersBenchmark.kt b/examples/src/main/kotlin/kscience/kmath/ast/ExpressionsInterpretersBenchmark.kt index f0a32e5bd..b25a61e96 100644 --- a/examples/src/main/kotlin/kscience/kmath/ast/ExpressionsInterpretersBenchmark.kt +++ b/examples/src/main/kotlin/kscience/kmath/ast/ExpressionsInterpretersBenchmark.kt @@ -1,70 +1,80 @@ package kscience.kmath.ast -// -//import kscience.kmath.asm.compile -//import kscience.kmath.expressions.Expression -//import kscience.kmath.expressions.expressionInField -//import kscience.kmath.expressions.invoke -//import kscience.kmath.operations.Field -//import kscience.kmath.operations.RealField -//import kotlin.random.Random -//import kotlin.system.measureTimeMillis -// -//class ExpressionsInterpretersBenchmark { -// private val algebra: Field = RealField -// fun functionalExpression() { -// val expr = algebra.expressionInField { -// variable("x") * const(2.0) + const(2.0) / variable("x") - const(16.0) -// } -// -// invokeAndSum(expr) -// } -// -// fun mstExpression() { -// val expr = algebra.mstInField { -// symbol("x") * number(2.0) + number(2.0) / symbol("x") - number(16.0) -// } -// -// invokeAndSum(expr) -// } -// -// fun asmExpression() { -// val expr = algebra.mstInField { -// symbol("x") * number(2.0) + number(2.0) / symbol("x") - number(16.0) -// }.compile() -// -// invokeAndSum(expr) -// } -// -// private fun invokeAndSum(expr: Expression) { -// val random = Random(0) -// var sum = 0.0 -// -// repeat(1000000) { -// sum += expr("x" to random.nextDouble()) -// } -// -// println(sum) -// } -//} -// -//fun main() { -// val benchmark = ExpressionsInterpretersBenchmark() -// -// val fe = measureTimeMillis { -// benchmark.functionalExpression() -// } -// -// println("fe=$fe") -// -// val mst = measureTimeMillis { -// benchmark.mstExpression() -// } -// -// println("mst=$mst") -// -// val asm = measureTimeMillis { -// benchmark.asmExpression() -// } -// -// println("asm=$asm") -//} + +import kscience.kmath.asm.compile +import kscience.kmath.expressions.Expression +import kscience.kmath.expressions.expressionInField +import kscience.kmath.expressions.invoke +import kscience.kmath.operations.Field +import kscience.kmath.operations.RealField +import kotlin.random.Random +import kotlin.system.measureTimeMillis + +internal class ExpressionsInterpretersBenchmark { + private val algebra: Field = RealField + fun functionalExpression() { + val expr = algebra.expressionInField { + variable("x") * const(2.0) + const(2.0) / variable("x") - const(16.0) + } + + invokeAndSum(expr) + } + + fun mstExpression() { + val expr = algebra.mstInField { + symbol("x") * number(2.0) + number(2.0) / symbol("x") - number(16.0) + } + + invokeAndSum(expr) + } + + fun asmExpression() { + val expr = algebra.mstInField { + symbol("x") * number(2.0) + number(2.0) / symbol("x") - number(16.0) + }.compile() + + invokeAndSum(expr) + } + + private fun invokeAndSum(expr: Expression) { + val random = Random(0) + var sum = 0.0 + + repeat(1000000) { + sum += expr("x" to random.nextDouble()) + } + + println(sum) + } +} + +/** + * This benchmark compares basically evaluation of simple function with MstExpression interpreter, ASM backend and + * core FunctionalExpressions API. + * + * The expected rating is: + * + * 1. ASM. + * 2. MST. + * 3. FE. + */ +fun main() { + val benchmark = ExpressionsInterpretersBenchmark() + + val fe = measureTimeMillis { + benchmark.functionalExpression() + } + + println("fe=$fe") + + val mst = measureTimeMillis { + benchmark.mstExpression() + } + + println("mst=$mst") + + val asm = measureTimeMillis { + benchmark.asmExpression() + } + + println("asm=$asm") +} diff --git a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt new file mode 100644 index 000000000..e63b0c9c0 --- /dev/null +++ b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt @@ -0,0 +1,22 @@ +package kscience.kmath.ast + +import edu.umontreal.kotlingrad.experimental.DoublePrecision +import kscience.kmath.asm.compile +import kscience.kmath.ast.kotlingrad.mst +import kscience.kmath.ast.kotlingrad.sfun +import kscience.kmath.ast.kotlingrad.svar +import kscience.kmath.expressions.invoke +import kscience.kmath.operations.RealField + +/** + * In this example, x^2-4*x-44 function is differentiated with Kotlin∇, and the autodiff result is compared with + * valid derivative. + */ +fun main() { + val proto = DoublePrecision.prototype + val x by MstAlgebra.symbol("x").svar(proto) + val quadratic = "x^2-4*x-44".parseMath().sfun(proto) + val actualDerivative = MstExpression(RealField, quadratic.d(x).mst()).compile() + val expectedDerivative = MstExpression(RealField, "2*x-4".parseMath()).compile() + assert(actualDerivative("x" to 123.0) == expectedDerivative("x" to 123.0)) +} From 4bf430b2c07a351d116e383a0d7806802af026c2 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 12 Oct 2020 23:17:54 +0700 Subject: [PATCH 07/19] Rename converter functions, add symbol delegate provider for MstAlgebra --- .../kscience/kmath/ast/KotlingradSupport.kt | 8 ++--- .../kmath/ast/kotlingrad/ScalarsAdapters.kt | 36 +++++++++---------- .../kmath/ast/kotlingrad/AdaptingTests.kt | 20 +++++------ .../kotlin/kscience/kmath/ast/extensions.kt | 22 ++++++++++++ 4 files changed, 54 insertions(+), 32 deletions(-) create mode 100644 kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/extensions.kt diff --git a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt index e63b0c9c0..c2e8456da 100644 --- a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt +++ b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt @@ -3,8 +3,8 @@ package kscience.kmath.ast import edu.umontreal.kotlingrad.experimental.DoublePrecision import kscience.kmath.asm.compile import kscience.kmath.ast.kotlingrad.mst -import kscience.kmath.ast.kotlingrad.sfun -import kscience.kmath.ast.kotlingrad.svar +import kscience.kmath.ast.kotlingrad.sFun +import kscience.kmath.ast.kotlingrad.sVar import kscience.kmath.expressions.invoke import kscience.kmath.operations.RealField @@ -14,8 +14,8 @@ import kscience.kmath.operations.RealField */ fun main() { val proto = DoublePrecision.prototype - val x by MstAlgebra.symbol("x").svar(proto) - val quadratic = "x^2-4*x-44".parseMath().sfun(proto) + val x by MstAlgebra.symbol("x").sVar(proto) + val quadratic = "x^2-4*x-44".parseMath().sFun(proto) val actualDerivative = MstExpression(RealField, quadratic.d(x).mst()).compile() val expectedDerivative = MstExpression(RealField, "2*x-4".parseMath()).compile() assert(actualDerivative("x" to 123.0) == expectedDerivative("x" to 123.0)) diff --git a/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt b/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt index 16c96646a..3e7cd0439 100644 --- a/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt +++ b/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt @@ -55,7 +55,7 @@ public fun > SFun.mst(): MST = MstExtendedField { * @receiver the node. * @return a new constant. */ -public fun > MST.Numeric.sconst(): SConst = SConst(value) +public fun > MST.Numeric.sConst(): SConst = SConst(value) /** * Maps [MST.Symbolic] to [SVar] directly. @@ -64,7 +64,7 @@ public fun > MST.Numeric.sconst(): SConst = SConst(value) * @param proto the prototype instance. * @return a new variable. */ -public fun > MST.Symbolic.svar(proto: X): SVar = SVar(proto, value) +public fun > MST.Symbolic.sVar(proto: X): SVar = SVar(proto, value) /** * Maps [MST] objects to [SFun]. Unsupported operations throw [IllegalStateException]. @@ -80,28 +80,28 @@ public fun > MST.Symbolic.svar(proto: X): SVar = SVar(proto, valu * @param proto the prototype instance. * @return a scalar function. */ -public fun > MST.sfun(proto: X): SFun = when (this) { - is MST.Numeric -> sconst() - is MST.Symbolic -> svar(proto) +public fun > MST.sFun(proto: X): SFun = when (this) { + is MST.Numeric -> sConst() + is MST.Symbolic -> sVar(proto) is MST.Unary -> when (operation) { - SpaceOperations.PLUS_OPERATION -> value.sfun(proto) - SpaceOperations.MINUS_OPERATION -> Negative(value.sfun(proto)) - TrigonometricOperations.SIN_OPERATION -> Sine(value.sfun(proto)) - TrigonometricOperations.COS_OPERATION -> Cosine(value.sfun(proto)) - TrigonometricOperations.TAN_OPERATION -> Tangent(value.sfun(proto)) - PowerOperations.SQRT_OPERATION -> Power(value.sfun(proto), SConst(0.5)) - ExponentialOperations.EXP_OPERATION -> Power(value.sfun(proto), E()) - ExponentialOperations.LN_OPERATION -> Log(value.sfun(proto)) + SpaceOperations.PLUS_OPERATION -> value.sFun(proto) + SpaceOperations.MINUS_OPERATION -> Negative(value.sFun(proto)) + TrigonometricOperations.SIN_OPERATION -> Sine(value.sFun(proto)) + TrigonometricOperations.COS_OPERATION -> Cosine(value.sFun(proto)) + TrigonometricOperations.TAN_OPERATION -> Tangent(value.sFun(proto)) + PowerOperations.SQRT_OPERATION -> Power(value.sFun(proto), SConst(0.5)) + ExponentialOperations.EXP_OPERATION -> Power(value.sFun(proto), E()) + ExponentialOperations.LN_OPERATION -> Log(value.sFun(proto)) else -> error("Unary operation $operation not defined in $this") } is MST.Binary -> when (operation) { - SpaceOperations.PLUS_OPERATION -> Sum(left.sfun(proto), right.sfun(proto)) - SpaceOperations.MINUS_OPERATION -> Sum(left.sfun(proto), Negative(right.sfun(proto))) - RingOperations.TIMES_OPERATION -> Prod(left.sfun(proto), right.sfun(proto)) - FieldOperations.DIV_OPERATION -> Prod(left.sfun(proto), Power(right.sfun(proto), Negative(One()))) - PowerOperations.POW_OPERATION -> Power(left.sfun(proto), SConst((right as MST.Numeric).value)) + SpaceOperations.PLUS_OPERATION -> Sum(left.sFun(proto), right.sFun(proto)) + SpaceOperations.MINUS_OPERATION -> Sum(left.sFun(proto), Negative(right.sFun(proto))) + RingOperations.TIMES_OPERATION -> Prod(left.sFun(proto), right.sFun(proto)) + FieldOperations.DIV_OPERATION -> Prod(left.sFun(proto), Power(right.sFun(proto), Negative(One()))) + PowerOperations.POW_OPERATION -> Power(left.sFun(proto), SConst((right as MST.Numeric).value)) else -> error("Binary operation $operation not defined in $this") } } diff --git a/kmath-ast-kotlingrad/src/test/kotlin/kscience/kmath/ast/kotlingrad/AdaptingTests.kt b/kmath-ast-kotlingrad/src/test/kotlin/kscience/kmath/ast/kotlingrad/AdaptingTests.kt index 94d25e411..c3c4602ad 100644 --- a/kmath-ast-kotlingrad/src/test/kotlin/kscience/kmath/ast/kotlingrad/AdaptingTests.kt +++ b/kmath-ast-kotlingrad/src/test/kotlin/kscience/kmath/ast/kotlingrad/AdaptingTests.kt @@ -18,24 +18,24 @@ internal class AdaptingTests { @Test fun symbol() { val c1 = MstAlgebra.symbol("x") - assertTrue(c1.svar(proto).name == "x") - val c2 = "kitten".parseMath().sfun(proto) + assertTrue(c1.sVar(proto).name == "x") + val c2 = "kitten".parseMath().sFun(proto) if (c2 is SVar) assertTrue(c2.name == "kitten") else fail() } @Test fun number() { val c1 = MstAlgebra.number(12354324) - assertTrue(c1.sconst().doubleValue == 12354324.0) - val c2 = "0.234".parseMath().sfun(proto) + assertTrue(c1.sConst().doubleValue == 12354324.0) + val c2 = "0.234".parseMath().sFun(proto) if (c2 is SConst) assertTrue(c2.doubleValue == 0.234) else fail() - val c3 = "1e-3".parseMath().sfun(proto) + val c3 = "1e-3".parseMath().sFun(proto) if (c3 is SConst) assertEquals(0.001, c3.value) else fail() } @Test fun simpleFunctionShape() { - val linear = "2*x+16".parseMath().sfun(proto) + val linear = "2*x+16".parseMath().sFun(proto) if (linear !is Sum) fail() if (linear.left !is Prod) fail() if (linear.right !is SConst) fail() @@ -43,8 +43,8 @@ internal class AdaptingTests { @Test fun simpleFunctionDerivative() { - val x = MstAlgebra.symbol("x").svar(proto) - val quadratic = "x^2-4*x-44".parseMath().sfun(proto) + val x = MstAlgebra.symbol("x").sVar(proto) + val quadratic = "x^2-4*x-44".parseMath().sFun(proto) val actualDerivative = MstExpression(RealField, quadratic.d(x).mst()).compile() val expectedDerivative = MstExpression(RealField, "2*x-4".parseMath()).compile() assertEquals(actualDerivative("x" to 123.0), expectedDerivative("x" to 123.0)) @@ -52,8 +52,8 @@ internal class AdaptingTests { @Test fun moreComplexDerivative() { - val x = MstAlgebra.symbol("x").svar(proto) - val composition = "-sqrt(sin(x^2)-cos(x)^2-16*x)".parseMath().sfun(proto) + val x = MstAlgebra.symbol("x").sVar(proto) + val composition = "-sqrt(sin(x^2)-cos(x)^2-16*x)".parseMath().sFun(proto) val actualDerivative = MstExpression(RealField, composition.d(x).mst()).compile() val expectedDerivative = MstExpression( diff --git a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/extensions.kt b/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/extensions.kt new file mode 100644 index 000000000..cba4bbb13 --- /dev/null +++ b/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/extensions.kt @@ -0,0 +1,22 @@ +package kscience.kmath.ast + +import kscience.kmath.operations.Algebra +import kotlin.properties.ReadOnlyProperty +import kotlin.reflect.KProperty + +/** + * Stores `provideDelegate` method returning property of [MST.Symbolic]. + */ +public object MstSymbolDelegateProvider { + /** + * Returns [ReadOnlyProperty] of [MST.Symbolic] with its value equal to the name of the property. + */ + public operator fun provideDelegate(thisRef: Any?, prop: KProperty<*>): ReadOnlyProperty = + ReadOnlyProperty { _, property -> MST.Symbolic(property.name) } +} + +/** + * Returns [MstSymbolDelegateProvider]. + */ +public val Algebra.symbol: MstSymbolDelegateProvider + get() = MstSymbolDelegateProvider From 06c3ce5aaf8020b95a18eaae95381fe80186bee7 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 12 Oct 2020 23:42:13 +0700 Subject: [PATCH 08/19] Simplify extensions.kt --- .../kotlin/kscience/kmath/ast/extensions.kt | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/extensions.kt b/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/extensions.kt index cba4bbb13..b790a3a88 100644 --- a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/extensions.kt +++ b/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/extensions.kt @@ -1,22 +1,12 @@ package kscience.kmath.ast import kscience.kmath.operations.Algebra +import kotlin.properties.PropertyDelegateProvider import kotlin.properties.ReadOnlyProperty -import kotlin.reflect.KProperty /** - * Stores `provideDelegate` method returning property of [MST.Symbolic]. + * Returns [PropertyDelegateProvider] providing [ReadOnlyProperty] of [MST.Symbolic] with its value equal to the name + * of the property. */ -public object MstSymbolDelegateProvider { - /** - * Returns [ReadOnlyProperty] of [MST.Symbolic] with its value equal to the name of the property. - */ - public operator fun provideDelegate(thisRef: Any?, prop: KProperty<*>): ReadOnlyProperty = - ReadOnlyProperty { _, property -> MST.Symbolic(property.name) } -} - -/** - * Returns [MstSymbolDelegateProvider]. - */ -public val Algebra.symbol: MstSymbolDelegateProvider - get() = MstSymbolDelegateProvider +public val Algebra.symbol: PropertyDelegateProvider, ReadOnlyProperty, MST.Symbolic>> + get() = PropertyDelegateProvider { _, _ -> ReadOnlyProperty { _, p -> MST.Symbolic(p.name) } } From 381137724dc6e6fbb3795b80a988dd64d2578d11 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Tue, 13 Oct 2020 19:47:07 +0700 Subject: [PATCH 09/19] Rename KG module --- examples/build.gradle.kts | 2 +- .../kscience/kmath/ast/KotlingradSupport.kt | 12 ++-- .../build.gradle.kts | 0 .../kmath}/kotlingrad/ScalarsAdapters.kt | 66 +++++++++---------- .../kmath}/kotlingrad/AdaptingTests.kt | 26 ++++---- settings.gradle.kts | 2 +- 6 files changed, 54 insertions(+), 54 deletions(-) rename {kmath-ast-kotlingrad => kmath-kotlingrad}/build.gradle.kts (100%) rename {kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast => kmath-kotlingrad/src/main/kotlin/kscience/kmath}/kotlingrad/ScalarsAdapters.kt (51%) rename {kmath-ast-kotlingrad/src/test/kotlin/kscience/kmath/ast => kmath-kotlingrad/src/test/kotlin/kscience/kmath}/kotlingrad/AdaptingTests.kt (74%) diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 968b372c3..46b677304 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -20,7 +20,7 @@ sourceSets.register("benchmarks") dependencies { implementation(project(":kmath-ast")) - implementation(project(":kmath-ast-kotlingrad")) + implementation(project(":kmath-kotlingrad")) implementation(project(":kmath-core")) implementation(project(":kmath-coroutines")) implementation(project(":kmath-commons")) diff --git a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt index c2e8456da..366a2b4fd 100644 --- a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt +++ b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt @@ -2,9 +2,9 @@ package kscience.kmath.ast import edu.umontreal.kotlingrad.experimental.DoublePrecision import kscience.kmath.asm.compile -import kscience.kmath.ast.kotlingrad.mst -import kscience.kmath.ast.kotlingrad.sFun -import kscience.kmath.ast.kotlingrad.sVar +import kscience.kmath.kotlingrad.toMst +import kscience.kmath.kotlingrad.tSFun +import kscience.kmath.kotlingrad.toSVar import kscience.kmath.expressions.invoke import kscience.kmath.operations.RealField @@ -14,9 +14,9 @@ import kscience.kmath.operations.RealField */ fun main() { val proto = DoublePrecision.prototype - val x by MstAlgebra.symbol("x").sVar(proto) - val quadratic = "x^2-4*x-44".parseMath().sFun(proto) - val actualDerivative = MstExpression(RealField, quadratic.d(x).mst()).compile() + val x by MstAlgebra.symbol("x").toSVar(proto) + val quadratic = "x^2-4*x-44".parseMath().tSFun(proto) + val actualDerivative = MstExpression(RealField, quadratic.d(x).toMst()).compile() val expectedDerivative = MstExpression(RealField, "2*x-4".parseMath()).compile() assert(actualDerivative("x" to 123.0) == expectedDerivative("x" to 123.0)) } diff --git a/kmath-ast-kotlingrad/build.gradle.kts b/kmath-kotlingrad/build.gradle.kts similarity index 100% rename from kmath-ast-kotlingrad/build.gradle.kts rename to kmath-kotlingrad/build.gradle.kts diff --git a/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt similarity index 51% rename from kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt rename to kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt index 3e7cd0439..f6f0c7a76 100644 --- a/kmath-ast-kotlingrad/src/main/kotlin/kscience/kmath/ast/kotlingrad/ScalarsAdapters.kt +++ b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt @@ -1,4 +1,4 @@ -package kscience.kmath.ast.kotlingrad +package kscience.kmath.kotlingrad import edu.umontreal.kotlingrad.experimental.* import kscience.kmath.ast.MST @@ -30,22 +30,22 @@ import kscience.kmath.operations.* * @receiver the scalar function. * @return a node. */ -public fun > SFun.mst(): MST = MstExtendedField { - when (this@mst) { +public fun > SFun.toMst(): MST = MstExtendedField { + when (this@toMst) { is SVar -> symbol(name) is SConst -> number(doubleValue) - is Sum -> left.mst() + right.mst() - is Prod -> left.mst() * right.mst() - is Power -> power(left.mst(), (right as SConst<*>).doubleValue) - is Negative -> -input.mst() - is Log -> ln(left.mst()) / ln(right.mst()) - is Sine -> sin(input.mst()) - is Cosine -> cos(input.mst()) - is Tangent -> tan(input.mst()) - is DProd -> this@mst().mst() - is SComposition -> this@mst().mst() - is VSumAll -> this@mst().mst() - is Derivative -> this@mst().mst() + is Sum -> left.toMst() + right.toMst() + is Prod -> left.toMst() * right.toMst() + is Power -> power(left.toMst(), (right as SConst<*>).doubleValue) + is Negative -> -input.toMst() + is Log -> ln(left.toMst()) / ln(right.toMst()) + is Sine -> sin(input.toMst()) + is Cosine -> cos(input.toMst()) + is Tangent -> tan(input.toMst()) + is DProd -> this@toMst().toMst() + is SComposition -> this@toMst().toMst() + is VSumAll -> this@toMst().toMst() + is Derivative -> this@toMst().toMst() } } @@ -55,7 +55,7 @@ public fun > SFun.mst(): MST = MstExtendedField { * @receiver the node. * @return a new constant. */ -public fun > MST.Numeric.sConst(): SConst = SConst(value) +public fun > MST.Numeric.toSConst(): SConst = SConst(value) /** * Maps [MST.Symbolic] to [SVar] directly. @@ -64,7 +64,7 @@ public fun > MST.Numeric.sConst(): SConst = SConst(value) * @param proto the prototype instance. * @return a new variable. */ -public fun > MST.Symbolic.sVar(proto: X): SVar = SVar(proto, value) +public fun > MST.Symbolic.toSVar(proto: X): SVar = SVar(proto, value) /** * Maps [MST] objects to [SFun]. Unsupported operations throw [IllegalStateException]. @@ -80,28 +80,28 @@ public fun > MST.Symbolic.sVar(proto: X): SVar = SVar(proto, valu * @param proto the prototype instance. * @return a scalar function. */ -public fun > MST.sFun(proto: X): SFun = when (this) { - is MST.Numeric -> sConst() - is MST.Symbolic -> sVar(proto) +public fun > MST.tSFun(proto: X): SFun = when (this) { + is MST.Numeric -> toSConst() + is MST.Symbolic -> toSVar(proto) is MST.Unary -> when (operation) { - SpaceOperations.PLUS_OPERATION -> value.sFun(proto) - SpaceOperations.MINUS_OPERATION -> Negative(value.sFun(proto)) - TrigonometricOperations.SIN_OPERATION -> Sine(value.sFun(proto)) - TrigonometricOperations.COS_OPERATION -> Cosine(value.sFun(proto)) - TrigonometricOperations.TAN_OPERATION -> Tangent(value.sFun(proto)) - PowerOperations.SQRT_OPERATION -> Power(value.sFun(proto), SConst(0.5)) - ExponentialOperations.EXP_OPERATION -> Power(value.sFun(proto), E()) - ExponentialOperations.LN_OPERATION -> Log(value.sFun(proto)) + SpaceOperations.PLUS_OPERATION -> value.tSFun(proto) + SpaceOperations.MINUS_OPERATION -> Negative(value.tSFun(proto)) + TrigonometricOperations.SIN_OPERATION -> Sine(value.tSFun(proto)) + TrigonometricOperations.COS_OPERATION -> Cosine(value.tSFun(proto)) + TrigonometricOperations.TAN_OPERATION -> Tangent(value.tSFun(proto)) + PowerOperations.SQRT_OPERATION -> Power(value.tSFun(proto), SConst(0.5)) + ExponentialOperations.EXP_OPERATION -> Power(value.tSFun(proto), E()) + ExponentialOperations.LN_OPERATION -> Log(value.tSFun(proto)) else -> error("Unary operation $operation not defined in $this") } is MST.Binary -> when (operation) { - SpaceOperations.PLUS_OPERATION -> Sum(left.sFun(proto), right.sFun(proto)) - SpaceOperations.MINUS_OPERATION -> Sum(left.sFun(proto), Negative(right.sFun(proto))) - RingOperations.TIMES_OPERATION -> Prod(left.sFun(proto), right.sFun(proto)) - FieldOperations.DIV_OPERATION -> Prod(left.sFun(proto), Power(right.sFun(proto), Negative(One()))) - PowerOperations.POW_OPERATION -> Power(left.sFun(proto), SConst((right as MST.Numeric).value)) + SpaceOperations.PLUS_OPERATION -> Sum(left.tSFun(proto), right.tSFun(proto)) + SpaceOperations.MINUS_OPERATION -> Sum(left.tSFun(proto), Negative(right.tSFun(proto))) + RingOperations.TIMES_OPERATION -> Prod(left.tSFun(proto), right.tSFun(proto)) + FieldOperations.DIV_OPERATION -> Prod(left.tSFun(proto), Power(right.tSFun(proto), Negative(One()))) + PowerOperations.POW_OPERATION -> Power(left.tSFun(proto), SConst((right as MST.Numeric).value)) else -> error("Binary operation $operation not defined in $this") } } diff --git a/kmath-ast-kotlingrad/src/test/kotlin/kscience/kmath/ast/kotlingrad/AdaptingTests.kt b/kmath-kotlingrad/src/test/kotlin/kscience/kmath/kotlingrad/AdaptingTests.kt similarity index 74% rename from kmath-ast-kotlingrad/src/test/kotlin/kscience/kmath/ast/kotlingrad/AdaptingTests.kt rename to kmath-kotlingrad/src/test/kotlin/kscience/kmath/kotlingrad/AdaptingTests.kt index c3c4602ad..25bdbf4be 100644 --- a/kmath-ast-kotlingrad/src/test/kotlin/kscience/kmath/ast/kotlingrad/AdaptingTests.kt +++ b/kmath-kotlingrad/src/test/kotlin/kscience/kmath/kotlingrad/AdaptingTests.kt @@ -1,4 +1,4 @@ -package kscience.kmath.ast.kotlingrad +package kscience.kmath.kotlingrad import edu.umontreal.kotlingrad.experimental.* import kscience.kmath.asm.compile @@ -18,24 +18,24 @@ internal class AdaptingTests { @Test fun symbol() { val c1 = MstAlgebra.symbol("x") - assertTrue(c1.sVar(proto).name == "x") - val c2 = "kitten".parseMath().sFun(proto) + assertTrue(c1.toSVar(proto).name == "x") + val c2 = "kitten".parseMath().tSFun(proto) if (c2 is SVar) assertTrue(c2.name == "kitten") else fail() } @Test fun number() { val c1 = MstAlgebra.number(12354324) - assertTrue(c1.sConst().doubleValue == 12354324.0) - val c2 = "0.234".parseMath().sFun(proto) + assertTrue(c1.toSConst().doubleValue == 12354324.0) + val c2 = "0.234".parseMath().tSFun(proto) if (c2 is SConst) assertTrue(c2.doubleValue == 0.234) else fail() - val c3 = "1e-3".parseMath().sFun(proto) + val c3 = "1e-3".parseMath().tSFun(proto) if (c3 is SConst) assertEquals(0.001, c3.value) else fail() } @Test fun simpleFunctionShape() { - val linear = "2*x+16".parseMath().sFun(proto) + val linear = "2*x+16".parseMath().tSFun(proto) if (linear !is Sum) fail() if (linear.left !is Prod) fail() if (linear.right !is SConst) fail() @@ -43,18 +43,18 @@ internal class AdaptingTests { @Test fun simpleFunctionDerivative() { - val x = MstAlgebra.symbol("x").sVar(proto) - val quadratic = "x^2-4*x-44".parseMath().sFun(proto) - val actualDerivative = MstExpression(RealField, quadratic.d(x).mst()).compile() + val x = MstAlgebra.symbol("x").toSVar(proto) + val quadratic = "x^2-4*x-44".parseMath().tSFun(proto) + val actualDerivative = MstExpression(RealField, quadratic.d(x).toMst()).compile() val expectedDerivative = MstExpression(RealField, "2*x-4".parseMath()).compile() assertEquals(actualDerivative("x" to 123.0), expectedDerivative("x" to 123.0)) } @Test fun moreComplexDerivative() { - val x = MstAlgebra.symbol("x").sVar(proto) - val composition = "-sqrt(sin(x^2)-cos(x)^2-16*x)".parseMath().sFun(proto) - val actualDerivative = MstExpression(RealField, composition.d(x).mst()).compile() + val x = MstAlgebra.symbol("x").toSVar(proto) + val composition = "-sqrt(sin(x^2)-cos(x)^2-16*x)".parseMath().tSFun(proto) + val actualDerivative = MstExpression(RealField, composition.d(x).toMst()).compile() val expectedDerivative = MstExpression( RealField, diff --git a/settings.gradle.kts b/settings.gradle.kts index 5fd072e1a..9343db854 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -40,5 +40,5 @@ include( ":kmath-ast", ":examples", ":kmath-ejml", - ":kmath-ast-kotlingrad" + ":kmath-kotlingrad" ) From 2723c376d9b123cb0d311411d6757d6fb8f379b7 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Tue, 13 Oct 2020 22:09:39 +0700 Subject: [PATCH 10/19] Use KG DSL instead of raw scalar construction --- .../kmath/kotlingrad/ScalarsAdapters.kt | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt index f6f0c7a76..e4777282f 100644 --- a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt +++ b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt @@ -16,7 +16,7 @@ import kscience.kmath.operations.* * - [SConst] -> [MstExtendedField.number]; * - [Sum] -> [MstExtendedField.add]; * - [Prod] -> [MstExtendedField.multiply]; - * - [Power] -> [MstExtendedField.power] (limited); + * - [Power] -> [MstExtendedField.power] (limited to constant exponents only); * - [Negative] -> [MstExtendedField.unaryMinus]; * - [Log] -> [MstExtendedField.ln] (left) / [MstExtendedField.ln] (right); * - [Sine] -> [MstExtendedField.sin]; @@ -36,7 +36,7 @@ public fun > SFun.toMst(): MST = MstExtendedField { is SConst -> number(doubleValue) is Sum -> left.toMst() + right.toMst() is Prod -> left.toMst() * right.toMst() - is Power -> power(left.toMst(), (right as SConst<*>).doubleValue) + is Power -> left.toMst() pow ((right as? SConst<*>)?.doubleValue ?: (right() as SConst<*>).doubleValue) is Negative -> -input.toMst() is Log -> ln(left.toMst()) / ln(right.toMst()) is Sine -> sin(input.toMst()) @@ -86,22 +86,22 @@ public fun > MST.tSFun(proto: X): SFun = when (this) { is MST.Unary -> when (operation) { SpaceOperations.PLUS_OPERATION -> value.tSFun(proto) - SpaceOperations.MINUS_OPERATION -> Negative(value.tSFun(proto)) - TrigonometricOperations.SIN_OPERATION -> Sine(value.tSFun(proto)) - TrigonometricOperations.COS_OPERATION -> Cosine(value.tSFun(proto)) - TrigonometricOperations.TAN_OPERATION -> Tangent(value.tSFun(proto)) - PowerOperations.SQRT_OPERATION -> Power(value.tSFun(proto), SConst(0.5)) - ExponentialOperations.EXP_OPERATION -> Power(value.tSFun(proto), E()) - ExponentialOperations.LN_OPERATION -> Log(value.tSFun(proto)) + SpaceOperations.MINUS_OPERATION -> -value.tSFun(proto) + TrigonometricOperations.SIN_OPERATION -> sin(value.tSFun(proto)) + TrigonometricOperations.COS_OPERATION -> cos(value.tSFun(proto)) + TrigonometricOperations.TAN_OPERATION -> tan(value.tSFun(proto)) + PowerOperations.SQRT_OPERATION -> value.tSFun(proto) pow SConst(0.5) + ExponentialOperations.EXP_OPERATION -> E() pow value.tSFun(proto) + ExponentialOperations.LN_OPERATION -> value.tSFun(proto).ln() else -> error("Unary operation $operation not defined in $this") } is MST.Binary -> when (operation) { - SpaceOperations.PLUS_OPERATION -> Sum(left.tSFun(proto), right.tSFun(proto)) - SpaceOperations.MINUS_OPERATION -> Sum(left.tSFun(proto), Negative(right.tSFun(proto))) - RingOperations.TIMES_OPERATION -> Prod(left.tSFun(proto), right.tSFun(proto)) - FieldOperations.DIV_OPERATION -> Prod(left.tSFun(proto), Power(right.tSFun(proto), Negative(One()))) - PowerOperations.POW_OPERATION -> Power(left.tSFun(proto), SConst((right as MST.Numeric).value)) + SpaceOperations.PLUS_OPERATION -> left.tSFun(proto) + right.tSFun(proto) + SpaceOperations.MINUS_OPERATION -> left.tSFun(proto) - right.tSFun(proto) + RingOperations.TIMES_OPERATION -> left.tSFun(proto) * right.tSFun(proto) + FieldOperations.DIV_OPERATION -> left.tSFun(proto) / right.tSFun(proto) + PowerOperations.POW_OPERATION -> left.tSFun(proto) pow (right as MST.Numeric).toSConst() else -> error("Binary operation $operation not defined in $this") } } From ea0ecc0fba58b31cb9e24020db20332cfb9e73bd Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Tue, 13 Oct 2020 22:18:44 +0700 Subject: [PATCH 11/19] Use postfix op. form --- .../main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt index e4777282f..99ab5e635 100644 --- a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt +++ b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt @@ -90,7 +90,7 @@ public fun > MST.tSFun(proto: X): SFun = when (this) { TrigonometricOperations.SIN_OPERATION -> sin(value.tSFun(proto)) TrigonometricOperations.COS_OPERATION -> cos(value.tSFun(proto)) TrigonometricOperations.TAN_OPERATION -> tan(value.tSFun(proto)) - PowerOperations.SQRT_OPERATION -> value.tSFun(proto) pow SConst(0.5) + PowerOperations.SQRT_OPERATION -> value.tSFun(proto).sqrt() ExponentialOperations.EXP_OPERATION -> E() pow value.tSFun(proto) ExponentialOperations.LN_OPERATION -> value.tSFun(proto).ln() else -> error("Unary operation $operation not defined in $this") From 7f8abbdd206f41da85c834508efa53815e234183 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Thu, 29 Oct 2020 02:22:34 +0700 Subject: [PATCH 12/19] Fix typo, introduce KG protocol delegating to algebra --- .../kscience/kmath/ast/KotlingradSupport.kt | 8 +-- .../kmath/kotlingrad/ScalarsAdapters.kt | 57 ++++++++++++++----- .../kmath/kotlingrad/AdaptingTests.kt | 12 ++-- 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt index 366a2b4fd..c8478a631 100644 --- a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt +++ b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt @@ -2,10 +2,10 @@ package kscience.kmath.ast import edu.umontreal.kotlingrad.experimental.DoublePrecision import kscience.kmath.asm.compile -import kscience.kmath.kotlingrad.toMst -import kscience.kmath.kotlingrad.tSFun -import kscience.kmath.kotlingrad.toSVar import kscience.kmath.expressions.invoke +import kscience.kmath.kotlingrad.toMst +import kscience.kmath.kotlingrad.toSFun +import kscience.kmath.kotlingrad.toSVar import kscience.kmath.operations.RealField /** @@ -15,7 +15,7 @@ import kscience.kmath.operations.RealField fun main() { val proto = DoublePrecision.prototype val x by MstAlgebra.symbol("x").toSVar(proto) - val quadratic = "x^2-4*x-44".parseMath().tSFun(proto) + val quadratic = "x^2-4*x-44".parseMath().toSFun(proto) val actualDerivative = MstExpression(RealField, quadratic.d(x).toMst()).compile() val expectedDerivative = MstExpression(RealField, "2*x-4".parseMath()).compile() assert(actualDerivative("x" to 123.0) == expectedDerivative("x" to 123.0)) diff --git a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt index 99ab5e635..a9a8a14b2 100644 --- a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt +++ b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt @@ -2,8 +2,13 @@ package kscience.kmath.kotlingrad import edu.umontreal.kotlingrad.experimental.* import kscience.kmath.ast.MST +import kscience.kmath.ast.MstAlgebra +import kscience.kmath.ast.MstExpression import kscience.kmath.ast.MstExtendedField import kscience.kmath.ast.MstExtendedField.unaryMinus +import kscience.kmath.expressions.DifferentiableExpression +import kscience.kmath.expressions.Expression +import kscience.kmath.expressions.Symbol import kscience.kmath.operations.* /** @@ -80,28 +85,52 @@ public fun > MST.Symbolic.toSVar(proto: X): SVar = SVar(proto, va * @param proto the prototype instance. * @return a scalar function. */ -public fun > MST.tSFun(proto: X): SFun = when (this) { +public fun > MST.toSFun(proto: X): SFun = when (this) { is MST.Numeric -> toSConst() is MST.Symbolic -> toSVar(proto) is MST.Unary -> when (operation) { - SpaceOperations.PLUS_OPERATION -> value.tSFun(proto) - SpaceOperations.MINUS_OPERATION -> -value.tSFun(proto) - TrigonometricOperations.SIN_OPERATION -> sin(value.tSFun(proto)) - TrigonometricOperations.COS_OPERATION -> cos(value.tSFun(proto)) - TrigonometricOperations.TAN_OPERATION -> tan(value.tSFun(proto)) - PowerOperations.SQRT_OPERATION -> value.tSFun(proto).sqrt() - ExponentialOperations.EXP_OPERATION -> E() pow value.tSFun(proto) - ExponentialOperations.LN_OPERATION -> value.tSFun(proto).ln() + SpaceOperations.PLUS_OPERATION -> value.toSFun(proto) + SpaceOperations.MINUS_OPERATION -> -value.toSFun(proto) + TrigonometricOperations.SIN_OPERATION -> sin(value.toSFun(proto)) + TrigonometricOperations.COS_OPERATION -> cos(value.toSFun(proto)) + TrigonometricOperations.TAN_OPERATION -> tan(value.toSFun(proto)) + PowerOperations.SQRT_OPERATION -> value.toSFun(proto).sqrt() + ExponentialOperations.EXP_OPERATION -> E() pow value.toSFun(proto) + ExponentialOperations.LN_OPERATION -> value.toSFun(proto).ln() else -> error("Unary operation $operation not defined in $this") } is MST.Binary -> when (operation) { - SpaceOperations.PLUS_OPERATION -> left.tSFun(proto) + right.tSFun(proto) - SpaceOperations.MINUS_OPERATION -> left.tSFun(proto) - right.tSFun(proto) - RingOperations.TIMES_OPERATION -> left.tSFun(proto) * right.tSFun(proto) - FieldOperations.DIV_OPERATION -> left.tSFun(proto) / right.tSFun(proto) - PowerOperations.POW_OPERATION -> left.tSFun(proto) pow (right as MST.Numeric).toSConst() + SpaceOperations.PLUS_OPERATION -> left.toSFun(proto) + right.toSFun(proto) + SpaceOperations.MINUS_OPERATION -> left.toSFun(proto) - right.toSFun(proto) + RingOperations.TIMES_OPERATION -> left.toSFun(proto) * right.toSFun(proto) + FieldOperations.DIV_OPERATION -> left.toSFun(proto) / right.toSFun(proto) + PowerOperations.POW_OPERATION -> left.toSFun(proto) pow (right as MST.Numeric).toSConst() else -> error("Binary operation $operation not defined in $this") } } + +public class KMathNumber(public val algebra: A, value: T) : + RealNumber, T>(value) where T : Number, A : NumericAlgebra { + public override fun wrap(number: Number): SConst> = SConst(algebra.number(number)) + override val proto: KMathNumber by lazy { KMathNumber(algebra, algebra.number(Double.NaN)) } +} + +public class KMathProtocol(algebra: A) : + Protocol>(KMathNumber(algebra, algebra.number(Double.NaN))) + where T : Number, A : NumericAlgebra + +public class DifferentiableMstExpression(public val algebra: A, public val mst: MST) : + DifferentiableExpression where A : NumericAlgebra, T : Number { + public val proto by lazy { KMathProtocol(algebra).prototype } + public val expr by lazy { MstExpression(algebra, mst) } + + public override fun invoke(arguments: Map): T = expr(arguments) + + public override fun derivativeOrNull(orders: Map): Expression { + val sfun = mst.toSFun(proto) + val orders2 = orders.mapKeys { (k, _) -> MstAlgebra.symbol(k.identity).toSVar(proto) } + TODO() + } +} diff --git a/kmath-kotlingrad/src/test/kotlin/kscience/kmath/kotlingrad/AdaptingTests.kt b/kmath-kotlingrad/src/test/kotlin/kscience/kmath/kotlingrad/AdaptingTests.kt index 25bdbf4be..682b0cf2e 100644 --- a/kmath-kotlingrad/src/test/kotlin/kscience/kmath/kotlingrad/AdaptingTests.kt +++ b/kmath-kotlingrad/src/test/kotlin/kscience/kmath/kotlingrad/AdaptingTests.kt @@ -19,7 +19,7 @@ internal class AdaptingTests { fun symbol() { val c1 = MstAlgebra.symbol("x") assertTrue(c1.toSVar(proto).name == "x") - val c2 = "kitten".parseMath().tSFun(proto) + val c2 = "kitten".parseMath().toSFun(proto) if (c2 is SVar) assertTrue(c2.name == "kitten") else fail() } @@ -27,15 +27,15 @@ internal class AdaptingTests { fun number() { val c1 = MstAlgebra.number(12354324) assertTrue(c1.toSConst().doubleValue == 12354324.0) - val c2 = "0.234".parseMath().tSFun(proto) + val c2 = "0.234".parseMath().toSFun(proto) if (c2 is SConst) assertTrue(c2.doubleValue == 0.234) else fail() - val c3 = "1e-3".parseMath().tSFun(proto) + val c3 = "1e-3".parseMath().toSFun(proto) if (c3 is SConst) assertEquals(0.001, c3.value) else fail() } @Test fun simpleFunctionShape() { - val linear = "2*x+16".parseMath().tSFun(proto) + val linear = "2*x+16".parseMath().toSFun(proto) if (linear !is Sum) fail() if (linear.left !is Prod) fail() if (linear.right !is SConst) fail() @@ -44,7 +44,7 @@ internal class AdaptingTests { @Test fun simpleFunctionDerivative() { val x = MstAlgebra.symbol("x").toSVar(proto) - val quadratic = "x^2-4*x-44".parseMath().tSFun(proto) + val quadratic = "x^2-4*x-44".parseMath().toSFun(proto) val actualDerivative = MstExpression(RealField, quadratic.d(x).toMst()).compile() val expectedDerivative = MstExpression(RealField, "2*x-4".parseMath()).compile() assertEquals(actualDerivative("x" to 123.0), expectedDerivative("x" to 123.0)) @@ -53,7 +53,7 @@ internal class AdaptingTests { @Test fun moreComplexDerivative() { val x = MstAlgebra.symbol("x").toSVar(proto) - val composition = "-sqrt(sin(x^2)-cos(x)^2-16*x)".parseMath().tSFun(proto) + val composition = "-sqrt(sin(x^2)-cos(x)^2-16*x)".parseMath().toSFun(proto) val actualDerivative = MstExpression(RealField, composition.d(x).toMst()).compile() val expectedDerivative = MstExpression( From 6f0f6577de5c555993d19ae3c2b19f5ab27f1e5b Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Thu, 29 Oct 2020 13:34:12 +0700 Subject: [PATCH 13/19] Refactor toSFun, update KG, delete KMath algebra protocol, update DifferentiableMstExpr. --- kmath-kotlingrad/build.gradle.kts | 2 +- .../kmath/kotlingrad/ScalarsAdapters.kt | 50 +++++++++---------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/kmath-kotlingrad/build.gradle.kts b/kmath-kotlingrad/build.gradle.kts index 0fe6e6b93..f2245c3d5 100644 --- a/kmath-kotlingrad/build.gradle.kts +++ b/kmath-kotlingrad/build.gradle.kts @@ -3,6 +3,6 @@ plugins { } dependencies { - api("com.github.breandan:kotlingrad:0.3.2") + api("com.github.breandan:kotlingrad:0.3.7") api(project(":kmath-ast")) } diff --git a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt index a9a8a14b2..24e8377bd 100644 --- a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt +++ b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt @@ -69,7 +69,7 @@ public fun > MST.Numeric.toSConst(): SConst = SConst(value) * @param proto the prototype instance. * @return a new variable. */ -public fun > MST.Symbolic.toSVar(proto: X): SVar = SVar(proto, value) +public fun > MST.Symbolic.toSVar(): SVar = SVar(value) /** * Maps [MST] objects to [SFun]. Unsupported operations throw [IllegalStateException]. @@ -85,28 +85,28 @@ public fun > MST.Symbolic.toSVar(proto: X): SVar = SVar(proto, va * @param proto the prototype instance. * @return a scalar function. */ -public fun > MST.toSFun(proto: X): SFun = when (this) { +public fun > MST.toSFun(): SFun = when (this) { is MST.Numeric -> toSConst() - is MST.Symbolic -> toSVar(proto) + is MST.Symbolic -> toSVar() is MST.Unary -> when (operation) { - SpaceOperations.PLUS_OPERATION -> value.toSFun(proto) - SpaceOperations.MINUS_OPERATION -> -value.toSFun(proto) - TrigonometricOperations.SIN_OPERATION -> sin(value.toSFun(proto)) - TrigonometricOperations.COS_OPERATION -> cos(value.toSFun(proto)) - TrigonometricOperations.TAN_OPERATION -> tan(value.toSFun(proto)) - PowerOperations.SQRT_OPERATION -> value.toSFun(proto).sqrt() - ExponentialOperations.EXP_OPERATION -> E() pow value.toSFun(proto) - ExponentialOperations.LN_OPERATION -> value.toSFun(proto).ln() + SpaceOperations.PLUS_OPERATION -> value.toSFun() + SpaceOperations.MINUS_OPERATION -> (-value).toSFun() + TrigonometricOperations.SIN_OPERATION -> sin(value.toSFun()) + TrigonometricOperations.COS_OPERATION -> cos(value.toSFun()) + TrigonometricOperations.TAN_OPERATION -> tan(value.toSFun()) + PowerOperations.SQRT_OPERATION -> value.toSFun().sqrt() + ExponentialOperations.EXP_OPERATION -> exp(value.toSFun()) + ExponentialOperations.LN_OPERATION -> value.toSFun().ln() else -> error("Unary operation $operation not defined in $this") } is MST.Binary -> when (operation) { - SpaceOperations.PLUS_OPERATION -> left.toSFun(proto) + right.toSFun(proto) - SpaceOperations.MINUS_OPERATION -> left.toSFun(proto) - right.toSFun(proto) - RingOperations.TIMES_OPERATION -> left.toSFun(proto) * right.toSFun(proto) - FieldOperations.DIV_OPERATION -> left.toSFun(proto) / right.toSFun(proto) - PowerOperations.POW_OPERATION -> left.toSFun(proto) pow (right as MST.Numeric).toSConst() + SpaceOperations.PLUS_OPERATION -> left.toSFun() + right.toSFun() + SpaceOperations.MINUS_OPERATION -> left.toSFun() - right.toSFun() + RingOperations.TIMES_OPERATION -> left.toSFun() * right.toSFun() + FieldOperations.DIV_OPERATION -> left.toSFun() / right.toSFun() + PowerOperations.POW_OPERATION -> left.toSFun() pow (right as MST.Numeric).toSConst() else -> error("Binary operation $operation not defined in $this") } } @@ -114,23 +114,23 @@ public fun > MST.toSFun(proto: X): SFun = when (this) { public class KMathNumber(public val algebra: A, value: T) : RealNumber, T>(value) where T : Number, A : NumericAlgebra { public override fun wrap(number: Number): SConst> = SConst(algebra.number(number)) - override val proto: KMathNumber by lazy { KMathNumber(algebra, algebra.number(Double.NaN)) } } -public class KMathProtocol(algebra: A) : - Protocol>(KMathNumber(algebra, algebra.number(Double.NaN))) - where T : Number, A : NumericAlgebra - public class DifferentiableMstExpression(public val algebra: A, public val mst: MST) : DifferentiableExpression where A : NumericAlgebra, T : Number { - public val proto by lazy { KMathProtocol(algebra).prototype } public val expr by lazy { MstExpression(algebra, mst) } - public override fun invoke(arguments: Map): T = expr(arguments) + public override fun invoke(arguments: Map): T = expr(arguments) public override fun derivativeOrNull(orders: Map): Expression { - val sfun = mst.toSFun(proto) - val orders2 = orders.mapKeys { (k, _) -> MstAlgebra.symbol(k.identity).toSVar(proto) } + TODO() + } + + public fun derivativeOrNull(orders: List): Expression { + orders.map { MstAlgebra.symbol(it.identity).toSVar>() } + .fold>, SFun>>(mst.toSFun()) { result, sVar -> result.d(sVar) } + .toMst() + TODO() } } From 29a670483ba315a3adde3f6fabc2f69c3d1c09e8 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Fri, 30 Oct 2020 01:09:11 +0700 Subject: [PATCH 14/19] Update KG and Maven repos, delete symbol delegate provider, implement working differentiable mst expression based on SFun shape to MST conversion --- build.gradle.kts | 7 ++- examples/build.gradle.kts | 8 --- .../ast/ExpressionsInterpretersBenchmark.kt | 2 +- .../kscience/kmath/ast/KotlingradSupport.kt | 16 +++--- .../kscience/kmath/ast/MstExpression.kt | 25 +++++---- .../kotlin/kscience/kmath/ast/extensions.kt | 12 ---- .../jvmMain/kotlin/kscience/kmath/asm/asm.kt | 3 +- kmath-kotlingrad/build.gradle.kts | 3 +- .../kotlingrad/DifferentiableMstExpression.kt | 53 ++++++++++++++++++ .../kscience/kmath/kotlingrad/KMathNumber.kt | 18 ++++++ .../kmath/kotlingrad/ScalarsAdapters.kt | 56 ++++++++----------- 11 files changed, 125 insertions(+), 78 deletions(-) delete mode 100644 kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/extensions.kt create mode 100644 kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/DifferentiableMstExpression.kt create mode 100644 kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/KMathNumber.kt diff --git a/build.gradle.kts b/build.gradle.kts index 51ce48d84..095697bc4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,10 +9,15 @@ internal val githubProject: String by extra("kmath") allprojects { repositories { jcenter() + maven("https://clojars.org/repo") + maven("https://dl.bintray.com/egor-bogomolov/astminer/") + maven("https://dl.bintray.com/hotkeytlt/maven") maven("https://dl.bintray.com/kotlin/kotlin-eap") maven("https://dl.bintray.com/kotlin/kotlinx") - maven("https://dl.bintray.com/hotkeytlt/maven") + maven("https://dl.bintray.com/mipt-npm/dev") + maven("https://dl.bintray.com/mipt-npm/kscience") maven("https://jitpack.io") + mavenCentral() } group = "kscience.kmath" diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 3ca9bbb47..33018976d 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -8,14 +8,6 @@ plugins { } allOpen.annotation("org.openjdk.jmh.annotations.State") - -repositories { - maven("https://dl.bintray.com/mipt-npm/kscience") - maven("https://dl.bintray.com/mipt-npm/dev") - maven("https://dl.bintray.com/kotlin/kotlin-dev/") - mavenCentral() -} - sourceSets.register("benchmarks") dependencies { diff --git a/examples/src/main/kotlin/kscience/kmath/ast/ExpressionsInterpretersBenchmark.kt b/examples/src/main/kotlin/kscience/kmath/ast/ExpressionsInterpretersBenchmark.kt index b25a61e96..a4806ed68 100644 --- a/examples/src/main/kotlin/kscience/kmath/ast/ExpressionsInterpretersBenchmark.kt +++ b/examples/src/main/kotlin/kscience/kmath/ast/ExpressionsInterpretersBenchmark.kt @@ -13,7 +13,7 @@ internal class ExpressionsInterpretersBenchmark { private val algebra: Field = RealField fun functionalExpression() { val expr = algebra.expressionInField { - variable("x") * const(2.0) + const(2.0) / variable("x") - const(16.0) + symbol("x") * const(2.0) + const(2.0) / symbol("x") - const(16.0) } invokeAndSum(expr) diff --git a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt index c8478a631..7b5e1565d 100644 --- a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt +++ b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt @@ -1,11 +1,9 @@ package kscience.kmath.ast -import edu.umontreal.kotlingrad.experimental.DoublePrecision import kscience.kmath.asm.compile import kscience.kmath.expressions.invoke -import kscience.kmath.kotlingrad.toMst -import kscience.kmath.kotlingrad.toSFun -import kscience.kmath.kotlingrad.toSVar +import kscience.kmath.expressions.symbol +import kscience.kmath.kotlingrad.DifferentiableMstExpression import kscience.kmath.operations.RealField /** @@ -13,10 +11,12 @@ import kscience.kmath.operations.RealField * valid derivative. */ fun main() { - val proto = DoublePrecision.prototype - val x by MstAlgebra.symbol("x").toSVar(proto) - val quadratic = "x^2-4*x-44".parseMath().toSFun(proto) - val actualDerivative = MstExpression(RealField, quadratic.d(x).toMst()).compile() + val x by symbol + + val actualDerivative = DifferentiableMstExpression(RealField, "x^2-4*x-44".parseMath()) + .derivativeOrNull(listOf(x)) + .compile() + val expectedDerivative = MstExpression(RealField, "2*x-4".parseMath()).compile() assert(actualDerivative("x" to 123.0) == expectedDerivative("x" to 123.0)) } diff --git a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstExpression.kt b/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstExpression.kt index 5ca75e993..f68e3f5f8 100644 --- a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstExpression.kt +++ b/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/MstExpression.kt @@ -13,7 +13,7 @@ import kotlin.contracts.contract * @property mst the [MST] node. * @author Alexander Nozik */ -public class MstExpression(public val algebra: Algebra, public val mst: MST) : Expression { +public class MstExpression>(public val algebra: A, public val mst: MST) : Expression { private inner class InnerAlgebra(val arguments: Map) : NumericAlgebra { override fun symbol(value: String): T = arguments[StringSymbol(value)] ?: algebra.symbol(value) override fun unaryOperation(operation: String, arg: T): T = algebra.unaryOperation(operation, arg) @@ -21,8 +21,9 @@ public class MstExpression(public val algebra: Algebra, public val mst: MS override fun binaryOperation(operation: String, left: T, right: T): T = algebra.binaryOperation(operation, left, right) - override fun number(value: Number): T = if (algebra is NumericAlgebra) - algebra.number(value) + @Suppress("UNCHECKED_CAST") + override fun number(value: Number): T = if (algebra is NumericAlgebra<*>) + (algebra as NumericAlgebra).number(value) else error("Numeric nodes are not supported by $this") } @@ -38,14 +39,14 @@ public class MstExpression(public val algebra: Algebra, public val mst: MS public inline fun , E : Algebra> A.mst( mstAlgebra: E, block: E.() -> MST, -): MstExpression = MstExpression(this, mstAlgebra.block()) +): MstExpression = MstExpression(this, mstAlgebra.block()) /** * Builds [MstExpression] over [Space]. * * @author Alexander Nozik */ -public inline fun Space.mstInSpace(block: MstSpace.() -> MST): MstExpression { +public inline fun > A.mstInSpace(block: MstSpace.() -> MST): MstExpression { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return MstExpression(this, MstSpace.block()) } @@ -55,7 +56,7 @@ public inline fun Space.mstInSpace(block: MstSpace.() -> MS * * @author Alexander Nozik */ -public inline fun Ring.mstInRing(block: MstRing.() -> MST): MstExpression { +public inline fun > A.mstInRing(block: MstRing.() -> MST): MstExpression { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return MstExpression(this, MstRing.block()) } @@ -65,7 +66,7 @@ public inline fun Ring.mstInRing(block: MstRing.() -> MST): * * @author Alexander Nozik */ -public inline fun Field.mstInField(block: MstField.() -> MST): MstExpression { +public inline fun > A.mstInField(block: MstField.() -> MST): MstExpression { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return MstExpression(this, MstField.block()) } @@ -75,7 +76,7 @@ public inline fun Field.mstInField(block: MstField.() -> MS * * @author Iaroslav Postovalov */ -public inline fun Field.mstInExtendedField(block: MstExtendedField.() -> MST): MstExpression { +public inline fun > A.mstInExtendedField(block: MstExtendedField.() -> MST): MstExpression { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return MstExpression(this, MstExtendedField.block()) } @@ -85,7 +86,7 @@ public inline fun Field.mstInExtendedField(block: MstExtend * * @author Alexander Nozik */ -public inline fun > FunctionalExpressionSpace.mstInSpace(block: MstSpace.() -> MST): MstExpression { +public inline fun > FunctionalExpressionSpace.mstInSpace(block: MstSpace.() -> MST): MstExpression { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return algebra.mstInSpace(block) } @@ -95,7 +96,7 @@ public inline fun > FunctionalExpressionSpace> FunctionalExpressionRing.mstInRing(block: MstRing.() -> MST): MstExpression { +public inline fun > FunctionalExpressionRing.mstInRing(block: MstRing.() -> MST): MstExpression { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return algebra.mstInRing(block) } @@ -105,7 +106,7 @@ public inline fun > FunctionalExpressionRing. * * @author Alexander Nozik */ -public inline fun > FunctionalExpressionField.mstInField(block: MstField.() -> MST): MstExpression { +public inline fun > FunctionalExpressionField.mstInField(block: MstField.() -> MST): MstExpression { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return algebra.mstInField(block) } @@ -117,7 +118,7 @@ public inline fun > FunctionalExpressionField> FunctionalExpressionExtendedField.mstInExtendedField( block: MstExtendedField.() -> MST, -): MstExpression { +): MstExpression { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return algebra.mstInExtendedField(block) } diff --git a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/extensions.kt b/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/extensions.kt deleted file mode 100644 index b790a3a88..000000000 --- a/kmath-ast/src/commonMain/kotlin/kscience/kmath/ast/extensions.kt +++ /dev/null @@ -1,12 +0,0 @@ -package kscience.kmath.ast - -import kscience.kmath.operations.Algebra -import kotlin.properties.PropertyDelegateProvider -import kotlin.properties.ReadOnlyProperty - -/** - * Returns [PropertyDelegateProvider] providing [ReadOnlyProperty] of [MST.Symbolic] with its value equal to the name - * of the property. - */ -public val Algebra.symbol: PropertyDelegateProvider, ReadOnlyProperty, MST.Symbolic>> - get() = PropertyDelegateProvider { _, _ -> ReadOnlyProperty { _, p -> MST.Symbolic(p.name) } } diff --git a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/asm.kt b/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/asm.kt index 2b6fa6247..9ccfa464c 100644 --- a/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/asm.kt +++ b/kmath-ast/src/jvmMain/kotlin/kscience/kmath/asm/asm.kt @@ -69,4 +69,5 @@ public inline fun Algebra.expression(mst: MST): Expression< * * @author Alexander Nozik. */ -public inline fun MstExpression.compile(): Expression = mst.compileWith(T::class.java, algebra) +public inline fun MstExpression>.compile(): Expression = + mst.compileWith(T::class.java, algebra) diff --git a/kmath-kotlingrad/build.gradle.kts b/kmath-kotlingrad/build.gradle.kts index f2245c3d5..027a03bc9 100644 --- a/kmath-kotlingrad/build.gradle.kts +++ b/kmath-kotlingrad/build.gradle.kts @@ -3,6 +3,7 @@ plugins { } dependencies { - api("com.github.breandan:kotlingrad:0.3.7") + implementation("com.github.breandan:kaliningraph:0.1.2") + implementation("com.github.breandan:kotlingrad:0.3.7") api(project(":kmath-ast")) } diff --git a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/DifferentiableMstExpression.kt b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/DifferentiableMstExpression.kt new file mode 100644 index 000000000..88cc20639 --- /dev/null +++ b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/DifferentiableMstExpression.kt @@ -0,0 +1,53 @@ +package kscience.kmath.kotlingrad + +import edu.umontreal.kotlingrad.experimental.SFun +import kscience.kmath.ast.MST +import kscience.kmath.ast.MstAlgebra +import kscience.kmath.ast.MstExpression +import kscience.kmath.expressions.DifferentiableExpression +import kscience.kmath.expressions.Symbol +import kscience.kmath.operations.NumericAlgebra + +/** + * Represents wrapper of [MstExpression] implementing [DifferentiableExpression]. + * + * The principle of this API is converting the [mst] to an [SFun], differentiating it with Kotlin∇, then converting + * [SFun] back to [MST]. + * + * @param T the type of number. + * @param A the [NumericAlgebra] of [T]. + * @property expr the underlying [MstExpression]. + */ +public inline class DifferentiableMstExpression(public val expr: MstExpression) : + DifferentiableExpression where A : NumericAlgebra, T : Number { + public constructor(algebra: A, mst: MST) : this(MstExpression(algebra, mst)) + + /** + * The [MstExpression.algebra] of [expr]. + */ + public val algebra: A + get() = expr.algebra + + /** + * The [MstExpression.mst] of [expr]. + */ + public val mst: MST + get() = expr.mst + + public override fun invoke(arguments: Map): T = expr(arguments) + + public override fun derivativeOrNull(symbols: List): MstExpression = MstExpression( + algebra, + symbols.map(Symbol::identity) + .map(MstAlgebra::symbol) + .map { it.toSVar>() } + .fold(mst.toSFun(), SFun>::d) + .toMst(), + ) +} + +/** + * Wraps this [MstExpression] into [DifferentiableMstExpression]. + */ +public fun > MstExpression.differentiable(): DifferentiableMstExpression = + DifferentiableMstExpression(this) diff --git a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/KMathNumber.kt b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/KMathNumber.kt new file mode 100644 index 000000000..ce5658137 --- /dev/null +++ b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/KMathNumber.kt @@ -0,0 +1,18 @@ +package kscience.kmath.kotlingrad + +import edu.umontreal.kotlingrad.experimental.RealNumber +import edu.umontreal.kotlingrad.experimental.SConst +import kscience.kmath.operations.NumericAlgebra + +/** + * Implements [RealNumber] by delegating its functionality to [NumericAlgebra]. + * + * @param T the type of number. + * @param A the [NumericAlgebra] of [T]. + * @property algebra the algebra. + * @param value the value of this number. + */ +public class KMathNumber(public val algebra: A, value: T) : + RealNumber, T>(value) where T : Number, A : NumericAlgebra { + public override fun wrap(number: Number): SConst> = SConst(algebra.number(number)) +} diff --git a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt index 24e8377bd..b6effab4b 100644 --- a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt +++ b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/ScalarsAdapters.kt @@ -3,14 +3,26 @@ package kscience.kmath.kotlingrad import edu.umontreal.kotlingrad.experimental.* import kscience.kmath.ast.MST import kscience.kmath.ast.MstAlgebra -import kscience.kmath.ast.MstExpression import kscience.kmath.ast.MstExtendedField import kscience.kmath.ast.MstExtendedField.unaryMinus -import kscience.kmath.expressions.DifferentiableExpression -import kscience.kmath.expressions.Expression -import kscience.kmath.expressions.Symbol import kscience.kmath.operations.* +/** + * Maps [SVar] to [MST.Symbolic] directly. + * + * @receiver the variable. + * @return a node. + */ +public fun > SVar.toMst(): MST.Symbolic = MstAlgebra.symbol(name) + +/** + * Maps [SVar] to [MST.Numeric] directly. + * + * @receiver the constant. + * @return a node. + */ +public fun > SConst.toMst(): MST.Numeric = MstAlgebra.number(doubleValue) + /** * Maps [SFun] objects to [MST]. Some unsupported operations like [Derivative] are bound and converted then. * [Power] operation is limited to constant right-hand side arguments. @@ -37,8 +49,8 @@ import kscience.kmath.operations.* */ public fun > SFun.toMst(): MST = MstExtendedField { when (this@toMst) { - is SVar -> symbol(name) - is SConst -> number(doubleValue) + is SVar -> toMst() + is SConst -> toMst() is Sum -> left.toMst() + right.toMst() is Prod -> left.toMst() * right.toMst() is Power -> left.toMst() pow ((right as? SConst<*>)?.doubleValue ?: (right() as SConst<*>).doubleValue) @@ -69,7 +81,7 @@ public fun > MST.Numeric.toSConst(): SConst = SConst(value) * @param proto the prototype instance. * @return a new variable. */ -public fun > MST.Symbolic.toSVar(): SVar = SVar(value) +internal fun > MST.Symbolic.toSVar(): SVar = SVar(value) /** * Maps [MST] objects to [SFun]. Unsupported operations throw [IllegalStateException]. @@ -90,12 +102,12 @@ public fun > MST.toSFun(): SFun = when (this) { is MST.Symbolic -> toSVar() is MST.Unary -> when (operation) { - SpaceOperations.PLUS_OPERATION -> value.toSFun() - SpaceOperations.MINUS_OPERATION -> (-value).toSFun() + SpaceOperations.PLUS_OPERATION -> +value.toSFun() + SpaceOperations.MINUS_OPERATION -> -value.toSFun() TrigonometricOperations.SIN_OPERATION -> sin(value.toSFun()) TrigonometricOperations.COS_OPERATION -> cos(value.toSFun()) TrigonometricOperations.TAN_OPERATION -> tan(value.toSFun()) - PowerOperations.SQRT_OPERATION -> value.toSFun().sqrt() + PowerOperations.SQRT_OPERATION -> sqrt(value.toSFun()) ExponentialOperations.EXP_OPERATION -> exp(value.toSFun()) ExponentialOperations.LN_OPERATION -> value.toSFun().ln() else -> error("Unary operation $operation not defined in $this") @@ -110,27 +122,3 @@ public fun > MST.toSFun(): SFun = when (this) { else -> error("Binary operation $operation not defined in $this") } } - -public class KMathNumber(public val algebra: A, value: T) : - RealNumber, T>(value) where T : Number, A : NumericAlgebra { - public override fun wrap(number: Number): SConst> = SConst(algebra.number(number)) -} - -public class DifferentiableMstExpression(public val algebra: A, public val mst: MST) : - DifferentiableExpression where A : NumericAlgebra, T : Number { - public val expr by lazy { MstExpression(algebra, mst) } - - public override fun invoke(arguments: Map): T = expr(arguments) - - public override fun derivativeOrNull(orders: Map): Expression { - TODO() - } - - public fun derivativeOrNull(orders: List): Expression { - orders.map { MstAlgebra.symbol(it.identity).toSVar>() } - .fold>, SFun>>(mst.toSFun()) { result, sVar -> result.d(sVar) } - .toMst() - - TODO() - } -} From bc4eb95ae7c05ff957092586d8823d6064dfc21f Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Fri, 30 Oct 2020 16:40:43 +0700 Subject: [PATCH 15/19] Add extension functions for DifferentiableMstExpression --- .../kotlin/kscience/kmath/ast/KotlingradSupport.kt | 3 ++- .../kmath/kotlingrad/DifferentiableMstExpression.kt | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt index 7b5e1565d..9b34426f7 100644 --- a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt +++ b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt @@ -4,6 +4,7 @@ import kscience.kmath.asm.compile import kscience.kmath.expressions.invoke import kscience.kmath.expressions.symbol import kscience.kmath.kotlingrad.DifferentiableMstExpression +import kscience.kmath.kotlingrad.derivative import kscience.kmath.operations.RealField /** @@ -14,7 +15,7 @@ fun main() { val x by symbol val actualDerivative = DifferentiableMstExpression(RealField, "x^2-4*x-44".parseMath()) - .derivativeOrNull(listOf(x)) + .derivative(x) .compile() val expectedDerivative = MstExpression(RealField, "2*x-4".parseMath()).compile() diff --git a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/DifferentiableMstExpression.kt b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/DifferentiableMstExpression.kt index 88cc20639..ddfc7cccb 100644 --- a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/DifferentiableMstExpression.kt +++ b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/DifferentiableMstExpression.kt @@ -5,6 +5,7 @@ import kscience.kmath.ast.MST import kscience.kmath.ast.MstAlgebra import kscience.kmath.ast.MstExpression import kscience.kmath.expressions.DifferentiableExpression +import kscience.kmath.expressions.StringSymbol import kscience.kmath.expressions.Symbol import kscience.kmath.operations.NumericAlgebra @@ -46,6 +47,15 @@ public inline class DifferentiableMstExpression(public val expr: MstExpres ) } +public fun > DifferentiableMstExpression.derivative(symbols: List): MstExpression = + derivativeOrNull(symbols) + +public fun > DifferentiableMstExpression.derivative(vararg symbols: Symbol): MstExpression = + derivative(symbols.toList()) + +public fun > DifferentiableMstExpression.derivative(name: String): MstExpression = + derivative(StringSymbol(name)) + /** * Wraps this [MstExpression] into [DifferentiableMstExpression]. */ From ef7066b8c94d0c9450d2b1a0f1eeb79ffb27fdc9 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Fri, 30 Oct 2020 16:40:58 +0700 Subject: [PATCH 16/19] Update example --- .../src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt index 9b34426f7..5fbb5b86a 100644 --- a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt +++ b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt @@ -5,6 +5,7 @@ import kscience.kmath.expressions.invoke import kscience.kmath.expressions.symbol import kscience.kmath.kotlingrad.DifferentiableMstExpression import kscience.kmath.kotlingrad.derivative +import kscience.kmath.kotlingrad.differentiable import kscience.kmath.operations.RealField /** @@ -14,7 +15,8 @@ import kscience.kmath.operations.RealField fun main() { val x by symbol - val actualDerivative = DifferentiableMstExpression(RealField, "x^2-4*x-44".parseMath()) + val actualDerivative = MstExpression(RealField, "x^2-4*x-44".parseMath()) + .differentiable() .derivative(x) .compile() From d14e4376595670f7ff9148f01d34fccd277f9e2b Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Fri, 30 Oct 2020 16:57:19 +0700 Subject: [PATCH 17/19] Update DifferentiableExpression by providing second type argument representing the result of differentiation --- .../kscience/kmath/ast/KotlingradSupport.kt | 1 - .../DerivativeStructureExpression.kt | 10 ++-- .../expressions/DifferentiableExpression.kt | 34 ++++++----- .../kscience/kmath/expressions/Expression.kt | 4 +- .../kmath/expressions/SimpleAutoDiff.kt | 56 +++++++++---------- .../kotlingrad/DifferentiableMstExpression.kt | 12 +--- 6 files changed, 57 insertions(+), 60 deletions(-) diff --git a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt index 5fbb5b86a..5acd97e3d 100644 --- a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt +++ b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt @@ -3,7 +3,6 @@ package kscience.kmath.ast import kscience.kmath.asm.compile import kscience.kmath.expressions.invoke import kscience.kmath.expressions.symbol -import kscience.kmath.kotlingrad.DifferentiableMstExpression import kscience.kmath.kotlingrad.derivative import kscience.kmath.kotlingrad.differentiable import kscience.kmath.operations.RealField diff --git a/kmath-commons/src/main/kotlin/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt b/kmath-commons/src/main/kotlin/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt index 244dc1314..345babe8b 100644 --- a/kmath-commons/src/main/kotlin/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt +++ b/kmath-commons/src/main/kotlin/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt @@ -95,10 +95,10 @@ public class DerivativeStructureField( public override operator fun Number.plus(b: DerivativeStructure): DerivativeStructure = b + this public override operator fun Number.minus(b: DerivativeStructure): DerivativeStructure = b - this - public companion object : AutoDiffProcessor { - override fun process(function: DerivativeStructureField.() -> DerivativeStructure): DifferentiableExpression { - return DerivativeStructureExpression(function) - } + public companion object : + AutoDiffProcessor> { + public override fun process(function: DerivativeStructureField.() -> DerivativeStructure): DifferentiableExpression> = + DerivativeStructureExpression(function) } } @@ -108,7 +108,7 @@ public class DerivativeStructureField( */ public class DerivativeStructureExpression( public val function: DerivativeStructureField.() -> DerivativeStructure, -) : DifferentiableExpression { +) : DifferentiableExpression> { public override operator fun invoke(arguments: Map): Double = DerivativeStructureField(0, arguments).function().value diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/DifferentiableExpression.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/DifferentiableExpression.kt index ac1f4bc20..890ad5f71 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/DifferentiableExpression.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/DifferentiableExpression.kt @@ -1,29 +1,37 @@ package kscience.kmath.expressions /** - * An expression that provides derivatives + * Represents expression which structure can be differentiated. + * + * @param T the type this expression takes as argument and returns. + * @param R the type of expression this expression can be differentiated to. */ -public interface DifferentiableExpression : Expression { - public fun derivativeOrNull(symbols: List): Expression? +public interface DifferentiableExpression> : Expression { + /** + * Differentiates this expression by ordered collection of [symbols]. + */ + public fun derivativeOrNull(symbols: List): R? } -public fun DifferentiableExpression.derivative(symbols: List): Expression = +public fun > DifferentiableExpression.derivative(symbols: List): R = derivativeOrNull(symbols) ?: error("Derivative by symbols $symbols not provided") -public fun DifferentiableExpression.derivative(vararg symbols: Symbol): Expression = +public fun > DifferentiableExpression.derivative(vararg symbols: Symbol): R = derivative(symbols.toList()) -public fun DifferentiableExpression.derivative(name: String): Expression = +public fun > DifferentiableExpression.derivative(name: String): R = derivative(StringSymbol(name)) /** * A [DifferentiableExpression] that defines only first derivatives */ -public abstract class FirstDerivativeExpression : DifferentiableExpression { +public abstract class FirstDerivativeExpression> : DifferentiableExpression { + /** + * Returns first derivative of this expression by given [symbol]. + */ + public abstract fun derivativeOrNull(symbol: Symbol): R? - public abstract fun derivativeOrNull(symbol: Symbol): Expression? - - public override fun derivativeOrNull(symbols: List): Expression? { + public final override fun derivativeOrNull(symbols: List): R? { val dSymbol = symbols.firstOrNull() ?: return null return derivativeOrNull(dSymbol) } @@ -32,6 +40,6 @@ public abstract class FirstDerivativeExpression : DifferentiableExpression /** * A factory that converts an expression in autodiff variables to a [DifferentiableExpression] */ -public interface AutoDiffProcessor> { - public fun process(function: A.() -> I): DifferentiableExpression -} \ No newline at end of file +public fun interface AutoDiffProcessor, R : Expression> { + public fun process(function: A.() -> I): DifferentiableExpression +} diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/Expression.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/Expression.kt index 568de255e..98940e767 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/Expression.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/Expression.kt @@ -22,7 +22,9 @@ public inline class StringSymbol(override val identity: String) : Symbol { } /** - * An elementary function that could be invoked on a map of arguments + * An elementary function that could be invoked on a map of arguments. + * + * @param T the type this expression takes as argument and returns. */ public fun interface Expression { /** diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/SimpleAutoDiff.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/SimpleAutoDiff.kt index 5a9642690..e8a894d23 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/SimpleAutoDiff.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/SimpleAutoDiff.kt @@ -68,7 +68,7 @@ public fun > F.simpleAutoDiff( ): DerivationResult { contract { callsInPlace(body, InvocationKind.EXACTLY_ONCE) } - return SimpleAutoDiffField(this, bindings).derivate(body) + return SimpleAutoDiffField(this, bindings).differentiate(body) } public fun > F.simpleAutoDiff( @@ -83,12 +83,21 @@ public open class SimpleAutoDiffField>( public val context: F, bindings: Map, ) : Field>, ExpressionAlgebra> { + public override val zero: AutoDiffValue + get() = const(context.zero) + + public override val one: AutoDiffValue + get() = const(context.one) // this stack contains pairs of blocks and values to apply them to private var stack: Array = arrayOfNulls(8) private var sp: Int = 0 private val derivatives: MutableMap, T> = hashMapOf() + private val bindings: Map> = bindings.entries.associate { + it.key.identity to AutoDiffVariableWithDerivative(it.key.identity, it.value, context.zero) + } + /** * Differentiable variable with value and derivative of differentiation ([simpleAutoDiff]) result * with respect to this variable. @@ -106,11 +115,7 @@ public open class SimpleAutoDiffField>( override fun hashCode(): Int = identity.hashCode() } - private val bindings: Map> = bindings.entries.associate { - it.key.identity to AutoDiffVariableWithDerivative(it.key.identity, it.value, context.zero) - } - - override fun bindOrNull(symbol: Symbol): AutoDiffValue? = bindings[symbol.identity] + public override fun bindOrNull(symbol: Symbol): AutoDiffValue? = bindings[symbol.identity] private fun getDerivative(variable: AutoDiffValue): T = (variable as? AutoDiffVariableWithDerivative)?.d ?: derivatives[variable] ?: context.zero @@ -119,7 +124,6 @@ public open class SimpleAutoDiffField>( if (variable is AutoDiffVariableWithDerivative) variable.d = value else derivatives[variable] = value } - @Suppress("UNCHECKED_CAST") private fun runBackwardPass() { while (sp > 0) { @@ -129,9 +133,6 @@ public open class SimpleAutoDiffField>( } } - override val zero: AutoDiffValue get() = const(context.zero) - override val one: AutoDiffValue get() = const(context.one) - override fun const(value: T): AutoDiffValue = AutoDiffValue(value) /** @@ -165,7 +166,7 @@ public open class SimpleAutoDiffField>( } - internal fun derivate(function: SimpleAutoDiffField.() -> AutoDiffValue): DerivationResult { + internal fun differentiate(function: SimpleAutoDiffField.() -> AutoDiffValue): DerivationResult { val result = function() result.d = context.one // computing derivative w.r.t result runBackwardPass() @@ -174,41 +175,41 @@ public open class SimpleAutoDiffField>( // Overloads for Double constants - override operator fun Number.plus(b: AutoDiffValue): AutoDiffValue = + public override operator fun Number.plus(b: AutoDiffValue): AutoDiffValue = derive(const { this@plus.toDouble() * one + b.value }) { z -> b.d += z.d } - override operator fun AutoDiffValue.plus(b: Number): AutoDiffValue = b.plus(this) + public override operator fun AutoDiffValue.plus(b: Number): AutoDiffValue = b.plus(this) - override operator fun Number.minus(b: AutoDiffValue): AutoDiffValue = + public override operator fun Number.minus(b: AutoDiffValue): AutoDiffValue = derive(const { this@minus.toDouble() * one - b.value }) { z -> b.d -= z.d } - override operator fun AutoDiffValue.minus(b: Number): AutoDiffValue = + public override operator fun AutoDiffValue.minus(b: Number): AutoDiffValue = derive(const { this@minus.value - one * b.toDouble() }) { z -> this@minus.d += z.d } // Basic math (+, -, *, /) - override fun add(a: AutoDiffValue, b: AutoDiffValue): AutoDiffValue = + public override fun add(a: AutoDiffValue, b: AutoDiffValue): AutoDiffValue = derive(const { a.value + b.value }) { z -> a.d += z.d b.d += z.d } - override fun multiply(a: AutoDiffValue, b: AutoDiffValue): AutoDiffValue = + public override fun multiply(a: AutoDiffValue, b: AutoDiffValue): AutoDiffValue = derive(const { a.value * b.value }) { z -> a.d += z.d * b.value b.d += z.d * a.value } - override fun divide(a: AutoDiffValue, b: AutoDiffValue): AutoDiffValue = + public override fun divide(a: AutoDiffValue, b: AutoDiffValue): AutoDiffValue = derive(const { a.value / b.value }) { z -> a.d += z.d / b.value b.d -= z.d * a.value / (b.value * b.value) } - override fun multiply(a: AutoDiffValue, k: Number): AutoDiffValue = + public override fun multiply(a: AutoDiffValue, k: Number): AutoDiffValue = derive(const { k.toDouble() * a.value }) { z -> a.d += z.d * k.toDouble() } @@ -220,15 +221,15 @@ public open class SimpleAutoDiffField>( public class SimpleAutoDiffExpression>( public val field: F, public val function: SimpleAutoDiffField.() -> AutoDiffValue, -) : FirstDerivativeExpression() { +) : FirstDerivativeExpression>() { public override operator fun invoke(arguments: Map): T { //val bindings = arguments.entries.map { it.key.bind(it.value) } return SimpleAutoDiffField(field, arguments).function().value } - override fun derivativeOrNull(symbol: Symbol): Expression = Expression { arguments -> + public override fun derivativeOrNull(symbol: Symbol): Expression = Expression { arguments -> //val bindings = arguments.entries.map { it.key.bind(it.value) } - val derivationResult = SimpleAutoDiffField(field, arguments).derivate(function) + val derivationResult = SimpleAutoDiffField(field, arguments).differentiate(function) derivationResult.derivative(symbol) } } @@ -236,13 +237,10 @@ public class SimpleAutoDiffExpression>( /** * Generate [AutoDiffProcessor] for [SimpleAutoDiffExpression] */ -public fun > simpleAutoDiff(field: F): AutoDiffProcessor, SimpleAutoDiffField> { - return object : AutoDiffProcessor, SimpleAutoDiffField> { - override fun process(function: SimpleAutoDiffField.() -> AutoDiffValue): DifferentiableExpression { - return SimpleAutoDiffExpression(field, function) - } +public fun > simpleAutoDiff(field: F): AutoDiffProcessor, SimpleAutoDiffField, Expression> = + AutoDiffProcessor { function -> + SimpleAutoDiffExpression(field, function) } -} // Extensions for differentiation of various basic mathematical functions @@ -392,4 +390,4 @@ public class SimpleAutoDiffExtendedField>( public override fun atanh(arg: AutoDiffValue): AutoDiffValue = (this as SimpleAutoDiffField).atanh(arg) -} \ No newline at end of file +} diff --git a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/DifferentiableMstExpression.kt b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/DifferentiableMstExpression.kt index ddfc7cccb..dd5e46f90 100644 --- a/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/DifferentiableMstExpression.kt +++ b/kmath-kotlingrad/src/main/kotlin/kscience/kmath/kotlingrad/DifferentiableMstExpression.kt @@ -5,7 +5,6 @@ import kscience.kmath.ast.MST import kscience.kmath.ast.MstAlgebra import kscience.kmath.ast.MstExpression import kscience.kmath.expressions.DifferentiableExpression -import kscience.kmath.expressions.StringSymbol import kscience.kmath.expressions.Symbol import kscience.kmath.operations.NumericAlgebra @@ -20,7 +19,7 @@ import kscience.kmath.operations.NumericAlgebra * @property expr the underlying [MstExpression]. */ public inline class DifferentiableMstExpression(public val expr: MstExpression) : - DifferentiableExpression where A : NumericAlgebra, T : Number { + DifferentiableExpression> where A : NumericAlgebra, T : Number { public constructor(algebra: A, mst: MST) : this(MstExpression(algebra, mst)) /** @@ -47,15 +46,6 @@ public inline class DifferentiableMstExpression(public val expr: MstExpres ) } -public fun > DifferentiableMstExpression.derivative(symbols: List): MstExpression = - derivativeOrNull(symbols) - -public fun > DifferentiableMstExpression.derivative(vararg symbols: Symbol): MstExpression = - derivative(symbols.toList()) - -public fun > DifferentiableMstExpression.derivative(name: String): MstExpression = - derivative(StringSymbol(name)) - /** * Wraps this [MstExpression] into [DifferentiableMstExpression]. */ From 658a1703ed77f82ce19d2f7e7df89782e5307890 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Sat, 31 Oct 2020 21:44:52 +0700 Subject: [PATCH 18/19] Add KDoc comment --- .../kscience/kmath/expressions/DifferentiableExpression.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/DifferentiableExpression.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/DifferentiableExpression.kt index 890ad5f71..a15df1ac8 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/DifferentiableExpression.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/DifferentiableExpression.kt @@ -9,6 +9,9 @@ package kscience.kmath.expressions public interface DifferentiableExpression> : Expression { /** * Differentiates this expression by ordered collection of [symbols]. + * + * @param symbols the symbols. + * @return the derivative or `null`. */ public fun derivativeOrNull(symbols: List): R? } From 33d23c8d289784a00707c553f9c33fd7c64186b3 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 2 Nov 2020 01:08:55 +0700 Subject: [PATCH 19/19] Duplicate repositories declared in main build script, fix errors --- README.md | 16 +++++++++++++++ build.gradle.kts | 5 ++++- examples/build.gradle.kts | 14 +++++++++++++ .../kscience/kmath/ast/KotlingradSupport.kt | 2 +- .../optimization/CMOptimizationProblem.kt | 7 +++---- .../kmath/commons/optimization/cmFit.kt | 11 ++++------ .../commons/optimization/OptimizeTest.kt | 13 +++++++----- .../expressions/DifferentiableExpression.kt | 4 ++-- .../kmath/kotlingrad/AdaptingTests.kt | 20 +++++++++---------- .../kotlin/kscience/kmath/stat/Fitting.kt | 10 +++++++--- .../kmath/stat/OptimizationProblem.kt | 13 +++++------- settings.gradle.kts | 3 +-- 12 files changed, 74 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 2df9d3246..c4e3e5374 100644 --- a/README.md +++ b/README.md @@ -211,7 +211,15 @@ Release artifacts are accessible from bintray with following configuration (see ```kotlin repositories { + jcenter() + maven("https://clojars.org/repo") + maven("https://dl.bintray.com/egor-bogomolov/astminer/") + maven("https://dl.bintray.com/hotkeytlt/maven") + maven("https://dl.bintray.com/kotlin/kotlin-eap") + maven("https://dl.bintray.com/kotlin/kotlinx") maven("https://dl.bintray.com/mipt-npm/kscience") + maven("https://jitpack.io") + mavenCentral() } dependencies { @@ -228,7 +236,15 @@ Development builds are uploaded to the separate repository: ```kotlin repositories { + jcenter() + maven("https://clojars.org/repo") + maven("https://dl.bintray.com/egor-bogomolov/astminer/") + maven("https://dl.bintray.com/hotkeytlt/maven") + maven("https://dl.bintray.com/kotlin/kotlin-eap") + maven("https://dl.bintray.com/kotlin/kotlinx") maven("https://dl.bintray.com/mipt-npm/dev") + maven("https://jitpack.io") + mavenCentral() } ``` diff --git a/build.gradle.kts b/build.gradle.kts index 095697bc4..3514c91e6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,5 @@ +import ru.mipt.npm.gradle.KSciencePublishPlugin + plugins { id("ru.mipt.npm.project") } @@ -17,6 +19,7 @@ allprojects { maven("https://dl.bintray.com/mipt-npm/dev") maven("https://dl.bintray.com/mipt-npm/kscience") maven("https://jitpack.io") + maven("http://logicrunch.research.it.uu.se/maven/") mavenCentral() } @@ -25,7 +28,7 @@ allprojects { } subprojects { - if (name.startsWith("kmath")) apply() + if (name.startsWith("kmath")) apply() } readme { diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 99828c621..d42627ff0 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -10,6 +10,20 @@ plugins { allOpen.annotation("org.openjdk.jmh.annotations.State") sourceSets.register("benchmarks") +repositories { + jcenter() + maven("https://clojars.org/repo") + maven("https://dl.bintray.com/egor-bogomolov/astminer/") + maven("https://dl.bintray.com/hotkeytlt/maven") + maven("https://dl.bintray.com/kotlin/kotlin-eap") + maven("https://dl.bintray.com/kotlin/kotlinx") + maven("https://dl.bintray.com/mipt-npm/dev") + maven("https://dl.bintray.com/mipt-npm/kscience") + maven("https://jitpack.io") + maven("http://logicrunch.research.it.uu.se/maven/") + mavenCentral() +} + dependencies { implementation(project(":kmath-ast")) implementation(project(":kmath-kotlingrad")) diff --git a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt index 5acd97e3d..b3c827503 100644 --- a/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt +++ b/examples/src/main/kotlin/kscience/kmath/ast/KotlingradSupport.kt @@ -1,9 +1,9 @@ package kscience.kmath.ast import kscience.kmath.asm.compile +import kscience.kmath.expressions.derivative import kscience.kmath.expressions.invoke import kscience.kmath.expressions.symbol -import kscience.kmath.kotlingrad.derivative import kscience.kmath.kotlingrad.differentiable import kscience.kmath.operations.RealField diff --git a/kmath-commons/src/main/kotlin/kscience/kmath/commons/optimization/CMOptimizationProblem.kt b/kmath-commons/src/main/kotlin/kscience/kmath/commons/optimization/CMOptimizationProblem.kt index 13f9af7bb..d6f79529a 100644 --- a/kmath-commons/src/main/kotlin/kscience/kmath/commons/optimization/CMOptimizationProblem.kt +++ b/kmath-commons/src/main/kotlin/kscience/kmath/commons/optimization/CMOptimizationProblem.kt @@ -19,9 +19,8 @@ import kotlin.reflect.KClass public operator fun PointValuePair.component1(): DoubleArray = point public operator fun PointValuePair.component2(): Double = value -public class CMOptimizationProblem( - override val symbols: List, -) : OptimizationProblem, SymbolIndexer, OptimizationFeature { +public class CMOptimizationProblem(override val symbols: List, ) : + OptimizationProblem, SymbolIndexer, OptimizationFeature { private val optimizationData: HashMap, OptimizationData> = HashMap() private var optimizatorBuilder: (() -> MultivariateOptimizer)? = null public var convergenceChecker: ConvergenceChecker = SimpleValueChecker(DEFAULT_RELATIVE_TOLERANCE, @@ -49,7 +48,7 @@ public class CMOptimizationProblem( addOptimizationData(objectiveFunction) } - public override fun diffExpression(expression: DifferentiableExpression): Unit { + public override fun diffExpression(expression: DifferentiableExpression>) { expression(expression) val gradientFunction = ObjectiveFunctionGradient { val args = it.toMap() diff --git a/kmath-commons/src/main/kotlin/kscience/kmath/commons/optimization/cmFit.kt b/kmath-commons/src/main/kotlin/kscience/kmath/commons/optimization/cmFit.kt index 42475db6c..b8e8bfd4b 100644 --- a/kmath-commons/src/main/kotlin/kscience/kmath/commons/optimization/cmFit.kt +++ b/kmath-commons/src/main/kotlin/kscience/kmath/commons/optimization/cmFit.kt @@ -12,7 +12,6 @@ import kscience.kmath.structures.asBuffer import org.apache.commons.math3.analysis.differentiation.DerivativeStructure import org.apache.commons.math3.optim.nonlinear.scalar.GoalType - /** * Generate a chi squared expression from given x-y-sigma data and inline model. Provides automatic differentiation */ @@ -21,7 +20,7 @@ public fun Fitting.chiSquared( y: Buffer, yErr: Buffer, model: DerivativeStructureField.(x: DerivativeStructure) -> DerivativeStructure, -): DifferentiableExpression = chiSquared(DerivativeStructureField, x, y, yErr, model) +): DifferentiableExpression> = chiSquared(DerivativeStructureField, x, y, yErr, model) /** * Generate a chi squared expression from given x-y-sigma data and inline model. Provides automatic differentiation @@ -31,7 +30,7 @@ public fun Fitting.chiSquared( y: Iterable, yErr: Iterable, model: DerivativeStructureField.(x: DerivativeStructure) -> DerivativeStructure, -): DifferentiableExpression = chiSquared( +): DifferentiableExpression> = chiSquared( DerivativeStructureField, x.toList().asBuffer(), y.toList().asBuffer(), @@ -39,7 +38,6 @@ public fun Fitting.chiSquared( model ) - /** * Optimize expression without derivatives */ @@ -48,16 +46,15 @@ public fun Expression.optimize( configuration: CMOptimizationProblem.() -> Unit, ): OptimizationResult = optimizeWith(CMOptimizationProblem, symbols = symbols, configuration) - /** * Optimize differentiable expression */ -public fun DifferentiableExpression.optimize( +public fun DifferentiableExpression>.optimize( vararg symbols: Symbol, configuration: CMOptimizationProblem.() -> Unit, ): OptimizationResult = optimizeWith(CMOptimizationProblem, symbols = symbols, configuration) -public fun DifferentiableExpression.minimize( +public fun DifferentiableExpression>.minimize( vararg startPoint: Pair, configuration: CMOptimizationProblem.() -> Unit = {}, ): OptimizationResult { diff --git a/kmath-commons/src/test/kotlin/kscience/kmath/commons/optimization/OptimizeTest.kt b/kmath-commons/src/test/kotlin/kscience/kmath/commons/optimization/OptimizeTest.kt index fa1978f95..3290c8f32 100644 --- a/kmath-commons/src/test/kotlin/kscience/kmath/commons/optimization/OptimizeTest.kt +++ b/kmath-commons/src/test/kotlin/kscience/kmath/commons/optimization/OptimizeTest.kt @@ -47,14 +47,17 @@ internal class OptimizeTest { val sigma = 1.0 val generator = Distribution.normal(0.0, sigma) val chain = generator.sample(RandomGenerator.default(112667)) - val x = (1..100).map { it.toDouble() } - val y = x.map { it -> + val x = (1..100).map(Int::toDouble) + + val y = x.map { it.pow(2) + it + 1 + chain.nextDouble() } - val yErr = x.map { sigma } - val chi2 = Fitting.chiSquared(x, y, yErr) { x -> + + val yErr = List(x.size) { sigma } + + val chi2 = Fitting.chiSquared(x, y, yErr) { x1 -> val cWithDefault = bindOrNull(c) ?: one - bind(a) * x.pow(2) + bind(b) * x + cWithDefault + bind(a) * x1.pow(2) + bind(b) * x1 + cWithDefault } val result = chi2.minimize(a to 1.5, b to 0.9, c to 1.0) diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/DifferentiableExpression.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/DifferentiableExpression.kt index a15df1ac8..abce9c4ec 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/DifferentiableExpression.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/expressions/DifferentiableExpression.kt @@ -6,7 +6,7 @@ package kscience.kmath.expressions * @param T the type this expression takes as argument and returns. * @param R the type of expression this expression can be differentiated to. */ -public interface DifferentiableExpression> : Expression { +public interface DifferentiableExpression> : Expression { /** * Differentiates this expression by ordered collection of [symbols]. * @@ -43,6 +43,6 @@ public abstract class FirstDerivativeExpression> : Differen /** * A factory that converts an expression in autodiff variables to a [DifferentiableExpression] */ -public fun interface AutoDiffProcessor, R : Expression> { +public fun interface AutoDiffProcessor, out R : Expression> { public fun process(function: A.() -> I): DifferentiableExpression } diff --git a/kmath-kotlingrad/src/test/kotlin/kscience/kmath/kotlingrad/AdaptingTests.kt b/kmath-kotlingrad/src/test/kotlin/kscience/kmath/kotlingrad/AdaptingTests.kt index 682b0cf2e..77902211b 100644 --- a/kmath-kotlingrad/src/test/kotlin/kscience/kmath/kotlingrad/AdaptingTests.kt +++ b/kmath-kotlingrad/src/test/kotlin/kscience/kmath/kotlingrad/AdaptingTests.kt @@ -13,13 +13,11 @@ import kotlin.test.assertTrue import kotlin.test.fail internal class AdaptingTests { - private val proto: DReal = DoublePrecision.prototype - @Test fun symbol() { val c1 = MstAlgebra.symbol("x") - assertTrue(c1.toSVar(proto).name == "x") - val c2 = "kitten".parseMath().toSFun(proto) + assertTrue(c1.toSVar>().name == "x") + val c2 = "kitten".parseMath().toSFun>() if (c2 is SVar) assertTrue(c2.name == "kitten") else fail() } @@ -27,15 +25,15 @@ internal class AdaptingTests { fun number() { val c1 = MstAlgebra.number(12354324) assertTrue(c1.toSConst().doubleValue == 12354324.0) - val c2 = "0.234".parseMath().toSFun(proto) + val c2 = "0.234".parseMath().toSFun>() if (c2 is SConst) assertTrue(c2.doubleValue == 0.234) else fail() - val c3 = "1e-3".parseMath().toSFun(proto) + val c3 = "1e-3".parseMath().toSFun>() if (c3 is SConst) assertEquals(0.001, c3.value) else fail() } @Test fun simpleFunctionShape() { - val linear = "2*x+16".parseMath().toSFun(proto) + val linear = "2*x+16".parseMath().toSFun>() if (linear !is Sum) fail() if (linear.left !is Prod) fail() if (linear.right !is SConst) fail() @@ -43,8 +41,8 @@ internal class AdaptingTests { @Test fun simpleFunctionDerivative() { - val x = MstAlgebra.symbol("x").toSVar(proto) - val quadratic = "x^2-4*x-44".parseMath().toSFun(proto) + val x = MstAlgebra.symbol("x").toSVar>() + val quadratic = "x^2-4*x-44".parseMath().toSFun>() val actualDerivative = MstExpression(RealField, quadratic.d(x).toMst()).compile() val expectedDerivative = MstExpression(RealField, "2*x-4".parseMath()).compile() assertEquals(actualDerivative("x" to 123.0), expectedDerivative("x" to 123.0)) @@ -52,8 +50,8 @@ internal class AdaptingTests { @Test fun moreComplexDerivative() { - val x = MstAlgebra.symbol("x").toSVar(proto) - val composition = "-sqrt(sin(x^2)-cos(x)^2-16*x)".parseMath().toSFun(proto) + val x = MstAlgebra.symbol("x").toSVar>() + val composition = "-sqrt(sin(x^2)-cos(x)^2-16*x)".parseMath().toSFun>() val actualDerivative = MstExpression(RealField, composition.d(x).toMst()).compile() val expectedDerivative = MstExpression( diff --git a/kmath-stat/src/commonMain/kotlin/kscience/kmath/stat/Fitting.kt b/kmath-stat/src/commonMain/kotlin/kscience/kmath/stat/Fitting.kt index 01fdf4c5e..9d4655df2 100644 --- a/kmath-stat/src/commonMain/kotlin/kscience/kmath/stat/Fitting.kt +++ b/kmath-stat/src/commonMain/kotlin/kscience/kmath/stat/Fitting.kt @@ -12,16 +12,18 @@ public object Fitting { * Generate a chi squared expression from given x-y-sigma data and inline model. Provides automatic differentiation */ public fun chiSquared( - autoDiff: AutoDiffProcessor, + autoDiff: AutoDiffProcessor>, x: Buffer, y: Buffer, yErr: Buffer, model: A.(I) -> I, - ): DifferentiableExpression where A : ExtendedField, A : ExpressionAlgebra { + ): DifferentiableExpression> where A : ExtendedField, A : ExpressionAlgebra { require(x.size == y.size) { "X and y buffers should be of the same size" } require(y.size == yErr.size) { "Y and yErr buffer should of the same size" } + return autoDiff.process { var sum = zero + x.indices.forEach { val xValue = const(x[it]) val yValue = const(y[it]) @@ -29,6 +31,7 @@ public object Fitting { val modelValue = model(xValue) sum += ((yValue - modelValue) / yErrValue).pow(2) } + sum } } @@ -45,6 +48,7 @@ public object Fitting { ): Expression { require(x.size == y.size) { "X and y buffers should be of the same size" } require(y.size == yErr.size) { "Y and yErr buffer should of the same size" } + return Expression { arguments -> x.indices.sumByDouble { val xValue = x[it] @@ -56,4 +60,4 @@ public object Fitting { } } } -} \ No newline at end of file +} diff --git a/kmath-stat/src/commonMain/kotlin/kscience/kmath/stat/OptimizationProblem.kt b/kmath-stat/src/commonMain/kotlin/kscience/kmath/stat/OptimizationProblem.kt index ea522bff9..0f3cd9dd9 100644 --- a/kmath-stat/src/commonMain/kotlin/kscience/kmath/stat/OptimizationProblem.kt +++ b/kmath-stat/src/commonMain/kotlin/kscience/kmath/stat/OptimizationProblem.kt @@ -27,17 +27,17 @@ public interface OptimizationProblem { /** * Define the initial guess for the optimization problem */ - public fun initialGuess(map: Map): Unit + public fun initialGuess(map: Map) /** * Set an objective function expression */ - public fun expression(expression: Expression): Unit + public fun expression(expression: Expression) /** * Set a differentiable expression as objective function as function and gradient provider */ - public fun diffExpression(expression: DifferentiableExpression): Unit + public fun diffExpression(expression: DifferentiableExpression>) /** * Update the problem from previous optimization run @@ -50,9 +50,8 @@ public interface OptimizationProblem { public fun optimize(): OptimizationResult } -public interface OptimizationProblemFactory> { +public fun interface OptimizationProblemFactory> { public fun build(symbols: List): P - } public operator fun > OptimizationProblemFactory.invoke( @@ -60,7 +59,6 @@ public operator fun > OptimizationProblemFac block: P.() -> Unit, ): P = build(symbols).apply(block) - /** * Optimize expression without derivatives using specific [OptimizationProblemFactory] */ @@ -78,7 +76,7 @@ public fun > Expression.optimizeWith( /** * Optimize differentiable expression using specific [OptimizationProblemFactory] */ -public fun > DifferentiableExpression.optimizeWith( +public fun > DifferentiableExpression>.optimizeWith( factory: OptimizationProblemFactory, vararg symbols: Symbol, configuration: F.() -> Unit, @@ -88,4 +86,3 @@ public fun > DifferentiableExpression.op problem.diffExpression(this) return problem.optimize() } - diff --git a/settings.gradle.kts b/settings.gradle.kts index e825ddbdf..97dfe1b96 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,8 +1,7 @@ pluginManagement { repositories { - mavenLocal() - jcenter() gradlePluginPortal() + jcenter() maven("https://dl.bintray.com/kotlin/kotlin-eap") maven("https://dl.bintray.com/mipt-npm/kscience") maven("https://dl.bintray.com/mipt-npm/dev")