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 b043b1e18..5f69f9d12 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/FastHistogram.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/FastHistogram.kt @@ -2,6 +2,7 @@ package scientifik.kmath.histogram import scientifik.kmath.linear.RealVector import scientifik.kmath.linear.toVector +import scientifik.kmath.structures.Buffer import scientifik.kmath.structures.NDStructure import scientifik.kmath.structures.ndStructure import kotlin.math.floor @@ -11,7 +12,7 @@ class MultivariateBin(override val center: RealVector, val sizes: RealVector, va if (center.size != sizes.size) error("Dimension mismatch in bin creation. Expected ${center.size}, but found ${sizes.size}") } - override fun contains(vector: RealVector): Boolean { + 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.asSequence().mapIndexed { i, value -> value in (center[i] - sizes[i] / 2)..(center[i] + sizes[i] / 2) }.all { it } } @@ -70,12 +71,12 @@ class FastHistogram( } - override fun get(point: RealVector): MultivariateBin? { + override fun get(point: Buffer): MultivariateBin? { val index = IntArray(dimension) { getIndex(it, point[it]) } return bins[index] } - override fun put(point: RealVector) { + override fun put(point: Buffer) { this[point]?.inc() ?: error("Could not find appropriate bin (should not be possible)") } 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 9f6365888..1b5c231b4 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt @@ -1,15 +1,15 @@ package scientifik.kmath.histogram -import scientifik.kmath.linear.Vector -import scientifik.kmath.linear.toVector import scientifik.kmath.operations.Space +import scientifik.kmath.structures.ArrayBuffer +import scientifik.kmath.structures.Buffer /** * A simple geometric domain * TODO move to geometry module */ interface Domain { - operator fun contains(vector: Vector): Boolean + operator fun contains(vector: Buffer): Boolean val dimension: Int } @@ -21,7 +21,7 @@ interface Bin : Domain { * The value of this bin */ val value: Number - val center: Vector + val center: Buffer } interface Histogram> : Iterable { @@ -29,7 +29,7 @@ interface Histogram> : Iterable { /** * Find existing bin, corresponding to given coordinates */ - operator fun get(point: Vector): B? + operator fun get(point: Buffer): B? /** * Dimension of the histogram @@ -39,17 +39,17 @@ interface Histogram> : Iterable { /** * Increment appropriate bin */ - fun put(point: Vector) + fun put(point: Buffer) } -fun Histogram.put(vararg point: Double) = put(point.toVector()) +fun Histogram.put(vararg point: T) = put(ArrayBuffer(point)) -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 diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LinearAlgrebra.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LinearAlgrebra.kt index 269a098e3..e585b884d 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LinearAlgrebra.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LinearAlgrebra.kt @@ -4,10 +4,7 @@ import scientifik.kmath.operations.DoubleField import scientifik.kmath.operations.Field import scientifik.kmath.operations.Space import scientifik.kmath.operations.SpaceElement -import scientifik.kmath.structures.GenericNDField -import scientifik.kmath.structures.NDArray -import scientifik.kmath.structures.NDField -import scientifik.kmath.structures.get +import scientifik.kmath.structures.* /** * The space for linear elements. Supports scalar product alongside with standard linear operations. @@ -162,10 +159,8 @@ abstract class VectorSpace(val size: Int, val field: Field) : Space< } -interface Vector : SpaceElement, VectorSpace>, Iterable { - val size: Int get() = context.size - - operator fun get(i: Int): T +interface Vector : SpaceElement, VectorSpace>, Buffer, Iterable { + override val size: Int get() = context.size companion object { /** @@ -261,15 +256,17 @@ class ArrayVector internal constructor(override val context: ArrayVecto } } - override fun get(i: Int): T { - return array[i] + override fun get(index: Int): T { + return array[index] } override val self: ArrayVector get() = this override fun iterator(): Iterator = (0 until size).map { array[it] }.iterator() - override fun toString(): String = this.joinToString(prefix = "[",postfix = "]", separator = ", "){it.toString()} + override fun copy(): ArrayVector = ArrayVector(context, array) + + override fun toString(): String = this.joinToString(prefix = "[", postfix = "]", separator = ", ") { it.toString() } } typealias RealVector = Vector diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/Buffers.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/Buffers.kt index 70407e482..04983ef85 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/Buffers.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/Buffers.kt @@ -4,7 +4,7 @@ package scientifik.kmath.structures /** * A generic linear buffer for both primitives and objects */ -interface Buffer { +interface Buffer : Iterable { val size: Int @@ -36,6 +36,8 @@ inline class ListBuffer(private val list: MutableList) : MutableBuffer list[index] = value } + override fun iterator(): Iterator = list.iterator() + override fun copy(): MutableBuffer = ListBuffer(ArrayList(list)) } @@ -49,6 +51,8 @@ class ArrayBuffer(private val array: Array) : MutableBuffer { array[index] = value } + override fun iterator(): Iterator = array.iterator() + override fun copy(): MutableBuffer = ArrayBuffer(array.copyOf()) } @@ -62,6 +66,8 @@ class DoubleBuffer(private val array: DoubleArray) : MutableBuffer { array[index] = value } + override fun iterator(): Iterator = array.iterator() + override fun copy(): MutableBuffer = DoubleBuffer(array.copyOf()) } 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 b614c0716..2382c379c 100644 --- a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt +++ b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt @@ -2,6 +2,7 @@ package scientifik.kmath.histogram import scientifik.kmath.linear.RealVector import scientifik.kmath.linear.toVector +import scientifik.kmath.structures.Buffer import java.util.* import kotlin.math.floor @@ -15,7 +16,7 @@ class UnivariateBin(val position: Double, val size: Double, val counter: LongCou operator fun contains(value: Double): Boolean = value in (position - size / 2)..(position + size / 2) - override fun contains(vector: RealVector): Boolean = contains(vector[0]) + override fun contains(vector: Buffer): Boolean = contains(vector[0]) internal operator fun inc() = this.also { counter.increment()} @@ -44,7 +45,7 @@ class UnivariateHistogram private constructor(private val factory: (Double) -> U synchronized(this) { bins.put(it.position, it) } } - override fun get(point: RealVector): UnivariateBin? = get(point[0]) + override fun get(point: Buffer): UnivariateBin? = get(point[0]) override val dimension: Int get() = 1 @@ -57,7 +58,7 @@ class UnivariateHistogram private constructor(private val factory: (Double) -> U (get(value) ?: createBin(value)).inc() } - override fun put(point: RealVector) = put(point[0]) + override fun put(point: Buffer) = put(point[0]) companion object { fun uniform(binSize: Double, start: Double = 0.0): UnivariateHistogram {