forked from kscience/kmath
commit
defaf716d7
@ -37,8 +37,8 @@ public fun <T, R> List<T>.cumulative(initial: R, operation: (R, T) -> R): List<R
|
||||
/**
|
||||
* Cumulative sum with custom space
|
||||
*/
|
||||
public fun <T> Iterable<T>.cumulativeSum(space: Group<T>): Iterable<T> =
|
||||
space { cumulative(zero) { element: T, sum: T -> sum + element } }
|
||||
public fun <T> Iterable<T>.cumulativeSum(group: Group<T>): Iterable<T> =
|
||||
group { cumulative(zero) { element: T, sum: T -> sum + element } }
|
||||
|
||||
@JvmName("cumulativeSumOfDouble")
|
||||
public fun Iterable<Double>.cumulativeSum(): Iterable<Double> = cumulative(0.0) { element, sum -> sum + element }
|
||||
@ -49,8 +49,8 @@ public fun Iterable<Int>.cumulativeSum(): Iterable<Int> = cumulative(0) { elemen
|
||||
@JvmName("cumulativeSumOfLong")
|
||||
public fun Iterable<Long>.cumulativeSum(): Iterable<Long> = cumulative(0L) { element, sum -> sum + element }
|
||||
|
||||
public fun <T> Sequence<T>.cumulativeSum(space: Group<T>): Sequence<T> =
|
||||
space { cumulative(zero) { element: T, sum: T -> sum + element } }
|
||||
public fun <T> Sequence<T>.cumulativeSum(group: Group<T>): Sequence<T> =
|
||||
group { cumulative(zero) { element: T, sum: T -> sum + element } }
|
||||
|
||||
@JvmName("cumulativeSumOfDouble")
|
||||
public fun Sequence<Double>.cumulativeSum(): Sequence<Double> = cumulative(0.0) { element, sum -> sum + element }
|
||||
@ -61,8 +61,8 @@ public fun Sequence<Int>.cumulativeSum(): Sequence<Int> = cumulative(0) { elemen
|
||||
@JvmName("cumulativeSumOfLong")
|
||||
public fun Sequence<Long>.cumulativeSum(): Sequence<Long> = cumulative(0L) { element, sum -> sum + element }
|
||||
|
||||
public fun <T> List<T>.cumulativeSum(space: Group<T>): List<T> =
|
||||
space { cumulative(zero) { element: T, sum: T -> sum + element } }
|
||||
public fun <T> List<T>.cumulativeSum(group: Group<T>): List<T> =
|
||||
group { cumulative(zero) { element: T, sum: T -> sum + element } }
|
||||
|
||||
@JvmName("cumulativeSumOfDouble")
|
||||
public fun List<Double>.cumulativeSum(): List<Double> = cumulative(0.0) { element, sum -> sum + element }
|
||||
|
@ -49,19 +49,19 @@ public fun <T : Comparable<T>> Group<T>.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 <T> Iterable<T>.sumWith(space: Group<T>): T = space.sum(this)
|
||||
public fun <T> Iterable<T>.sumWith(group: Group<T>): 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 <T> Sequence<T>.sumWith(space: Group<T>): T = space.sum(this)
|
||||
public fun <T> Sequence<T>.sumWith(group: Group<T>): T = group.sum(this)
|
||||
|
||||
/**
|
||||
* Returns an average value of elements in the iterable in this [Group].
|
||||
|
@ -14,7 +14,7 @@ public fun <T> Flow<T>.cumulativeSum(group: GroupOperations<T>): Flow<T> =
|
||||
group { runningReduce { sum, element -> sum + element } }
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
public fun <T, S> Flow<T>.mean(algebra: S): Flow<T> where S : Group<T>, S : ScaleOperations<T> = algebra {
|
||||
public fun <T, S> Flow<T>.mean(space: S): Flow<T> where S : Group<T>, S : ScaleOperations<T> = space {
|
||||
data class Accumulator(var sum: T, var num: Int)
|
||||
|
||||
scan(Accumulator(zero, 0)) { sum, element ->
|
||||
|
@ -37,11 +37,11 @@ public class LongCounter : Counter<Long> {
|
||||
override val value: Long get() = innerValue.value
|
||||
}
|
||||
|
||||
public class ObjectCounter<T : Any>(public val space: Group<T>) : Counter<T> {
|
||||
private val innerValue = atomic(space.zero)
|
||||
public class ObjectCounter<T : Any>(public val group: Group<T>) : Counter<T> {
|
||||
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
|
||||
|
@ -66,16 +66,16 @@ public fun <T, I, R> ComposableStatistic<T, I, R>.flow(
|
||||
* Arithmetic mean
|
||||
*/
|
||||
public class Mean<T>(
|
||||
private val space: Group<T>,
|
||||
private val group: Group<T>,
|
||||
private val division: (sum: T, count: Int) -> T,
|
||||
) : ComposableStatistic<T, Pair<T, Int>, T> {
|
||||
public override suspend fun computeIntermediate(data: Buffer<T>): Pair<T, Int> =
|
||||
space { sum(data.asIterable()) } to data.size
|
||||
group { sum(data.asIterable()) } to data.size
|
||||
|
||||
public override suspend fun composeIntermediate(first: Pair<T, Int>, second: Pair<T, Int>): Pair<T, Int> =
|
||||
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, Int>): T = space {
|
||||
public override suspend fun toResult(intermediate: Pair<T, Int>): T = group {
|
||||
division(intermediate.first, intermediate.second)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user