Skip to content

Commit c97a973

Browse files
committed
added BigInteger to mean()
1 parent 6b4e9d0 commit c97a973

File tree

1 file changed

+16
-0
lines changed
  • core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/math

1 file changed

+16
-0
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/math/mean.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.jetbrains.kotlinx.dataframe.math
33
import org.jetbrains.kotlinx.dataframe.api.skipNA_default
44
import org.jetbrains.kotlinx.dataframe.impl.renderType
55
import java.math.BigDecimal
6+
import java.math.BigInteger
67
import kotlin.reflect.KType
78
import kotlin.reflect.full.withNullability
89

@@ -28,6 +29,8 @@ internal fun <T : Number> Sequence<T>.mean(type: KType, skipNA: Boolean = skipNA
2829

2930
Long::class -> (this as Sequence<Long>).map { it.toDouble() }.mean(false)
3031

32+
BigInteger::class -> (this as Sequence<BigInteger>).map { it.toDouble() }.mean(false)
33+
3134
BigDecimal::class -> (this as Sequence<BigDecimal>).map { it.toDouble() }.mean(skipNA)
3235

3336
Number::class -> (this as Sequence<Number>).map { it.toDouble() }.mean(skipNA)
@@ -132,6 +135,19 @@ public fun Iterable<Long>.mean(): Double =
132135
if (count > 0) sum / count else Double.NaN
133136
}
134137

138+
@JvmName("bigIntegerMean")
139+
public fun Iterable<BigInteger>.mean(): Double =
140+
if (this is Collection) {
141+
if (size > 0) sumOf { it.toDouble() } / size else Double.NaN
142+
} else {
143+
var count = 0
144+
val sum = sumOf {
145+
count++
146+
it.toDouble()
147+
}
148+
if (count > 0) sum / count else Double.NaN
149+
}
150+
135151
@JvmName("bigDecimalMean")
136152
public fun Iterable<BigDecimal>.mean(): Double =
137153
if (this is Collection) {

0 commit comments

Comments
 (0)