Add average and averageWith space extensions

This commit is contained in:
Iaroslav 2020-08-10 01:42:11 +07:00
parent 6329722131
commit 71c4ee0655
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7

View File

@ -4,7 +4,7 @@ package scientifik.kmath.operations
* Returns the sum of all elements in the iterable in this [Space]. * Returns the sum of all elements in the iterable in this [Space].
* *
* @receiver the algebra that provides addition. * @receiver the algebra that provides addition.
* @param data the collection to sum up. * @param data the iterable to sum up.
* @return the sum. * @return the sum.
*/ */
fun <T> Space<T>.sum(data: Iterable<T>): T = data.fold(zero) { left, right -> add(left, right) } fun <T> Space<T>.sum(data: Iterable<T>): T = data.fold(zero) { left, right -> add(left, right) }
@ -13,11 +13,29 @@ fun <T> Space<T>.sum(data: Iterable<T>): T = data.fold(zero) { left, right -> ad
* Returns the sum of all elements in the sequence in this [Space]. * Returns the sum of all elements in the sequence in this [Space].
* *
* @receiver the algebra that provides addition. * @receiver the algebra that provides addition.
* @param data the collection to sum up. * @param data the sequence to sum up.
* @return the sum. * @return the sum.
*/ */
fun <T> Space<T>.sum(data: Sequence<T>): T = data.fold(zero) { left, right -> add(left, right) } fun <T> Space<T>.sum(data: Sequence<T>): T = data.fold(zero) { left, right -> add(left, right) }
/**
* Returns an average value of elements in the iterable in this [Space].
*
* @receiver the algebra that provides addition and division.
* @param data the iterable to find average.
* @return the average value.
*/
fun <T> Space<T>.average(data: Iterable<T>): T = sum(data) / data.count()
/**
* Returns an average value of elements in the sequence in this [Space].
*
* @receiver the algebra that provides addition and division.
* @param data the sequence to find average.
* @return the average value.
*/
fun <T> Space<T>.average(data: Sequence<T>): T = sum(data) / data.count()
/** /**
* Returns the sum of all elements in the iterable in provided space. * Returns the sum of all elements in the iterable in provided space.
* *
@ -25,7 +43,34 @@ fun <T> Space<T>.sum(data: Sequence<T>): T = data.fold(zero) { left, right -> ad
* @param space the algebra that provides addition. * @param space the algebra that provides addition.
* @return the sum. * @return the sum.
*/ */
fun <T : Any, S : Space<T>> Iterable<T>.sumWith(space: S): T = space.sum(this) fun <T> Iterable<T>.sumWith(space: Space<T>): T = space.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.
* @return the sum.
*/
fun <T> Sequence<T>.sumWith(space: Space<T>): T = space.sum(this)
/**
* Returns an average value of elements in the iterable in this [Space].
*
* @receiver the iterable to find average.
* @param space the algebra that provides addition and division.
* @return the average value.
*/
fun <T> Iterable<T>.averageWith(space: Space<T>): T = space.average(this)
/**
* Returns an average value of elements in the sequence in this [Space].
*
* @receiver the sequence to find average.
* @param space the algebra that provides addition and division.
* @return the average value.
*/
fun <T> Sequence<T>.averageWith(space: Space<T>): T = space.average(this)
//TODO optimized power operation //TODO optimized power operation