Skip to content

Commit 2ef56b4

Browse files
committed
expanded std with BigInteger and Number
1 parent 4d4ebda commit 2ef56b4

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

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

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

@@ -26,6 +27,8 @@ internal fun <T : Number> Iterable<T?>.std(
2627
Int::class, Short::class, Byte::class -> (this as Iterable<Int>).std(ddof)
2728
Long::class -> (this as Iterable<Long>).std(ddof)
2829
BigDecimal::class -> (this as Iterable<BigDecimal>).std(ddof)
30+
BigInteger::class -> (this as Iterable<BigInteger>).std(ddof)
31+
Number::class -> (this as Iterable<Number>).map { it.toDouble() }.std(skipNA, ddof)
2932
Nothing::class -> Double.NaN
3033
else -> throw IllegalArgumentException("Unable to compute the std for type ${renderType(type)}")
3134
}
@@ -47,3 +50,6 @@ public fun Iterable<Long>.std(ddof: Int = ddof_default): Double = varianceAndMea
4750

4851
@JvmName("bigDecimalStd")
4952
public fun Iterable<BigDecimal>.std(ddof: Int = ddof_default): Double = varianceAndMean().std(ddof)
53+
54+
@JvmName("bigIntegerStd")
55+
public fun Iterable<BigInteger>.std(ddof: Int = ddof_default): Double = varianceAndMean().std(ddof)

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package org.jetbrains.kotlinx.dataframe.math
44

55
import org.jetbrains.kotlinx.dataframe.api.skipNA_default
66
import java.math.BigDecimal
7+
import java.math.BigInteger
78
import kotlin.math.sqrt
89

910
public data class BasicStats(val count: Int, val mean: Double, val variance: Double) {
@@ -114,3 +115,20 @@ public fun Iterable<BigDecimal>.varianceAndMean(): BasicStats {
114115
}
115116
return BasicStats(count, mean.toDouble(), variance.toDouble())
116117
}
118+
119+
@JvmName("bigIntegerVarianceAndMean")
120+
public fun Iterable<BigInteger>.varianceAndMean(): BasicStats {
121+
var count = 0
122+
var sum = BigInteger.ZERO
123+
for (element in this) {
124+
sum += element
125+
count++
126+
}
127+
val mean = sum.toDouble() / count
128+
var variance = .0
129+
for (element in this) {
130+
val diff = element.toDouble() - mean
131+
variance += diff * diff
132+
}
133+
return BasicStats(count, mean, variance)
134+
}

0 commit comments

Comments
 (0)