From 62c7099f8aea8882c3c5c69568f62f4b50c1f400 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 20 Nov 2018 22:09:07 +0300 Subject: [PATCH] Minor change to histogram API. Project build to kts. --- build.gradle | 24 ----------- build.gradle.kts | 28 +++++++++++++ .../kmath/histogram/FastHistogram.kt | 8 ++-- .../scientifik/kmath/histogram/Histogram.kt | 20 +++++---- .../kmath/histogram/PhantomHistogram.kt | 42 +++++++++++++++++++ .../scientifik/kmath/structures/Buffers.kt | 17 +++++++- .../kmath/histogram/UnivariateHistogram.kt | 7 +++- settings.gradle | 13 ------ settings.gradle.kts | 14 +++++++ 9 files changed, 123 insertions(+), 50 deletions(-) delete mode 100644 build.gradle create mode 100644 build.gradle.kts create mode 100644 kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/PhantomHistogram.kt delete mode 100644 settings.gradle create mode 100644 settings.gradle.kts diff --git a/build.gradle b/build.gradle deleted file mode 100644 index 51d476ed6..000000000 --- a/build.gradle +++ /dev/null @@ -1,24 +0,0 @@ -buildscript { - ext.kotlin_version = '1.3.0' - - repositories { - jcenter() - } - - dependencies { - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4+" - } -} - -allprojects { - apply plugin: 'maven-publish' - apply plugin: "com.jfrog.artifactory" - - group = 'scientifik' - version = '0.0.1-SNAPSHOT' -} - -if(file('artifactory.gradle').exists()){ - apply from: 'artifactory.gradle' -} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 000000000..2d7b9f119 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,28 @@ +buildscript { + val kotlin_version = "1.3.10" + + repositories { + jcenter() + } + + dependencies { + classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") + classpath("org.jfrog.buildinfo:build-info-extractor-gradle:4+") + } +} + +plugins { + id("com.jfrog.artifactory") version "4.8.1" apply false +} + +allprojects { + apply(plugin = "maven-publish") + apply(plugin = "com.jfrog.artifactory") + + group = "scientifik" + version = "0.0.1-SNAPSHOT" +} + +if(file("artifactory.gradle").exists()){ + apply(from = "artifactory.gradle") +} \ No newline at end of file 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 3852de316..621ea90c0 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/FastHistogram.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/FastHistogram.kt @@ -24,13 +24,14 @@ class MultivariateBin(override val center: RealVector, val sizes: RealVector, va } /** - * Uniform multivariate histogram with fixed borders. Based on NDStructure implementation with complexity of m for bin search, where m is the number of dimensions + * Uniform multivariate histogram with fixed borders. Based on NDStructure implementation with complexity of m for bin search, where m is the number of dimensions. + * The histogram is optimized for speed, but have large size in memory */ class FastHistogram( private val lower: RealVector, private val upper: RealVector, private val binNums: IntArray = IntArray(lower.size) { 20 } -) : Histogram { +) : MutableHistogram { init { // argument checks @@ -76,7 +77,8 @@ class FastHistogram( return bins[index] } - override fun put(point: Buffer) { + override fun put(point: Buffer, weight: Double) { + if (weight != 1.0) TODO("Implement weighting") 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 1b5c231b4..e143bd90d 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt @@ -4,12 +4,14 @@ import scientifik.kmath.operations.Space import scientifik.kmath.structures.ArrayBuffer import scientifik.kmath.structures.Buffer +typealias Point = Buffer + /** * A simple geometric domain * TODO move to geometry module */ interface Domain { - operator fun contains(vector: Buffer): Boolean + operator fun contains(vector: Point): Boolean val dimension: Int } @@ -21,7 +23,7 @@ interface Bin : Domain { * The value of this bin */ val value: Number - val center: Buffer + val center: Point } interface Histogram> : Iterable { @@ -29,27 +31,31 @@ interface Histogram> : Iterable { /** * Find existing bin, corresponding to given coordinates */ - operator fun get(point: Buffer): B? + operator fun get(point: Point): B? /** * Dimension of the histogram */ val dimension: Int +} + +interface MutableHistogram>: Histogram{ + /** * Increment appropriate bin */ - fun put(point: Buffer) + fun put(point: Point, weight: Double = 1.0) } -fun Histogram.put(vararg point: T) = put(ArrayBuffer(point)) +fun MutableHistogram.put(vararg point: T) = put(ArrayBuffer(point)) -fun Histogram.fill(sequence: Iterable>) = sequence.forEach { put(it) } +fun MutableHistogram.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 MutableHistogram.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/histogram/PhantomHistogram.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/PhantomHistogram.kt new file mode 100644 index 000000000..469856daa --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/PhantomHistogram.kt @@ -0,0 +1,42 @@ +package scientifik.kmath.histogram + +import scientifik.kmath.linear.RealVector +import scientifik.kmath.structures.NDStructure + +class BinTemplate(val center: RealVector, val sizes: RealVector) { + fun contains(vector: Point): 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 } + } +} + +class PhantomBin(val template: BinTemplate, override val value: Number) : Bin { + + override fun contains(vector: Point): Boolean = template.contains(vector) + + override val dimension: Int + get() = template.center.size + + override val center: Point + get() = template.center + +} + +class PhantomHistogram( + val bins: Map, + val data: NDStructure +) : Histogram { + + override val dimension: Int + get() = data.dimension + + override fun iterator(): Iterator { + return bins.asSequence().map {entry-> PhantomBin(entry.key,data[entry.value]) }.iterator() + } + + override fun get(point: Point): PhantomBin? { + val template = bins.keys.find { it.contains(point) } + return template?.let { PhantomBin(it, data[bins[it]!!]) } + } + +} \ No newline at end of file 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 04983ef85..3e2712f0c 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/Buffers.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/Buffers.kt @@ -56,7 +56,7 @@ class ArrayBuffer(private val array: Array) : MutableBuffer { override fun copy(): MutableBuffer = ArrayBuffer(array.copyOf()) } -class DoubleBuffer(private val array: DoubleArray) : MutableBuffer { +inline class DoubleBuffer(private val array: DoubleArray) : MutableBuffer { override val size: Int get() = array.size @@ -71,6 +71,21 @@ class DoubleBuffer(private val array: DoubleArray) : MutableBuffer { override fun copy(): MutableBuffer = DoubleBuffer(array.copyOf()) } +inline class IntBuffer(private val array: IntArray): MutableBuffer{ + override val size: Int + get() = array.size + + override fun get(index: Int): Int = array[index] + + override fun set(index: Int, value: Int) { + array[index] = value + } + + override fun iterator(): Iterator = array.iterator() + + override fun copy(): MutableBuffer = IntBuffer(array.copyOf()) +} + inline fun buffer(size: Int, noinline initializer: (Int) -> T): Buffer { return ArrayBuffer(Array(size, initializer)) } 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 2382c379c..26ad520e6 100644 --- a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt +++ b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt @@ -26,7 +26,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) : MutableHistogram { private val bins: TreeMap = TreeMap() @@ -58,7 +58,10 @@ class UnivariateHistogram private constructor(private val factory: (Double) -> U (get(value) ?: createBin(value)).inc() } - override fun put(point: Buffer) = put(point[0]) + override fun put(point: Buffer, weight: Double) { + if (weight != 1.0) TODO("Implement weighting") + put(point[0]) + } companion object { fun uniform(binSize: Double, start: Double = 0.0): UnivariateHistogram { diff --git a/settings.gradle b/settings.gradle deleted file mode 100644 index df4ccc99e..000000000 --- a/settings.gradle +++ /dev/null @@ -1,13 +0,0 @@ -pluginManagement { - repositories { - mavenCentral() - maven { url = 'https://plugins.gradle.org/m2/' } - } -} - -enableFeaturePreview('GRADLE_METADATA') - -rootProject.name = 'kmath' -include ':kmath-core' -include ':kmath-jmh' - diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 000000000..0f90d4c0b --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,14 @@ +pluginManagement { + repositories { + mavenCentral() + maven("https://plugins.gradle.org/m2/") + } +} + +enableFeaturePreview("GRADLE_METADATA") + +rootProject.name = "kmath" +include( + ":kmath-core", + ":kmath-jmh" +)