From 4ea29c82c5abcc079123bd964f77ac4ea0e307a8 Mon Sep 17 00:00:00 2001 From: Gleb Minaev <43728100+lounres@users.noreply.github.com> Date: Wed, 13 Jul 2022 12:05:53 +0300 Subject: [PATCH] Small fix of DSL1. --- .../kscience/kmath/functions/polynomials.kt | 4 +- .../labeledConstructors.kt | 40 +++++++++---------- .../numberedConstructors.kt | 36 ++++++++--------- 3 files changed, 40 insertions(+), 40 deletions(-) diff --git a/examples/src/main/kotlin/space/kscience/kmath/functions/polynomials.kt b/examples/src/main/kotlin/space/kscience/kmath/functions/polynomials.kt index 4151b0283..c65ca589d 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/functions/polynomials.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/functions/polynomials.kt @@ -107,8 +107,8 @@ fun numberedPolynomialsExample() { // Also there is DSL for constructing NumberedPolynomials: val polynomial5: NumberedPolynomial = NumberedPolynomialDSL1 { 3 {} - 5 { 2 inPowerOf 1u } - -7 with { 1 pow 2u; 3 pow 1u } + 5 { 1 inPowerOf 1u } + -7 with { 0 pow 2u; 2 pow 1u } // `pow` and `inPowerOf` are the same // `with` is omittable } diff --git a/kmath-polynomial/src/commonMain/kotlin/space.kscience.kmath.functions/labeledConstructors.kt b/kmath-polynomial/src/commonMain/kotlin/space.kscience.kmath.functions/labeledConstructors.kt index d23efc5c2..aa44660c1 100644 --- a/kmath-polynomial/src/commonMain/kotlin/space.kscience.kmath.functions/labeledConstructors.kt +++ b/kmath-polynomial/src/commonMain/kotlin/space.kscience.kmath.functions/labeledConstructors.kt @@ -265,7 +265,7 @@ public inline fun C.asLabeledPolynomial() : LabeledPolynomial = LabeledPo * For example, polynomial \(5 a^2 c^3 - 6 b\) can be described as * ``` * Int.algebra { - * val numberedPolynomial : NumberedPolynomial = NumberedPolynomial { + * val labeledPolynomial : LabeledPolynomial = LabeledPolynomialDSL1 { * 5 { a inPowerOf 2u; c inPowerOf 3u } // 5 a^2 c^3 + * (-6) { b inPowerOf 1u } // (-6) b^1 * } @@ -339,18 +339,18 @@ public class DSL1LabeledPolynomialBuilder( /** * Initial capacity of coefficients map. */ - initialCapacity: Int = 0 + initialCapacity: Int? = null ) { /** * Coefficients storage. Any declaration of any monomial updates the storage. * Afterward the storage will be used as a resulting coefficients map. */ - private val coefficients: MutableMap, C> = LinkedHashMap(initialCapacity) + private val coefficients: MutableMap, C> = if (initialCapacity != null) LinkedHashMap(initialCapacity) else LinkedHashMap() /** * Builds the resulting coefficients map. * - * In fact, it just returns [coefficients] as regular coefficients map of type `Map, C>`. + * In fact, it just returns [coefficients] as regular coefficients map of type `Map, C>`. */ @PublishedApi internal fun build(): LabeledPolynomial = LabeledPolynomial(coefficients) @@ -386,12 +386,12 @@ public class DSL1LabeledPolynomialBuilder( ///** // * Creates [LabeledPolynomial] with lambda [block] in context of [this] ring of constants. // * -// * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as +// * For example, polynomial \(5 a^2 c^3 - 6 b\) can be described as // * ``` // * Int.algebra { -// * val LabeledPolynomial : LabeledPolynomial = LabeledPolynomial { -// * 5 { 1 inPowerOf 2u; 3 inPowerOf 3u } // 5 x_1^2 x_3^3 + -// * (-6) { 2 inPowerOf 1u } // (-6) x_2^1 +// * val labeledPolynomial : LabeledPolynomial = LabeledPolynomialDSL1 { +// * 5 { a inPowerOf 2u; c inPowerOf 3u } // 5 a^2 c^3 + +// * (-6) { b inPowerOf 1u } // (-6) b^1 // * } // * } // * ``` @@ -402,39 +402,39 @@ public class DSL1LabeledPolynomialBuilder( // 2. Union types are implemented. Then all three functions should be rewritten // as one with single union type as a (context) receiver. //@UnstableKMathAPI -//public inline fun > A.LabeledPolynomialDSL1(initialCapacity: Int = 0, block: LabeledPolynomialBuilder.() -> Unit) : LabeledPolynomial = LabeledPolynomialBuilder(::add, initialCapacity).apply(block).build() +//public inline fun > A.LabeledPolynomialDSL1(initialCapacity: Int? = null, block: LabeledPolynomialBuilder.() -> Unit) : LabeledPolynomial = LabeledPolynomialBuilder(::add, initialCapacity).apply(block).build() /** * Creates [LabeledPolynomial] with lambda [block] in context of [this] ring of [LabeledPolynomial]s. * - * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as + * For example, polynomial \(5 a^2 c^3 - 6 b\) can be described as * ``` * Int.algebra { - * val LabeledPolynomial : LabeledPolynomial = LabeledPolynomial { - * 5 { 1 inPowerOf 2u; 3 inPowerOf 3u } // 5 x_1^2 x_3^3 + - * (-6) { 2 inPowerOf 1u } // (-6) x_2^1 + * val labeledPolynomial : LabeledPolynomial = LabeledPolynomialDSL1 { + * 5 { a inPowerOf 2u; c inPowerOf 3u } // 5 a^2 c^3 + + * (-6) { b inPowerOf 1u } // (-6) b^1 * } * } * ``` * @usesMathJax */ @UnstableKMathAPI -public inline fun > LabeledPolynomialSpace.LabeledPolynomialDSL1(initialCapacity: Int = 0, block: DSL1LabeledPolynomialBuilder.() -> Unit) : LabeledPolynomial = DSL1LabeledPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build() +public inline fun > LabeledPolynomialSpace.LabeledPolynomialDSL1(initialCapacity: Int? = null, block: DSL1LabeledPolynomialBuilder.() -> Unit) : LabeledPolynomial = DSL1LabeledPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build() /** * Creates [LabeledPolynomial] with lambda [block] in context of [this] field of [LabeledRationalFunction]s. * - * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as - * `` + * For example, polynomial \(5 a^2 c^3 - 6 b\) can be described as + * ``` * Int.algebra { - * val LabeledPolynomial : LabeledPolynomial = LabeledPolynomial { - * 5 { 1 inPowerOf 2u; 3 inPowerOf 3u } // 5 x_1^2 x_3^3 + - * (-6) { 2 inPowerOf 1u } // (-6) x_2^1 + * val labeledPolynomial : LabeledPolynomial = LabeledPolynomialDSL1 { + * 5 { a inPowerOf 2u; c inPowerOf 3u } // 5 a^2 c^3 + + * (-6) { b inPowerOf 1u } // (-6) b^1 * } * } * ``` * @usesMathJax */ @UnstableKMathAPI -public inline fun > LabeledRationalFunctionSpace.LabeledPolynomialDSL1(initialCapacity: Int = 0, block: DSL1LabeledPolynomialBuilder.() -> Unit) : LabeledPolynomial = DSL1LabeledPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build() +public inline fun > LabeledRationalFunctionSpace.LabeledPolynomialDSL1(initialCapacity: Int? = null, block: DSL1LabeledPolynomialBuilder.() -> Unit) : LabeledPolynomial = DSL1LabeledPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build() // Waiting for context receivers :( FIXME: Replace with context receivers when they will be available diff --git a/kmath-polynomial/src/commonMain/kotlin/space.kscience.kmath.functions/numberedConstructors.kt b/kmath-polynomial/src/commonMain/kotlin/space.kscience.kmath.functions/numberedConstructors.kt index 051019159..ce0db3d17 100644 --- a/kmath-polynomial/src/commonMain/kotlin/space.kscience.kmath.functions/numberedConstructors.kt +++ b/kmath-polynomial/src/commonMain/kotlin/space.kscience.kmath.functions/numberedConstructors.kt @@ -243,12 +243,12 @@ public inline fun C.asNumberedPolynomial() : NumberedPolynomial = Numbere /** * Marks DSL that allows to more simply create [NumberedPolynomial]s with good performance. * - * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as + * For example, polynomial \(5 x_0^2 x_2^3 - 6 x_1\) can be described as * ``` * Int.algebra { * val numberedPolynomial : NumberedPolynomial = NumberedPolynomial { - * 5 { 1 inPowerOf 2u; 3 inPowerOf 3u } // 5 x_1^2 x_3^3 + - * (-6) { 2 inPowerOf 1u } // (-6) x_2^1 + * 5 { 0 inPowerOf 2u; 2 inPowerOf 3u } // 5 x_0^2 x_2^3 + + * (-6) { 1 inPowerOf 1u } // (-6) x_1^1 * } * } * ``` @@ -285,7 +285,7 @@ public class DSL1NumberedPolynomialTermSignatureBuilder { */ public infix fun Int.inPowerOf(deg: UInt) { if (deg == 0u) return - val index = this - 1 + val index = this if (index > signature.lastIndex) { signature.addAll(List(index - signature.lastIndex - 1) { 0u }) signature.add(deg) @@ -326,13 +326,13 @@ public class DSL1NumberedPolynomialBuilder( /** * Initial capacity of coefficients map. */ - initialCapacity: Int = 0 + initialCapacity: Int? = null ) { /** * Coefficients storage. Any declaration of any monomial updates the storage. * Afterward the storage will be used as a resulting coefficients map. */ - private val coefficients: MutableMap, C> = LinkedHashMap(initialCapacity) + private val coefficients: MutableMap, C> = if (initialCapacity != null) LinkedHashMap(initialCapacity) else LinkedHashMap() /** * Builds the resulting coefficients map. @@ -373,12 +373,12 @@ public class DSL1NumberedPolynomialBuilder( ///** // * Creates [NumberedPolynomial] with lambda [block] in context of [this] ring of constants. // * -// * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as +// * For example, polynomial \(5 x_0^2 x_2^3 - 6 x_1\) can be described as // * ``` // * Int.algebra { // * val numberedPolynomial : NumberedPolynomial = NumberedPolynomial { -// * 5 { 1 inPowerOf 2u; 3 inPowerOf 3u } // 5 x_1^2 x_3^3 + -// * (-6) { 2 inPowerOf 1u } // (-6) x_2^1 +// * 5 { 0 inPowerOf 2u; 2 inPowerOf 3u } // 5 x_0^2 x_2^3 + +// * (-6) { 1 inPowerOf 1u } // (-6) x_1^1 // * } // * } // * ``` @@ -389,39 +389,39 @@ public class DSL1NumberedPolynomialBuilder( // 2. Union types are implemented. Then all three functions should be rewritten // as one with single union type as a (context) receiver. //@UnstableKMathAPI -//public inline fun > A.NumberedPolynomialDSL1(initialCapacity: Int = 0, block: NumberedPolynomialBuilder.() -> Unit) : NumberedPolynomial = NumberedPolynomialBuilder(::add, initialCapacity).apply(block).build() +//public inline fun > A.NumberedPolynomialDSL1(initialCapacity: Int? = null, block: NumberedPolynomialBuilder.() -> Unit) : NumberedPolynomial = NumberedPolynomialBuilder(::add, initialCapacity).apply(block).build() /** * Creates [NumberedPolynomial] with lambda [block] in context of [this] ring of [NumberedPolynomial]s. * - * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as + * For example, polynomial \(5 x_0^2 x_2^3 - 6 x_1\) can be described as * ``` * Int.algebra { * val numberedPolynomial : NumberedPolynomial = NumberedPolynomial { - * 5 { 1 inPowerOf 2u; 3 inPowerOf 3u } // 5 x_1^2 x_3^3 + - * (-6) { 2 inPowerOf 1u } // (-6) x_2^1 + * 5 { 0 inPowerOf 2u; 2 inPowerOf 3u } // 5 x_0^2 x_2^3 + + * (-6) { 1 inPowerOf 1u } // (-6) x_1^1 * } * } * ``` * @usesMathJax */ @UnstableKMathAPI -public inline fun > NumberedPolynomialSpace.NumberedPolynomialDSL1(initialCapacity: Int = 0, block: DSL1NumberedPolynomialBuilder.() -> Unit) : NumberedPolynomial = DSL1NumberedPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build() +public inline fun > NumberedPolynomialSpace.NumberedPolynomialDSL1(initialCapacity: Int? = null, block: DSL1NumberedPolynomialBuilder.() -> Unit) : NumberedPolynomial = DSL1NumberedPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build() /** * Creates [NumberedPolynomial] with lambda [block] in context of [this] field of [NumberedRationalFunction]s. * - * For example, polynomial \(5 x_1^2 x_3^3 - 6 x_2\) can be described as + * For example, polynomial \(5 x_0^2 x_2^3 - 6 x_1\) can be described as * ``` * Int.algebra { * val numberedPolynomial : NumberedPolynomial = NumberedPolynomial { - * 5 { 1 inPowerOf 2u; 3 inPowerOf 3u } // 5 x_1^2 x_3^3 + - * (-6) { 2 inPowerOf 1u } // (-6) x_2^1 + * 5 { 0 inPowerOf 2u; 2 inPowerOf 3u } // 5 x_0^2 x_2^3 + + * (-6) { 1 inPowerOf 1u } // (-6) x_1^1 * } * } * ``` * @usesMathJax */ @UnstableKMathAPI -public inline fun > NumberedRationalFunctionSpace.NumberedPolynomialDSL1(initialCapacity: Int = 0, block: DSL1NumberedPolynomialBuilder.() -> Unit) : NumberedPolynomial = DSL1NumberedPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build() +public inline fun > NumberedRationalFunctionSpace.NumberedPolynomialDSL1(initialCapacity: Int? = null, block: DSL1NumberedPolynomialBuilder.() -> Unit) : NumberedPolynomial = DSL1NumberedPolynomialBuilder({ left: C, right: C -> left + right }, initialCapacity).apply(block).build() // Waiting for context receivers :( FIXME: Replace with context receivers when they will be available