Added phantom histograms

This commit is contained in:
Alexander Nozik 2018-11-23 15:46:14 +03:00
parent 8f5276bdcb
commit 29da5bad28
2 changed files with 18 additions and 2 deletions

View File

@ -84,6 +84,21 @@ class FastHistogram(
override fun iterator(): Iterator<MultivariateBin> = bins.asSequence().map { it.second }.iterator() override fun iterator(): Iterator<MultivariateBin> = bins.asSequence().map { it.second }.iterator()
/**
* Convert this histogram into NDStructure containing bin values but not bin descriptions
*/
fun asND(): NDStructure<Number> {
return ndStructure(this.bins.shape) { bins[it].value }
}
/**
* Create a phantom lightweight immutable copy of this histogram
*/
fun asPhantom(): PhantomHistogram<Double> {
val binTemplates = bins.associate { (index, bin) -> BinTemplate<Double>(bin.center, bin.sizes) to index }
return PhantomHistogram(binTemplates, asND())
}
companion object { companion object {
/** /**

View File

@ -4,7 +4,7 @@ import scientifik.kmath.linear.Vector
import scientifik.kmath.operations.Space import scientifik.kmath.operations.Space
import scientifik.kmath.structures.NDStructure import scientifik.kmath.structures.NDStructure
class BinTemplate<T : Comparable<T>>(val center: Vector<T>, val sizes: Vector<T>) { data class BinTemplate<T : Comparable<T>>(val center: Vector<T>, val sizes: Vector<T>) {
fun contains(vector: Point<out T>): Boolean { fun contains(vector: Point<out T>): Boolean {
if (vector.size != center.size) error("Dimension mismatch for input vector. Expected ${center.size}, but found ${vector.size}") if (vector.size != center.size) error("Dimension mismatch for input vector. Expected ${center.size}, but found ${vector.size}")
val upper = center + sizes/2.0 val upper = center + sizes/2.0
@ -40,10 +40,11 @@ class PhantomBin<T : Comparable<T>>(val template: BinTemplate<T>, override val v
/** /**
* Immutable histogram with explicit structure for content and additional external bin description. * Immutable histogram with explicit structure for content and additional external bin description.
* Bin search is slow, but full histogram algebra is supported. * Bin search is slow, but full histogram algebra is supported.
* @param bins map a template into structure index
*/ */
class PhantomHistogram<T : Comparable<T>>( class PhantomHistogram<T : Comparable<T>>(
val bins: Map<BinTemplate<T>, IntArray>, val bins: Map<BinTemplate<T>, IntArray>,
val data: NDStructure<Double> val data: NDStructure<Number>
) : Histogram<T, PhantomBin<T>> { ) : Histogram<T, PhantomBin<T>> {
override val dimension: Int override val dimension: Int