diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram1D.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram1D.kt index 237bd87b9..0c9352e76 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram1D.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram1D.kt @@ -6,6 +6,7 @@ package space.kscience.kmath.histogram import space.kscience.kmath.domains.Domain1D +import space.kscience.kmath.domains.center import space.kscience.kmath.linear.Point import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.operations.asSequence @@ -16,7 +17,6 @@ import space.kscience.kmath.structures.Buffer * A univariate bin based on a range * * @property binValue The value of histogram including weighting - * @property standardDeviation Standard deviation of the bin value. Zero or negative if not applicable */ @UnstableKMathAPI public data class Bin1D, out V>( @@ -56,5 +56,8 @@ public fun Histogram1DBuilder.fill(array: DoubleArray): Unit = array.forEach(this::putValue) @UnstableKMathAPI -public fun Histogram1DBuilder.fill(buffer: Buffer): Unit = - buffer.asSequence().forEach(this::putValue) \ No newline at end of file +public fun Histogram1DBuilder.fill(buffer: Buffer): Unit = + buffer.asSequence().forEach(this::putValue) + +@OptIn(UnstableKMathAPI::class) +public val Bin1D.center: Double get() = domain.center \ No newline at end of file diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogram1D.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogram1D.kt index 758e7969a..93eca192f 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogram1D.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogram1D.kt @@ -17,7 +17,7 @@ import kotlin.math.floor @OptIn(UnstableKMathAPI::class) public class UniformHistogram1D( public val group: UniformHistogram1DGroup, - public val values: Map, + internal val values: Map, ) : Histogram1D { private val startPoint get() = group.startPoint @@ -82,7 +82,7 @@ public class UniformHistogram1DGroup( ) /** - * + * Fill histogram. */ public inline fun produce(block: Histogram1DBuilder.() -> Unit): UniformHistogram1D { val map = HashMap() @@ -104,23 +104,25 @@ public class UniformHistogram1DGroup( * (conserving the norming). */ @OptIn(UnstableKMathAPI::class) - public fun produceFrom(histogram: Histogram1D): UniformHistogram1D = - if ((histogram as? UniformHistogram1D)?.group == this) histogram - else { - val map = HashMap() - histogram.bins.forEach { bin -> - val range = bin.domain.range - val indexOfLeft = getIndex(range.start) - val indexOfRight = getIndex(range.endInclusive) - val numBins = indexOfRight - indexOfLeft + 1 - for (i in indexOfLeft..indexOfRight) { - map[indexOfLeft] = with(valueAlgebra) { - (map[indexOfLeft] ?: zero) + bin.binValue / numBins - } + public fun produceFrom( + histogram: Histogram1D, + ): UniformHistogram1D = if ((histogram as? UniformHistogram1D)?.group == this) { + histogram + } else { + val map = HashMap() + histogram.bins.forEach { bin -> + val range = bin.domain.range + val indexOfLeft = getIndex(range.start) + val indexOfRight = getIndex(range.endInclusive) + val numBins = indexOfRight - indexOfLeft + 1 + for (i in indexOfLeft..indexOfRight) { + map[indexOfLeft] = with(valueAlgebra) { + (map[indexOfLeft] ?: zero) + bin.binValue / numBins } } - UniformHistogram1D(this, map) } + UniformHistogram1D(this, map) + } } public fun Histogram.Companion.uniform1D( @@ -133,4 +135,22 @@ public fun Histogram.Companion.uniform1D( @UnstableKMathAPI public fun UniformHistogram1DGroup.produce( buffer: Buffer, -): UniformHistogram1D = produce { fill(buffer) } \ No newline at end of file +): UniformHistogram1D = produce { fill(buffer) } + +/** + * Map of bin centers to bin values + */ +@OptIn(UnstableKMathAPI::class) +public val UniformHistogram1D.binValues: Map + get() = bins.associate { it.center to it.binValue } + + +//TODO add normalized values inside Field-based histogram spaces with context receivers +///** +// * Map of bin centers to normalized bin values (bin size as normalization) +// */ +//@OptIn(UnstableKMathAPI::class) +//public val UniformHistogram1D.binValuesNormalized: Map +// get() = group.valueAlgebra { +// bins.associate { it.center to it.binValue / group.binSize } +// } \ No newline at end of file