diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/FastHistogram.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/FastHistogram.kt index 37f46b1d7..8b30e7ebf 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/FastHistogram.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/FastHistogram.kt @@ -8,23 +8,6 @@ private operator fun RealPoint.minus(other: RealPoint) = ListBuffer((0 until siz private inline fun Buffer.mapIndexed(crossinline mapper: (Int, Double) -> T): Sequence = (0 until size).asSequence().map { mapper(it, get(it)) } - -//class MultivariateBin(override val center: RealPoint, val sizes: RealPoint, var counter: Long = 0) : Bin { -// init { -// if (center.size != sizes.size) error("Dimension mismatch in bin creation. Expected ${center.size}, but found ${sizes.size}") -// } -// -// override fun contains(vector: Buffer): Boolean { -// if (vector.size != center.size) error("Dimension mismatch for input vector. Expected ${center.size}, but found ${vector.size}") -// return vector.mapIndexed { i, value -> value in (center[i] - sizes[i] / 2)..(center[i] + sizes[i] / 2) }.all { it } -// } -// -// override val value get() = counter -// internal operator fun inc() = this.also { counter++ } -// -// override val dimension: Int get() = center.size -//} - /** * Uniform multivariate histogram with fixed borders. Based on NDStructure implementation with complexity of m for bin search, where m is the number of dimensions. */ @@ -109,18 +92,17 @@ class FastHistogram( /** * Convert this histogram into NDStructure containing bin values but not bin descriptions */ - fun asND(): NDStructure { + fun asNDStructure(): NDStructure { return ndStructure(this.values.shape) { values[it].sum() } } -// /** -// * Create a phantom lightweight immutable copy of this histogram -// */ -// fun asPhantom(): PhantomHistogram { -// val center = -// val binTemplates = bins.associate { (index, bin) -> BinTemplate(bin.center, bin.sizes) to index } -// return PhantomHistogram(binTemplates, asND()) -// } + /** + * Create a phantom lightweight immutable copy of this histogram + */ + fun asPhantomHistogram(): PhantomHistogram { + val binTemplates = values.associate { (index, _) -> getTemplate(index) to index } + return PhantomHistogram(binTemplates, asNDStructure()) + } companion object { @@ -148,8 +130,8 @@ class FastHistogram( */ fun fromRanges(vararg ranges: Pair, Int>): FastHistogram { return FastHistogram( - ranges.map { it.first.start }.toVector(), - ranges.map { it.first.endInclusive }.toVector(), + ListBuffer(ranges.map { it.first.start }), + ListBuffer(ranges.map { it.first.endInclusive }), ranges.map { it.second }.toIntArray() ) } diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt index 86110f140..08214142e 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt @@ -53,6 +53,7 @@ interface MutableHistogram>: Histogram{ fun MutableHistogram.put(vararg point: T) = put(ArrayBuffer(point)) fun MutableHistogram.put(vararg point: Number) = put(DoubleBuffer(point.map { it.toDouble() }.toDoubleArray())) +fun MutableHistogram.put(vararg point: Double) = put(DoubleBuffer(point)) fun MutableHistogram.fill(sequence: Iterable>) = sequence.forEach { put(it) } diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/NDStructure.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/NDStructure.kt index ed05613c5..ec985eb8b 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/NDStructure.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/NDStructure.kt @@ -83,15 +83,15 @@ class DefaultStrides(override val shape: IntArray) : Strides { } override fun index(offset: Int): IntArray { - return sequence { - var current = offset - var strideIndex = strides.size - 2 - while (strideIndex >= 0) { - yield(current / strides[strideIndex]) - current %= strides[strideIndex] - strideIndex-- - } - }.toList().reversed().toIntArray() + val res = IntArray(shape.size) + var current = offset + var strideIndex = strides.size - 2 + while (strideIndex >= 0) { + res[ strideIndex] = (current / strides[strideIndex]) + current %= strides[strideIndex] + strideIndex-- + } + return res } override val linearSize: Int