diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/LabeledPolynomial.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/LabeledPolynomial.kt index 29aeb6bb0..3d1b91f7b 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/LabeledPolynomial.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/LabeledPolynomial.kt @@ -54,59 +54,86 @@ internal constructor( */ internal fun Map.cleanUp() = filterValues { it > 0U } -//context(LabeledPolynomialSpace>) -//@Suppress("FunctionName") -//internal fun LabeledPolynomial(coefs: Map, C>, toCheckInput: Boolean = false) : LabeledPolynomial { -// if (!toCheckInput) return LabeledPolynomial(coefs) -// -// val fixedCoefs = mutableMapOf, C>() -// -// for (entry in coefs) { -// val key = entry.key.cleanUp() -// val value = entry.value -// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value -// } -// -// return LabeledPolynomial( -// fixedCoefs.filterValues { it.isNotZero() } -// ) -//} -// -//context(LabeledPolynomialSpace>) -//@Suppress("FunctionName") -//internal fun LabeledPolynomial(pairs: Collection, C>>, toCheckInput: Boolean = false) : LabeledPolynomial { -// if (!toCheckInput) return LabeledPolynomial(pairs.toMap()) -// -// val fixedCoefs = mutableMapOf, C>() -// -// for (entry in pairs) { -// val key = entry.first.cleanUp() -// val value = entry.second -// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value -// } -// -// return LabeledPolynomial( -// fixedCoefs.filterValues { it.isNotZero() } -// ) -//} -// -//// TODO: Do not know how to make it without context receivers -//context(LabeledPolynomialSpace>) -//@Suppress("FunctionName") -//public fun LabeledPolynomial(coefs: Map, C>) : LabeledPolynomial = LabeledPolynomial(coefs, toCheckInput = true) -// -//context(LabeledPolynomialSpace>) -//@Suppress("FunctionName") -//public fun LabeledPolynomial(pairs: Collection, C>>) : LabeledPolynomial = LabeledPolynomial(pairs, toCheckInput = true) -// -//context(LabeledPolynomialSpace>) -//@Suppress("FunctionName") -//public fun LabeledPolynomial(vararg pairs: Pair, C>) : LabeledPolynomial = LabeledPolynomial(pairs.toList(), toCheckInput = true) -// -//context(LabeledPolynomialSpace>) -//public fun Symbol.asLabeledPolynomial() : LabeledPolynomial = LabeledPolynomial(mapOf(mapOf(this to 1u) to constantOne)) +// Waiting for context receivers :( TODO: Replace with context receivers when they will be available -public fun C.asLabeledPolynomial() : LabeledPolynomial = LabeledPolynomial(mapOf(emptyMap() to this)) +@Suppress("FunctionName", "NOTHING_TO_INLINE") +internal inline fun > LabeledPolynomialSpace.LabeledPolynomial(coefs: Map, C>, toCheckInput: Boolean = true) : LabeledPolynomial = ring.LabeledPolynomial(coefs, toCheckInput) +@Suppress("FunctionName") +internal fun > A.LabeledPolynomial(coefs: Map, C>, toCheckInput: Boolean = true) : LabeledPolynomial { + if (!toCheckInput) return LabeledPolynomial(coefs) + + val fixedCoefs = mutableMapOf, C>() + + for (entry in coefs) { + val key = entry.key.cleanUp() + val value = entry.value + fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value + } + + return LabeledPolynomial( + fixedCoefs.filterValues { it != zero } + ) +} + +@Suppress("FunctionName", "NOTHING_TO_INLINE") +internal inline fun > LabeledPolynomialSpace.LabeledPolynomial(pairs: Collection, C>>, toCheckInput: Boolean = true) : LabeledPolynomial = ring.LabeledPolynomial(pairs, toCheckInput) +@Suppress("FunctionName") +internal fun > A.LabeledPolynomial(pairs: Collection, C>>, toCheckInput: Boolean = true) : LabeledPolynomial { + if (!toCheckInput) return LabeledPolynomial(pairs.toMap()) + + val fixedCoefs = mutableMapOf, C>() + + for (entry in pairs) { + val key = entry.first.cleanUp() + val value = entry.second + fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value + } + + return LabeledPolynomial( + fixedCoefs.filterValues { it != zero } + ) +} + +@Suppress("FunctionName", "NOTHING_TO_INLINE") +internal inline fun > LabeledPolynomialSpace.LabeledPolynomial(vararg pairs: Pair, C>, toCheckInput: Boolean = true) : LabeledPolynomial = ring.LabeledPolynomial(pairs = pairs, toCheckInput = toCheckInput) +@Suppress("FunctionName") +internal fun > A.LabeledPolynomial(vararg pairs: Pair, C>, toCheckInput: Boolean = true) : LabeledPolynomial { + if (!toCheckInput) return LabeledPolynomial(pairs.toMap()) + + val fixedCoefs = mutableMapOf, C>() + + for (entry in pairs) { + val key = entry.first.cleanUp() + val value = entry.second + fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value + } + + return LabeledPolynomial( + fixedCoefs.filterValues { it != zero } + ) +} + +@Suppress("FunctionName") +public fun > A.LabeledPolynomial(coefs: Map, C>) : LabeledPolynomial = LabeledPolynomial(coefs, toCheckInput = true) +@Suppress("FunctionName") +public fun > LabeledPolynomialSpace.LabeledPolynomial(coefs: Map, C>) : LabeledPolynomial = LabeledPolynomial(coefs, toCheckInput = true) + +@Suppress("FunctionName") +public fun > A.LabeledPolynomial(pairs: Collection, C>>) : LabeledPolynomial = LabeledPolynomial(pairs, toCheckInput = true) +@Suppress("FunctionName") +public fun > LabeledPolynomialSpace.LabeledPolynomial(pairs: Collection, C>>) : LabeledPolynomial = LabeledPolynomial(pairs, toCheckInput = true) + +@Suppress("FunctionName") +public fun > A.LabeledPolynomial(vararg pairs: Pair, C>) : LabeledPolynomial = LabeledPolynomial(*pairs, toCheckInput = true) +@Suppress("FunctionName") +public fun > LabeledPolynomialSpace.LabeledPolynomial(vararg pairs: Pair, C>) : LabeledPolynomial = LabeledPolynomial(*pairs, toCheckInput = true) + +//context(A) +//public fun > Symbol.asLabeledPolynomial() : LabeledPolynomial = LabeledPolynomial(mapOf(mapOf(this to 1u) to one)) +//context(LabeledPolynomialSpace) +//public fun > Symbol.asLabeledPolynomial() : LabeledPolynomial = LabeledPolynomial(mapOf(mapOf(this to 1u) to constantOne)) + +public fun C.asLabeledPolynomial() : LabeledPolynomial = LabeledPolynomial(mapOf(emptyMap() to this)) /** * Space of polynomials. diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedPolynomial.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedPolynomial.kt index 570ccce8e..411409e39 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedPolynomial.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/functions/NumberedPolynomial.kt @@ -53,56 +53,86 @@ internal constructor( */ internal fun List.cleanUp() = subList(0, indexOfLast { it != 0U } + 1) -//context(NumberedPolynomialSpace>) -//@Suppress("FunctionName") -//internal fun NumberedPolynomial(coefs: Map, C>, toCheckInput: Boolean = false) : NumberedPolynomial { -// if (!toCheckInput) return NumberedPolynomial(coefs) -// -// val fixedCoefs = mutableMapOf, C>() -// -// for (entry in coefs) { -// val key = entry.key.cleanUp() -// val value = entry.value -// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value -// } -// -// return NumberedPolynomial( -// fixedCoefs.filterValues { it.isNotZero() } -// ) -//} -// -//context(NumberedPolynomialSpace>) -//@Suppress("FunctionName") -//internal fun NumberedPolynomial(pairs: Collection, C>>, toCheckInput: Boolean = false) : NumberedPolynomial { -// if (!toCheckInput) return NumberedPolynomial(pairs.toMap()) -// -// val fixedCoefs = mutableMapOf, C>() -// -// for (entry in pairs) { -// val key = entry.first.cleanUp() -// val value = entry.second -// fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value -// } -// -// return NumberedPolynomial( -// fixedCoefs.filterValues { it.isNotZero() } -// ) -//} -// -//// TODO: Do not know how to make it without context receivers -//context(NumberedPolynomialSpace>) -//@Suppress("FunctionName") -//public fun NumberedPolynomial(coefs: Map, C>) : NumberedPolynomial = NumberedPolynomial(coefs, toCheckInput = true) -// -//context(NumberedPolynomialSpace>) -//@Suppress("FunctionName") -//public fun NumberedPolynomial(pairs: Collection, C>>) : NumberedPolynomial = NumberedPolynomial(pairs, toCheckInput = true) -// -//context(NumberedPolynomialSpace>) -//@Suppress("FunctionName") -//public fun NumberedPolynomial(vararg pairs: Pair, C>) : NumberedPolynomial = NumberedPolynomial(pairs.toList(), toCheckInput = true) +// Waiting for context receivers :( TODO: Replace with context receivers when they will be available -public fun C.asNumberedPolynomial() : NumberedPolynomial = NumberedPolynomial(mapOf(emptyList() to this)) +@Suppress("FunctionName", "NOTHING_TO_INLINE") +internal inline fun > NumberedPolynomialSpace.NumberedPolynomial(coefs: Map, C>, toCheckInput: Boolean = true) : NumberedPolynomial = ring.NumberedPolynomial(coefs, toCheckInput) +@Suppress("FunctionName") +internal fun > A.NumberedPolynomial(coefs: Map, C>, toCheckInput: Boolean = true) : NumberedPolynomial { + if (!toCheckInput) return NumberedPolynomial(coefs) + + val fixedCoefs = mutableMapOf, C>() + + for (entry in coefs) { + val key = entry.key.cleanUp() + val value = entry.value + fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value + } + + return NumberedPolynomial( + fixedCoefs.filterValues { it != zero } + ) +} + +@Suppress("FunctionName", "NOTHING_TO_INLINE") +internal inline fun > NumberedPolynomialSpace.NumberedPolynomial(pairs: Collection, C>>, toCheckInput: Boolean = true) : NumberedPolynomial = ring.NumberedPolynomial(pairs, toCheckInput) +@Suppress("FunctionName") +internal fun > A.NumberedPolynomial(pairs: Collection, C>>, toCheckInput: Boolean = true) : NumberedPolynomial { + if (!toCheckInput) return NumberedPolynomial(pairs.toMap()) + + val fixedCoefs = mutableMapOf, C>() + + for (entry in pairs) { + val key = entry.first.cleanUp() + val value = entry.second + fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value + } + + return NumberedPolynomial( + fixedCoefs.filterValues { it != zero } + ) +} + +@Suppress("FunctionName", "NOTHING_TO_INLINE") +internal inline fun > NumberedPolynomialSpace.NumberedPolynomial(vararg pairs: Pair, C>, toCheckInput: Boolean = true) : NumberedPolynomial = ring.NumberedPolynomial(pairs = pairs, toCheckInput = toCheckInput) +@Suppress("FunctionName") +internal fun > A.NumberedPolynomial(vararg pairs: Pair, C>, toCheckInput: Boolean = true) : NumberedPolynomial { + if (!toCheckInput) return NumberedPolynomial(pairs.toMap()) + + val fixedCoefs = mutableMapOf, C>() + + for (entry in pairs) { + val key = entry.first.cleanUp() + val value = entry.second + fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value + } + + return NumberedPolynomial( + fixedCoefs.filterValues { it != zero } + ) +} + +@Suppress("FunctionName") +public fun > A.NumberedPolynomial(coefs: Map, C>) : NumberedPolynomial = NumberedPolynomial(coefs, toCheckInput = true) +@Suppress("FunctionName") +public fun > NumberedPolynomialSpace.NumberedPolynomial(coefs: Map, C>) : NumberedPolynomial = NumberedPolynomial(coefs, toCheckInput = true) + +@Suppress("FunctionName") +public fun > A.NumberedPolynomial(pairs: Collection, C>>) : NumberedPolynomial = NumberedPolynomial(pairs, toCheckInput = true) +@Suppress("FunctionName") +public fun > NumberedPolynomialSpace.NumberedPolynomial(pairs: Collection, C>>) : NumberedPolynomial = NumberedPolynomial(pairs, toCheckInput = true) + +@Suppress("FunctionName") +public fun > A.NumberedPolynomial(vararg pairs: Pair, C>) : NumberedPolynomial = NumberedPolynomial(*pairs, toCheckInput = true) +@Suppress("FunctionName") +public fun > NumberedPolynomialSpace.NumberedPolynomial(vararg pairs: Pair, C>) : NumberedPolynomial = NumberedPolynomial(*pairs, toCheckInput = true) + +//context(A) +//public fun > Symbol.asNumberedPolynomial() : NumberedPolynomial = NumberedPolynomial(mapOf(mapOf(this to 1u) to one)) +//context(NumberedPolynomialSpace) +//public fun > Symbol.asNumberedPolynomial() : NumberedPolynomial = NumberedPolynomial(mapOf(mapOf(this to 1u) to constantOne)) + +public fun C.asNumberedPolynomial() : NumberedPolynomial = NumberedPolynomial(mapOf(emptyList() to this)) /** * Space of polynomials. @@ -574,47 +604,4 @@ public open class NumberedPolynomialSpace>( for ((degs, c) in this) if (c.isZero()) this.remove(degs) } } - - @Suppress("FunctionName") - internal fun NumberedPolynomial(coefs: Map, C>, toCheckInput: Boolean = false) : NumberedPolynomial { - if (!toCheckInput) return NumberedPolynomial(coefs) - - val fixedCoefs = mutableMapOf, C>() - - for (entry in coefs) { - val key = entry.key.cleanUp() - val value = entry.value - fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value - } - - return NumberedPolynomial( - fixedCoefs.filterValues { it.isNotZero() } - ) - } - - @Suppress("FunctionName") - internal fun NumberedPolynomial(pairs: Collection, C>>, toCheckInput: Boolean = false) : NumberedPolynomial { - if (!toCheckInput) return NumberedPolynomial(pairs.toMap()) - - val fixedCoefs = mutableMapOf, C>() - - for (entry in pairs) { - val key = entry.first.cleanUp() - val value = entry.second - fixedCoefs[key] = if (key in fixedCoefs) fixedCoefs[key]!! + value else value - } - - return NumberedPolynomial( - fixedCoefs.filterValues { it.isNotZero() } - ) - } - - @Suppress("FunctionName") - public fun NumberedPolynomial(coefs: Map, C>) : NumberedPolynomial = NumberedPolynomial(coefs, toCheckInput = true) - - @Suppress("FunctionName") - public fun NumberedPolynomial(pairs: Collection, C>>) : NumberedPolynomial = NumberedPolynomial(pairs, toCheckInput = true) - - @Suppress("FunctionName") - public fun NumberedPolynomial(vararg pairs: Pair, C>) : NumberedPolynomial = NumberedPolynomial(pairs.toList(), toCheckInput = true) } \ No newline at end of file