From 381137724dc6e6fbb3795b80a988dd64d2578d11 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Tue, 13 Oct 2020 19:47:07 +0700 Subject: [PATCH] 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" )