This commit is contained in:
Iaroslav Postovalov 2021-03-12 19:03:56 +07:00
parent f449bdd58f
commit 993bba3133
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
5 changed files with 22 additions and 21 deletions

View File

@ -37,8 +37,8 @@ public fun <T, R> List<T>.cumulative(initial: R, operation: (R, T) -> R): List<R
/** /**
* Cumulative sum with custom space * Cumulative sum with custom space
*/ */
public fun <T> Iterable<T>.cumulativeSum(space: Group<T>): Iterable<T> = public fun <T> Iterable<T>.cumulativeSum(group: Group<T>): Iterable<T> =
space { cumulative(zero) { element: T, sum: T -> sum + element } } group { cumulative(zero) { element: T, sum: T -> sum + element } }
@JvmName("cumulativeSumOfDouble") @JvmName("cumulativeSumOfDouble")
public fun Iterable<Double>.cumulativeSum(): Iterable<Double> = cumulative(0.0) { element, sum -> sum + element } 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") @JvmName("cumulativeSumOfLong")
public fun Iterable<Long>.cumulativeSum(): Iterable<Long> = cumulative(0L) { element, sum -> sum + element } public fun Iterable<Long>.cumulativeSum(): Iterable<Long> = cumulative(0L) { element, sum -> sum + element }
public fun <T> Sequence<T>.cumulativeSum(space: Group<T>): Sequence<T> = public fun <T> Sequence<T>.cumulativeSum(group: Group<T>): Sequence<T> =
space { cumulative(zero) { element: T, sum: T -> sum + element } } group { cumulative(zero) { element: T, sum: T -> sum + element } }
@JvmName("cumulativeSumOfDouble") @JvmName("cumulativeSumOfDouble")
public fun Sequence<Double>.cumulativeSum(): Sequence<Double> = cumulative(0.0) { element, sum -> sum + element } 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") @JvmName("cumulativeSumOfLong")
public fun Sequence<Long>.cumulativeSum(): Sequence<Long> = cumulative(0L) { element, sum -> sum + element } public fun Sequence<Long>.cumulativeSum(): Sequence<Long> = cumulative(0L) { element, sum -> sum + element }
public fun <T> List<T>.cumulativeSum(space: Group<T>): List<T> = public fun <T> List<T>.cumulativeSum(group: Group<T>): List<T> =
space { cumulative(zero) { element: T, sum: T -> sum + element } } group { cumulative(zero) { element: T, sum: T -> sum + element } }
@JvmName("cumulativeSumOfDouble") @JvmName("cumulativeSumOfDouble")
public fun List<Double>.cumulativeSum(): List<Double> = cumulative(0.0) { element, sum -> sum + element } public fun List<Double>.cumulativeSum(): List<Double> = cumulative(0.0) { element, sum -> sum + element }

View File

@ -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. * Returns the sum of all elements in the iterable in provided space.
* *
* @receiver the collection to sum up. * @receiver the collection to sum up.
* @param space the algebra that provides addition. * @param group the algebra that provides addition.
* @return the sum. * @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. * Returns the sum of all elements in the sequence in provided space.
* *
* @receiver the collection to sum up. * @receiver the collection to sum up.
* @param space the algebra that provides addition. * @param group the algebra that provides addition.
* @return the sum. * @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]. * Returns an average value of elements in the iterable in this [Group].
@ -71,7 +71,7 @@ public fun <T> Sequence<T>.sumWith(space: Group<T>): T = space.sum(this)
* @return the average value. * @return the average value.
* @author Iaroslav Postovalov * @author Iaroslav Postovalov
*/ */
public fun <T, S> Iterable<T>.averageWith(space: S): T where S : Group<T>, S : ScaleOperations<T> = public fun <T, S> Iterable<T>.averageWith(space: S): T where S : Group<T>, S : ScaleOperations<T> =
space.average(this) space.average(this)
/** /**
@ -82,7 +82,7 @@ public fun <T, S> Iterable<T>.averageWith(space: S): T where S : Group<T>, S :
* @return the average value. * @return the average value.
* @author Iaroslav Postovalov * @author Iaroslav Postovalov
*/ */
public fun <T, S> Sequence<T>.averageWith(space: S): T where S : Group<T>, S : ScaleOperations<T> = public fun <T, S> Sequence<T>.averageWith(space: S): T where S : Group<T>, S : ScaleOperations<T> =
space.average(this) space.average(this)
//TODO optimized power operation //TODO optimized power operation

View File

@ -14,7 +14,7 @@ public fun <T> Flow<T>.cumulativeSum(group: GroupOperations<T>): Flow<T> =
group { runningReduce { sum, element -> sum + element } } group { runningReduce { sum, element -> sum + element } }
@ExperimentalCoroutinesApi @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) data class Accumulator(var sum: T, var num: Int)
scan(Accumulator(zero, 0)) { sum, element -> scan(Accumulator(zero, 0)) { sum, element ->

View File

@ -11,7 +11,8 @@ import space.kscience.kmath.operations.RealField
public interface Counter<T : Any> { public interface Counter<T : Any> {
public fun add(delta: T) public fun add(delta: T)
public val value: T public val value: T
public companion object{
public companion object {
public fun real(): ObjectCounter<Double> = ObjectCounter(RealField) public fun real(): ObjectCounter<Double> = ObjectCounter(RealField)
} }
} }
@ -36,11 +37,11 @@ public class LongCounter : Counter<Long> {
override val value: Long get() = innerValue.value override val value: Long get() = innerValue.value
} }
public class ObjectCounter<T : Any>(public val space: Group<T>) : Counter<T> { public class ObjectCounter<T : Any>(public val group: Group<T>) : Counter<T> {
private val innerValue = atomic(space.zero) private val innerValue = atomic(group.zero)
override fun add(delta: T) { override fun add(delta: T) {
innerValue.getAndUpdate { space.run { it + delta } } innerValue.getAndUpdate { group.run { it + delta } }
} }
override val value: T get() = innerValue.value override val value: T get() = innerValue.value

View File

@ -66,16 +66,16 @@ public fun <T, I, R> ComposableStatistic<T, I, R>.flow(
* Arithmetic mean * Arithmetic mean
*/ */
public class Mean<T>( public class Mean<T>(
private val space: Group<T>, private val group: Group<T>,
private val division: (sum: T, count: Int) -> T, private val division: (sum: T, count: Int) -> T,
) : ComposableStatistic<T, Pair<T, Int>, T> { ) : ComposableStatistic<T, Pair<T, Int>, T> {
public override suspend fun computeIntermediate(data: Buffer<T>): Pair<T, Int> = 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> = 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) division(intermediate.first, intermediate.second)
} }