From 993bba3133581498865ed71cbe22a303a879b58e Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Fri, 12 Mar 2021 19:03:56 +0700 Subject: [PATCH] Fix #226 --- .../kotlin/space/kscience/kmath/misc/cumulative.kt | 12 ++++++------ .../kscience/kmath/operations/algebraExtensions.kt | 12 ++++++------ .../kotlin/space/kscience/kmath/chains/flowExtra.kt | 2 +- .../kotlin/space/kscience/kmath/histogram/Counter.kt | 9 +++++---- .../kotlin/space/kscience/kmath/stat/Statistic.kt | 8 ++++---- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/cumulative.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/cumulative.kt index b50e095cf..6466695a6 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/cumulative.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/cumulative.kt @@ -37,8 +37,8 @@ public fun List.cumulative(initial: R, operation: (R, T) -> R): List Iterable.cumulativeSum(space: Group): Iterable = - space { cumulative(zero) { element: T, sum: T -> sum + element } } +public fun Iterable.cumulativeSum(group: Group): Iterable = + group { cumulative(zero) { element: T, sum: T -> sum + element } } @JvmName("cumulativeSumOfDouble") public fun Iterable.cumulativeSum(): Iterable = cumulative(0.0) { element, sum -> sum + element } @@ -49,8 +49,8 @@ public fun Iterable.cumulativeSum(): Iterable = cumulative(0) { elemen @JvmName("cumulativeSumOfLong") public fun Iterable.cumulativeSum(): Iterable = cumulative(0L) { element, sum -> sum + element } -public fun Sequence.cumulativeSum(space: Group): Sequence = - space { cumulative(zero) { element: T, sum: T -> sum + element } } +public fun Sequence.cumulativeSum(group: Group): Sequence = + group { cumulative(zero) { element: T, sum: T -> sum + element } } @JvmName("cumulativeSumOfDouble") public fun Sequence.cumulativeSum(): Sequence = cumulative(0.0) { element, sum -> sum + element } @@ -61,8 +61,8 @@ public fun Sequence.cumulativeSum(): Sequence = cumulative(0) { elemen @JvmName("cumulativeSumOfLong") public fun Sequence.cumulativeSum(): Sequence = cumulative(0L) { element, sum -> sum + element } -public fun List.cumulativeSum(space: Group): List = - space { cumulative(zero) { element: T, sum: T -> sum + element } } +public fun List.cumulativeSum(group: Group): List = + group { cumulative(zero) { element: T, sum: T -> sum + element } } @JvmName("cumulativeSumOfDouble") public fun List.cumulativeSum(): List = cumulative(0.0) { element, sum -> sum + element } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/algebraExtensions.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/algebraExtensions.kt index 52a0dec6f..b927655e3 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/algebraExtensions.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/algebraExtensions.kt @@ -49,19 +49,19 @@ public fun > Group.abs(value: T): T = if (value > zero) val * Returns the sum of all elements in the iterable in provided space. * * @receiver the collection to sum up. - * @param space the algebra that provides addition. + * @param group the algebra that provides addition. * @return the sum. */ -public fun Iterable.sumWith(space: Group): T = space.sum(this) +public fun Iterable.sumWith(group: Group): T = group.sum(this) /** * Returns the sum of all elements in the sequence in provided space. * * @receiver the collection to sum up. - * @param space the algebra that provides addition. + * @param group the algebra that provides addition. * @return the sum. */ -public fun Sequence.sumWith(space: Group): T = space.sum(this) +public fun Sequence.sumWith(group: Group): T = group.sum(this) /** * Returns an average value of elements in the iterable in this [Group]. @@ -71,7 +71,7 @@ public fun Sequence.sumWith(space: Group): T = space.sum(this) * @return the average value. * @author Iaroslav Postovalov */ -public fun Iterable.averageWith(space: S): T where S : Group, S : ScaleOperations = +public fun Iterable.averageWith(space: S): T where S : Group, S : ScaleOperations = space.average(this) /** @@ -82,7 +82,7 @@ public fun Iterable.averageWith(space: S): T where S : Group, S : * @return the average value. * @author Iaroslav Postovalov */ -public fun Sequence.averageWith(space: S): T where S : Group, S : ScaleOperations = +public fun Sequence.averageWith(space: S): T where S : Group, S : ScaleOperations = space.average(this) //TODO optimized power operation diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/flowExtra.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/flowExtra.kt index 81b4327fc..7d4914a01 100644 --- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/flowExtra.kt +++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/flowExtra.kt @@ -14,7 +14,7 @@ public fun Flow.cumulativeSum(group: GroupOperations): Flow = group { runningReduce { sum, element -> sum + element } } @ExperimentalCoroutinesApi -public fun Flow.mean(algebra: S): Flow where S : Group, S : ScaleOperations = algebra { +public fun Flow.mean(space: S): Flow where S : Group, S : ScaleOperations = space { data class Accumulator(var sum: T, var num: Int) scan(Accumulator(zero, 0)) { sum, element -> diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt index d5f3965d9..5f79fdab8 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt @@ -11,7 +11,8 @@ import space.kscience.kmath.operations.RealField public interface Counter { public fun add(delta: T) public val value: T - public companion object{ + + public companion object { public fun real(): ObjectCounter = ObjectCounter(RealField) } } @@ -36,11 +37,11 @@ public class LongCounter : Counter { override val value: Long get() = innerValue.value } -public class ObjectCounter(public val space: Group) : Counter { - private val innerValue = atomic(space.zero) +public class ObjectCounter(public val group: Group) : Counter { + private val innerValue = atomic(group.zero) override fun add(delta: T) { - innerValue.getAndUpdate { space.run { it + delta } } + innerValue.getAndUpdate { group.run { it + delta } } } override val value: T get() = innerValue.value diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Statistic.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Statistic.kt index a9f7cd3e4..6fe0fee9b 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Statistic.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Statistic.kt @@ -66,16 +66,16 @@ public fun ComposableStatistic.flow( * Arithmetic mean */ public class Mean( - private val space: Group, + private val group: Group, private val division: (sum: T, count: Int) -> T, ) : ComposableStatistic, T> { public override suspend fun computeIntermediate(data: Buffer): Pair = - space { sum(data.asIterable()) } to data.size + group { sum(data.asIterable()) } to data.size public override suspend fun composeIntermediate(first: Pair, second: Pair): Pair = - space { first.first + second.first } to (first.second + second.second) + group { first.first + second.first } to (first.second + second.second) - public override suspend fun toResult(intermediate: Pair): T = space { + public override suspend fun toResult(intermediate: Pair): T = group { division(intermediate.first, intermediate.second) }