forked from kscience/kmath
Minor change to histogram API. Project build to kts.
This commit is contained in:
parent
be18014d54
commit
62c7099f8a
24
build.gradle
24
build.gradle
@ -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'
|
||||
}
|
28
build.gradle.kts
Normal file
28
build.gradle.kts
Normal file
@ -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")
|
||||
}
|
@ -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<Double, MultivariateBin> {
|
||||
) : MutableHistogram<Double, MultivariateBin> {
|
||||
|
||||
init {
|
||||
// argument checks
|
||||
@ -76,7 +77,8 @@ class FastHistogram(
|
||||
return bins[index]
|
||||
}
|
||||
|
||||
override fun put(point: Buffer<out Double>) {
|
||||
override fun put(point: Buffer<out Double>, weight: Double) {
|
||||
if (weight != 1.0) TODO("Implement weighting")
|
||||
this[point]?.inc() ?: error("Could not find appropriate bin (should not be possible)")
|
||||
}
|
||||
|
||||
|
@ -4,12 +4,14 @@ import scientifik.kmath.operations.Space
|
||||
import scientifik.kmath.structures.ArrayBuffer
|
||||
import scientifik.kmath.structures.Buffer
|
||||
|
||||
typealias Point<T> = Buffer<T>
|
||||
|
||||
/**
|
||||
* A simple geometric domain
|
||||
* TODO move to geometry module
|
||||
*/
|
||||
interface Domain<T: Any> {
|
||||
operator fun contains(vector: Buffer<out T>): Boolean
|
||||
operator fun contains(vector: Point<out T>): Boolean
|
||||
val dimension: Int
|
||||
}
|
||||
|
||||
@ -21,7 +23,7 @@ interface Bin<T: Any> : Domain<T> {
|
||||
* The value of this bin
|
||||
*/
|
||||
val value: Number
|
||||
val center: Buffer<T>
|
||||
val center: Point<T>
|
||||
}
|
||||
|
||||
interface Histogram<T: Any, out B : Bin<T>> : Iterable<B> {
|
||||
@ -29,27 +31,31 @@ interface Histogram<T: Any, out B : Bin<T>> : Iterable<B> {
|
||||
/**
|
||||
* Find existing bin, corresponding to given coordinates
|
||||
*/
|
||||
operator fun get(point: Buffer<out T>): B?
|
||||
operator fun get(point: Point<out T>): B?
|
||||
|
||||
/**
|
||||
* Dimension of the histogram
|
||||
*/
|
||||
val dimension: Int
|
||||
|
||||
}
|
||||
|
||||
interface MutableHistogram<T: Any, out B : Bin<T>>: Histogram<T,B>{
|
||||
|
||||
/**
|
||||
* Increment appropriate bin
|
||||
*/
|
||||
fun put(point: Buffer<out T>)
|
||||
fun put(point: Point<out T>, weight: Double = 1.0)
|
||||
}
|
||||
|
||||
fun <T: Any> Histogram<T,*>.put(vararg point: T) = put(ArrayBuffer(point))
|
||||
fun <T: Any> MutableHistogram<T,*>.put(vararg point: T) = put(ArrayBuffer(point))
|
||||
|
||||
fun <T: Any> Histogram<T,*>.fill(sequence: Iterable<Buffer<T>>) = sequence.forEach { put(it) }
|
||||
fun <T: Any> MutableHistogram<T,*>.fill(sequence: Iterable<Point<T>>) = sequence.forEach { put(it) }
|
||||
|
||||
/**
|
||||
* Pass a sequence builder into histogram
|
||||
*/
|
||||
fun <T: Any> Histogram<T, *>.fill(buider: suspend SequenceScope<Buffer<T>>.() -> Unit) = fill(sequence(buider).asIterable())
|
||||
fun <T: Any> MutableHistogram<T, *>.fill(buider: suspend SequenceScope<Point<T>>.() -> Unit) = fill(sequence(buider).asIterable())
|
||||
|
||||
/**
|
||||
* A space to perform arithmetic operations on histograms
|
||||
|
@ -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<out Double>): 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<Double> {
|
||||
|
||||
override fun contains(vector: Point<out Double>): Boolean = template.contains(vector)
|
||||
|
||||
override val dimension: Int
|
||||
get() = template.center.size
|
||||
|
||||
override val center: Point<Double>
|
||||
get() = template.center
|
||||
|
||||
}
|
||||
|
||||
class PhantomHistogram(
|
||||
val bins: Map<BinTemplate, IntArray>,
|
||||
val data: NDStructure<Double>
|
||||
) : Histogram<Double, PhantomBin> {
|
||||
|
||||
override val dimension: Int
|
||||
get() = data.dimension
|
||||
|
||||
override fun iterator(): Iterator<PhantomBin> {
|
||||
return bins.asSequence().map {entry-> PhantomBin(entry.key,data[entry.value]) }.iterator()
|
||||
}
|
||||
|
||||
override fun get(point: Point<out Double>): PhantomBin? {
|
||||
val template = bins.keys.find { it.contains(point) }
|
||||
return template?.let { PhantomBin(it, data[bins[it]!!]) }
|
||||
}
|
||||
|
||||
}
|
@ -56,7 +56,7 @@ class ArrayBuffer<T>(private val array: Array<T>) : MutableBuffer<T> {
|
||||
override fun copy(): MutableBuffer<T> = ArrayBuffer(array.copyOf())
|
||||
}
|
||||
|
||||
class DoubleBuffer(private val array: DoubleArray) : MutableBuffer<Double> {
|
||||
inline class DoubleBuffer(private val array: DoubleArray) : MutableBuffer<Double> {
|
||||
override val size: Int
|
||||
get() = array.size
|
||||
|
||||
@ -71,6 +71,21 @@ class DoubleBuffer(private val array: DoubleArray) : MutableBuffer<Double> {
|
||||
override fun copy(): MutableBuffer<Double> = DoubleBuffer(array.copyOf())
|
||||
}
|
||||
|
||||
inline class IntBuffer(private val array: IntArray): MutableBuffer<Int>{
|
||||
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<Int> = array.iterator()
|
||||
|
||||
override fun copy(): MutableBuffer<Int> = IntBuffer(array.copyOf())
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> buffer(size: Int, noinline initializer: (Int) -> T): Buffer<T> {
|
||||
return ArrayBuffer(Array(size, initializer))
|
||||
}
|
||||
|
@ -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<Double,UnivariateBin> {
|
||||
class UnivariateHistogram private constructor(private val factory: (Double) -> UnivariateBin) : MutableHistogram<Double,UnivariateBin> {
|
||||
|
||||
private val bins: TreeMap<Double, UnivariateBin> = TreeMap()
|
||||
|
||||
@ -58,7 +58,10 @@ class UnivariateHistogram private constructor(private val factory: (Double) -> U
|
||||
(get(value) ?: createBin(value)).inc()
|
||||
}
|
||||
|
||||
override fun put(point: Buffer<out Double>) = put(point[0])
|
||||
override fun put(point: Buffer<out Double>, weight: Double) {
|
||||
if (weight != 1.0) TODO("Implement weighting")
|
||||
put(point[0])
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun uniform(binSize: Double, start: Double = 0.0): UnivariateHistogram {
|
||||
|
@ -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'
|
||||
|
14
settings.gradle.kts
Normal file
14
settings.gradle.kts
Normal file
@ -0,0 +1,14 @@
|
||||
pluginManagement {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven("https://plugins.gradle.org/m2/")
|
||||
}
|
||||
}
|
||||
|
||||
enableFeaturePreview("GRADLE_METADATA")
|
||||
|
||||
rootProject.name = "kmath"
|
||||
include(
|
||||
":kmath-core",
|
||||
":kmath-jmh"
|
||||
)
|
Loading…
Reference in New Issue
Block a user