From 6a0ef6a2356c26911189e90e8ad8d4213ecce757 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 28 Oct 2018 21:41:58 +0300 Subject: [PATCH] Added limited polimorphism to Histogram --- .../kmath/histogram/FastHistogram.kt | 10 +++---- .../scientifik/kmath/histogram/Histogram.kt | 26 +++++++++---------- .../kmath/histogram/UnivariateHistogram.kt | 4 +-- 3 files changed, 20 insertions(+), 20 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 5fd4c7f03..b043b1e18 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/FastHistogram.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/FastHistogram.kt @@ -6,13 +6,13 @@ import scientifik.kmath.structures.NDStructure import scientifik.kmath.structures.ndStructure import kotlin.math.floor -class MultivariateBin(override val center: RealVector, val sizes: RealVector, val counter: LongCounter = LongCounter()) : Bin { +class MultivariateBin(override val center: RealVector, val sizes: RealVector, val counter: LongCounter = LongCounter()) : Bin { init { if (center.size != sizes.size) error("Dimension mismatch in bin creation. Expected ${center.size}, but found ${sizes.size}") } override fun contains(vector: RealVector): 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}") return vector.asSequence().mapIndexed { i, value -> value in (center[i] - sizes[i] / 2)..(center[i] + sizes[i] / 2) }.all { it } } @@ -28,8 +28,8 @@ class MultivariateBin(override val center: RealVector, val sizes: RealVector, va class FastHistogram( private val lower: RealVector, private val upper: RealVector, - private val binNums: IntArray = IntArray(lower.size) { 100 } -) : Histogram { + private val binNums: IntArray = IntArray(lower.size) { 20 } +) : Histogram { init { // argument checks @@ -105,7 +105,7 @@ class FastHistogram( *) *``` */ - fun fromRanges(vararg ranges: Pair,Int>): FastHistogram { + fun fromRanges(vararg ranges: Pair, Int>): FastHistogram { return FastHistogram( ranges.map { it.first.start }.toVector(), ranges.map { it.first.endInclusive }.toVector(), 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 79b06ee10..9f6365888 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt @@ -1,6 +1,6 @@ package scientifik.kmath.histogram -import scientifik.kmath.linear.RealVector +import scientifik.kmath.linear.Vector import scientifik.kmath.linear.toVector import scientifik.kmath.operations.Space @@ -8,28 +8,28 @@ import scientifik.kmath.operations.Space * A simple geometric domain * TODO move to geometry module */ -interface Domain { - operator fun contains(vector: RealVector): Boolean +interface Domain { + operator fun contains(vector: Vector): Boolean val dimension: Int } /** * The bin in the histogram. The histogram is by definition always done in the real space */ -interface Bin : Domain { +interface Bin : Domain { /** * The value of this bin */ val value: Number - val center: RealVector + val center: Vector } -interface Histogram : Iterable { +interface Histogram> : Iterable { /** * Find existing bin, corresponding to given coordinates */ - operator fun get(point: RealVector): B? + operator fun get(point: Vector): B? /** * Dimension of the histogram @@ -39,24 +39,24 @@ interface Histogram : Iterable { /** * Increment appropriate bin */ - fun put(point: RealVector) + fun put(point: Vector) } -fun Histogram<*>.put(vararg point: Double) = put(point.toVector()) +fun Histogram.put(vararg point: Double) = put(point.toVector()) -fun Histogram<*>.fill(sequence: Iterable) = sequence.forEach { put(it) } +fun Histogram.fill(sequence: Iterable>) = sequence.forEach { put(it) } /** * Pass a sequence builder into histogram */ -fun Histogram<*>.fill(buider: suspend SequenceScope.() -> Unit) = fill(sequence(buider).asIterable()) +fun Histogram.fill(buider: suspend SequenceScope>.() -> Unit) = fill(sequence(buider).asIterable()) /** * A space to perform arithmetic operations on histograms */ -interface HistogramSpace> : Space { +interface HistogramSpace, H : Histogram> : Space { /** * Rules for performing operations on bins */ - val binSpace: Space + val binSpace: Space> } \ No newline at end of file diff --git a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt index a3b066b02..b614c0716 100644 --- a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt +++ b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt @@ -7,7 +7,7 @@ import kotlin.math.floor //TODO move to common -class UnivariateBin(val position: Double, val size: Double, val counter: LongCounter = LongCounter()) : Bin { +class UnivariateBin(val position: Double, val size: Double, val counter: LongCounter = LongCounter()) : Bin { //TODO add weighting override val value: Number get() = counter.sum() @@ -25,7 +25,7 @@ class UnivariateBin(val position: Double, val size: Double, val counter: LongCou /** * Univariate histogram with log(n) bin search speed */ -class UnivariateHistogram private constructor(private val factory: (Double) -> UnivariateBin) : Histogram { +class UnivariateHistogram private constructor(private val factory: (Double) -> UnivariateBin) : Histogram { private val bins: TreeMap = TreeMap()