From 29da5bad2842a08480e5e1403dd97e8d7055a2d8 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 23 Nov 2018 15:46:14 +0300 Subject: [PATCH] Added phantom histograms --- .../scientifik/kmath/histogram/FastHistogram.kt | 15 +++++++++++++++ .../kmath/histogram/PhantomHistogram.kt | 5 +++-- 2 files changed, 18 insertions(+), 2 deletions(-) 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 621ea90c0..37c4eed2a 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/FastHistogram.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/FastHistogram.kt @@ -84,6 +84,21 @@ class FastHistogram( override fun iterator(): Iterator = bins.asSequence().map { it.second }.iterator() + /** + * Convert this histogram into NDStructure containing bin values but not bin descriptions + */ + fun asND(): NDStructure { + return ndStructure(this.bins.shape) { bins[it].value } + } + + /** + * Create a phantom lightweight immutable copy of this histogram + */ + fun asPhantom(): PhantomHistogram { + val binTemplates = bins.associate { (index, bin) -> BinTemplate(bin.center, bin.sizes) to index } + return PhantomHistogram(binTemplates, asND()) + } + companion object { /** diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/PhantomHistogram.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/PhantomHistogram.kt index 041eccfe2..9ea17c730 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/PhantomHistogram.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/PhantomHistogram.kt @@ -4,7 +4,7 @@ import scientifik.kmath.linear.Vector import scientifik.kmath.operations.Space import scientifik.kmath.structures.NDStructure -class BinTemplate>(val center: Vector, val sizes: Vector) { +data class BinTemplate>(val center: Vector, val sizes: Vector) { fun contains(vector: Point): Boolean { if (vector.size != center.size) error("Dimension mismatch for input vector. Expected ${center.size}, but found ${vector.size}") val upper = center + sizes/2.0 @@ -40,10 +40,11 @@ class PhantomBin>(val template: BinTemplate, override val v /** * Immutable histogram with explicit structure for content and additional external bin description. * Bin search is slow, but full histogram algebra is supported. + * @param bins map a template into structure index */ class PhantomHistogram>( val bins: Map, IntArray>, - val data: NDStructure + val data: NDStructure ) : Histogram> { override val dimension: Int