From fc5ec8fed74b0a4f27d212a9e78e61d6cd9794bb Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Wed, 9 Sep 2020 22:31:54 +0700 Subject: [PATCH] Drop koma support, add more explicit visibility modifiers --- README.md | 3 - doc/features.md | 3 - examples/build.gradle.kts | 3 - .../kmath/structures/ArrayBenchmark.kt | 29 ++-- .../ast/ExpressionsInterpretersBenchmark.kt | 140 +++++++++--------- .../kmath/linear/LinearAlgebraBenchmark.kt | 55 ------- .../kmath/linear/MultiplicationBenchmark.kt | 49 ------ kmath-commons/build.gradle.kts | 2 +- .../kotlin/scientifik/kmath/chains/Chain.kt | 25 ++-- .../scientifik/kmath/functions/functions.kt | 2 +- .../scientifik/kmath/histogram/Histogram.kt | 1 - .../kmath/histogram/UnivariateHistogram.kt | 4 +- kmath-koma/build.gradle.kts | 30 ---- .../scientifik.kmath.linear/KomaMatrix.kt | 105 ------------- .../scientifik/kmath/prob/Distribution.kt | 2 +- .../kmath/prob/FactorizedDistribution.kt | 30 ++-- .../scientifik/kmath/prob/RandomChain.kt | 7 +- .../scientifik/kmath/prob/RandomGenerator.kt | 55 +++---- .../scientifik/kmath/prob/SamplerAlgebra.kt | 17 +-- .../kotlin/scientifik/kmath/prob/Statistic.kt | 44 +++--- .../kmath/prob/UniformDistribution.kt | 34 ++--- .../scientifik/kmath/prob/distributions.kt | 12 +- settings.gradle.kts | 4 +- 23 files changed, 182 insertions(+), 474 deletions(-) delete mode 100644 examples/src/main/kotlin/scientifik/kmath/linear/LinearAlgebraBenchmark.kt delete mode 100644 examples/src/main/kotlin/scientifik/kmath/linear/MultiplicationBenchmark.kt delete mode 100644 kmath-koma/build.gradle.kts delete mode 100644 kmath-koma/src/commonMain/kotlin/scientifik.kmath.linear/KomaMatrix.kt diff --git a/README.md b/README.md index 24a7d7a4a..08d1e988a 100644 --- a/README.md +++ b/README.md @@ -54,9 +54,6 @@ can be used for a wide variety of purposes from high performance calculations to library in Kotlin code and maybe rewrite some parts to better suit the Kotlin programming paradigm, however there is no fixed roadmap for that. Feel free to submit a feature request if you want something to be done first. -* **Koma wrapper** [Koma](https://github.com/kyonifer/koma) is a well established numerics library in Kotlin, specifically linear algebra. -The plan is to have wrappers for koma implementations for compatibility with kmath API. - ## Planned features * **Messaging** A mathematical notation to support multi-language and multi-node communication for mathematical tasks. diff --git a/doc/features.md b/doc/features.md index e6a820c1e..0f2c4203f 100644 --- a/doc/features.md +++ b/doc/features.md @@ -12,6 +12,3 @@ api and multiple library back-ends. * [Expressions](./expressions.md) * Commons math integration - -* Koma integration - diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 2c51574e5..40da41d2b 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -10,7 +10,6 @@ plugins { allOpen.annotation("org.openjdk.jmh.annotations.State") repositories { - maven("http://dl.bintray.com/kyonifer/maven") maven("https://dl.bintray.com/mipt-npm/scientifik") maven("https://dl.bintray.com/mipt-npm/dev") maven("https://dl.bintray.com/kotlin/kotlin-dev/") @@ -25,10 +24,8 @@ dependencies { implementation(project(":kmath-coroutines")) implementation(project(":kmath-commons")) implementation(project(":kmath-prob")) - implementation(project(":kmath-koma")) implementation(project(":kmath-viktor")) implementation(project(":kmath-dimensions")) - implementation("com.kyonifer:koma-core-ejml:0.12") implementation("org.jetbrains.kotlinx:kotlinx-io-jvm:0.2.0-npm-dev-6") implementation("org.jetbrains.kotlinx:kotlinx.benchmark.runtime:0.2.0-dev-20") "benchmarksCompile"(sourceSets.main.get().output + sourceSets.main.get().compileClasspath) //sourceSets.main.output + sourceSets.main.runtimeClasspath diff --git a/examples/src/benchmarks/kotlin/scientifik/kmath/structures/ArrayBenchmark.kt b/examples/src/benchmarks/kotlin/scientifik/kmath/structures/ArrayBenchmark.kt index d605e1b9c..2ccb27871 100644 --- a/examples/src/benchmarks/kotlin/scientifik/kmath/structures/ArrayBenchmark.kt +++ b/examples/src/benchmarks/kotlin/scientifik/kmath/structures/ArrayBenchmark.kt @@ -5,44 +5,33 @@ import org.openjdk.jmh.annotations.Scope import org.openjdk.jmh.annotations.State import java.nio.IntBuffer - @State(Scope.Benchmark) class ArrayBenchmark { - @Benchmark fun benchmarkArrayRead() { var res = 0 - for (i in 1..size) { - res += array[size - i] - } + for (i in 1..size) res += array[size - i] } @Benchmark fun benchmarkBufferRead() { var res = 0 - for (i in 1..size) { - res += arrayBuffer.get(size - i) - } + for (i in 1..size) res += arrayBuffer.get(size - i) } @Benchmark fun nativeBufferRead() { var res = 0 - for (i in 1..size) { - res += nativeBuffer.get(size - i) - } + for (i in 1..size) res += nativeBuffer.get(size - i) } companion object { - val size = 1000 - - val array = IntArray(size) { it } - val arrayBuffer = IntBuffer.wrap(array) - val nativeBuffer = IntBuffer.allocate(size).also { - for (i in 0 until size) { - it.put(i, i) - } + const val size: Int = 1000 + val array: IntArray = IntArray(size) { it } + val arrayBuffer: IntBuffer = IntBuffer.wrap(array) + val nativeBuffer: IntBuffer = IntBuffer.allocate(size).also { + for (i in 0 until size) it.put(i, i) } } -} \ No newline at end of file +} diff --git a/examples/src/main/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt b/examples/src/main/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt index 17a70a4aa..80e70d149 100644 --- a/examples/src/main/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt +++ b/examples/src/main/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt @@ -1,70 +1,70 @@ -package scientifik.kmath.ast - -import scientifik.kmath.asm.compile -import scientifik.kmath.expressions.Expression -import scientifik.kmath.expressions.expressionInField -import scientifik.kmath.expressions.invoke -import scientifik.kmath.operations.Field -import scientifik.kmath.operations.RealField -import kotlin.random.Random -import kotlin.system.measureTimeMillis - -class ExpressionsInterpretersBenchmark { - private val algebra: Field = RealField - fun functionalExpression() { - val expr = algebra.expressionInField { - variable("x") * const(2.0) + const(2.0) / variable("x") - const(16.0) - } - - invokeAndSum(expr) - } - - fun mstExpression() { - val expr = algebra.mstInField { - symbol("x") * number(2.0) + number(2.0) / symbol("x") - number(16.0) - } - - invokeAndSum(expr) - } - - fun asmExpression() { - val expr = algebra.mstInField { - symbol("x") * number(2.0) + number(2.0) / symbol("x") - number(16.0) - }.compile() - - invokeAndSum(expr) - } - - private fun invokeAndSum(expr: Expression) { - val random = Random(0) - var sum = 0.0 - - repeat(1000000) { - sum += expr("x" to random.nextDouble()) - } - - println(sum) - } -} - -fun main() { - val benchmark = ExpressionsInterpretersBenchmark() - - val fe = measureTimeMillis { - benchmark.functionalExpression() - } - - println("fe=$fe") - - val mst = measureTimeMillis { - benchmark.mstExpression() - } - - println("mst=$mst") - - val asm = measureTimeMillis { - benchmark.asmExpression() - } - - println("asm=$asm") -} +//package scientifik.kmath.ast +// +//import scientifik.kmath.asm.compile +//import scientifik.kmath.expressions.Expression +//import scientifik.kmath.expressions.expressionInField +//import scientifik.kmath.expressions.invoke +//import scientifik.kmath.operations.Field +//import scientifik.kmath.operations.RealField +//import kotlin.random.Random +//import kotlin.system.measureTimeMillis +// +//class ExpressionsInterpretersBenchmark { +// private val algebra: Field = RealField +// fun functionalExpression() { +// val expr = algebra.expressionInField { +// variable("x") * const(2.0) + const(2.0) / variable("x") - const(16.0) +// } +// +// invokeAndSum(expr) +// } +// +// fun mstExpression() { +// val expr = algebra.mstInField { +// symbol("x") * number(2.0) + number(2.0) / symbol("x") - number(16.0) +// } +// +// invokeAndSum(expr) +// } +// +// fun asmExpression() { +// val expr = algebra.mstInField { +// symbol("x") * number(2.0) + number(2.0) / symbol("x") - number(16.0) +// }.compile() +// +// invokeAndSum(expr) +// } +// +// private fun invokeAndSum(expr: Expression) { +// val random = Random(0) +// var sum = 0.0 +// +// repeat(1000000) { +// sum += expr("x" to random.nextDouble()) +// } +// +// println(sum) +// } +//} +// +//fun main() { +// val benchmark = ExpressionsInterpretersBenchmark() +// +// val fe = measureTimeMillis { +// benchmark.functionalExpression() +// } +// +// println("fe=$fe") +// +// val mst = measureTimeMillis { +// benchmark.mstExpression() +// } +// +// println("mst=$mst") +// +// val asm = measureTimeMillis { +// benchmark.asmExpression() +// } +// +// println("asm=$asm") +//} diff --git a/examples/src/main/kotlin/scientifik/kmath/linear/LinearAlgebraBenchmark.kt b/examples/src/main/kotlin/scientifik/kmath/linear/LinearAlgebraBenchmark.kt deleted file mode 100644 index 6cc5411b8..000000000 --- a/examples/src/main/kotlin/scientifik/kmath/linear/LinearAlgebraBenchmark.kt +++ /dev/null @@ -1,55 +0,0 @@ -package scientifik.kmath.linear - -import koma.matrix.ejml.EJMLMatrixFactory -import scientifik.kmath.commons.linear.CMMatrixContext -import scientifik.kmath.commons.linear.inverse -import scientifik.kmath.commons.linear.toCM -import scientifik.kmath.operations.RealField -import scientifik.kmath.operations.invoke -import scientifik.kmath.structures.Matrix -import kotlin.contracts.ExperimentalContracts -import kotlin.random.Random -import kotlin.system.measureTimeMillis - -@ExperimentalContracts -fun main() { - val random = Random(1224) - val dim = 100 - //creating invertible matrix - val u = Matrix.real(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } - val l = Matrix.real(dim, dim) { i, j -> if (i >= j) random.nextDouble() else 0.0 } - val matrix = l dot u - - val n = 5000 // iterations - - MatrixContext.real { - repeat(50) { val res = inverse(matrix) } - val inverseTime = measureTimeMillis { repeat(n) { val res = inverse(matrix) } } - println("[kmath] Inversion of $n matrices $dim x $dim finished in $inverseTime millis") - } - - //commons-math - - val commonsTime = measureTimeMillis { - CMMatrixContext { - val cm = matrix.toCM() //avoid overhead on conversion - repeat(n) { val res = inverse(cm) } - } - } - - - println("[commons-math] Inversion of $n matrices $dim x $dim finished in $commonsTime millis") - - //koma-ejml - - val komaTime = measureTimeMillis { - (KomaMatrixContext(EJMLMatrixFactory(), RealField)) { - val km = matrix.toKoma() //avoid overhead on conversion - repeat(n) { - val res = inverse(km) - } - } - } - - println("[koma-ejml] Inversion of $n matrices $dim x $dim finished in $komaTime millis") -} \ No newline at end of file diff --git a/examples/src/main/kotlin/scientifik/kmath/linear/MultiplicationBenchmark.kt b/examples/src/main/kotlin/scientifik/kmath/linear/MultiplicationBenchmark.kt deleted file mode 100644 index 3ae550682..000000000 --- a/examples/src/main/kotlin/scientifik/kmath/linear/MultiplicationBenchmark.kt +++ /dev/null @@ -1,49 +0,0 @@ -package scientifik.kmath.linear - -import koma.matrix.ejml.EJMLMatrixFactory -import scientifik.kmath.commons.linear.CMMatrixContext -import scientifik.kmath.commons.linear.toCM -import scientifik.kmath.operations.RealField -import scientifik.kmath.operations.invoke -import scientifik.kmath.structures.Matrix -import kotlin.random.Random -import kotlin.system.measureTimeMillis - -fun main() { - val random = Random(12224) - val dim = 1000 - //creating invertible matrix - val matrix1 = Matrix.real(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } - val matrix2 = Matrix.real(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } - -// //warmup -// matrix1 dot matrix2 - - CMMatrixContext { - val cmMatrix1 = matrix1.toCM() - val cmMatrix2 = matrix2.toCM() - - val cmTime = measureTimeMillis { - cmMatrix1 dot cmMatrix2 - } - - println("CM implementation time: $cmTime") - } - - (KomaMatrixContext(EJMLMatrixFactory(), RealField)) { - val komaMatrix1 = matrix1.toKoma() - val komaMatrix2 = matrix2.toKoma() - - val komaTime = measureTimeMillis { - komaMatrix1 dot komaMatrix2 - } - - println("Koma-ejml implementation time: $komaTime") - } - - val genericTime = measureTimeMillis { - val res = matrix1 dot matrix2 - } - - println("Generic implementation time: $genericTime") -} \ No newline at end of file diff --git a/kmath-commons/build.gradle.kts b/kmath-commons/build.gradle.kts index 45eee7765..b64b855a4 100644 --- a/kmath-commons/build.gradle.kts +++ b/kmath-commons/build.gradle.kts @@ -5,6 +5,6 @@ dependencies { api(project(":kmath-core")) api(project(":kmath-coroutines")) api(project(":kmath-prob")) - api(project(":kmath-functions")) +// api(project(":kmath-functions")) api("org.apache.commons:commons-math3:3.6.1") } diff --git a/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/chains/Chain.kt b/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/chains/Chain.kt index bf79c1601..e27290fbb 100644 --- a/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/chains/Chain.kt +++ b/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/chains/Chain.kt @@ -43,7 +43,6 @@ public interface Chain : Flow { public companion object } - public fun Iterator.asChain(): Chain = SimpleChain { next() } public fun Sequence.asChain(): Chain = iterator().asChain() @@ -51,22 +50,20 @@ public fun Sequence.asChain(): Chain = iterator().asChain() * A simple chain of independent tokens */ public class SimpleChain(private val gen: suspend () -> R) : Chain { - override suspend fun next(): R = gen() - override fun fork(): Chain = this + public override suspend fun next(): R = gen() + public override fun fork(): Chain = this } /** * A stateless Markov chain */ public class MarkovChain(private val seed: suspend () -> R, private val gen: suspend (R) -> R) : Chain { - - private val mutex = Mutex() - + private val mutex: Mutex = Mutex() private var value: R? = null public fun value(): R? = value - override suspend fun next(): R { + public override suspend fun next(): R { mutex.withLock { val newValue = gen(value ?: seed()) value = newValue @@ -74,9 +71,7 @@ public class MarkovChain(private val seed: suspend () -> R, private } } - override fun fork(): Chain { - return MarkovChain(seed = { value ?: seed() }, gen = gen) - } + public override fun fork(): Chain = MarkovChain(seed = { value ?: seed() }, gen = gen) } /** @@ -91,12 +86,11 @@ public class StatefulChain( private val gen: suspend S.(R) -> R ) : Chain { private val mutex: Mutex = Mutex() - private var value: R? = null public fun value(): R? = value - override suspend fun next(): R { + public override suspend fun next(): R { mutex.withLock { val newValue = state.gen(value ?: state.seed()) value = newValue @@ -104,16 +98,15 @@ public class StatefulChain( } } - override fun fork(): Chain = StatefulChain(forkState(state), seed, forkState, gen) + public override fun fork(): Chain = StatefulChain(forkState(state), seed, forkState, gen) } /** * A chain that repeats the same value */ public class ConstantChain(public val value: T) : Chain { - override suspend fun next(): T = value - - override fun fork(): Chain = this + public override suspend fun next(): T = value + public override fun fork(): Chain = this } /** diff --git a/kmath-functions/src/commonMain/kotlin/scientifik/kmath/functions/functions.kt b/kmath-functions/src/commonMain/kotlin/scientifik/kmath/functions/functions.kt index 7b26fd221..2b6073e07 100644 --- a/kmath-functions/src/commonMain/kotlin/scientifik/kmath/functions/functions.kt +++ b/kmath-functions/src/commonMain/kotlin/scientifik/kmath/functions/functions.kt @@ -24,7 +24,7 @@ public interface SuspendableMathFunction, R> { public suspend operator fun C.invoke(arg: T): R } -public suspend fun SuspendableMathFunction.invoke(arg: Double) = RealField.invoke(arg) +public suspend fun SuspendableMathFunction.invoke(arg: Double): R = RealField.invoke(arg) /** * A parametric function with parameter diff --git a/kmath-histograms/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt b/kmath-histograms/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt index 0acef2fb7..530dee4f2 100644 --- a/kmath-histograms/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt +++ b/kmath-histograms/src/commonMain/kotlin/scientifik/kmath/histogram/Histogram.kt @@ -29,7 +29,6 @@ public interface Histogram> : Iterable { * Dimension of the histogram */ public val dimension: Int - } public interface MutableHistogram> : Histogram { diff --git a/kmath-histograms/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt b/kmath-histograms/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt index 24c30bf4c..add01d82c 100644 --- a/kmath-histograms/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt +++ b/kmath-histograms/src/jvmMain/kotlin/scientifik/kmath/histogram/UnivariateHistogram.kt @@ -44,7 +44,7 @@ public class UnivariateHistogram private constructor(private val factory: (Doubl } private fun createBin(value: Double): UnivariateBin = factory(value).also { - synchronized(this) { bins.put(it.position, it) } + synchronized(this) { bins[it.position] = it } } public override operator fun get(point: Buffer): UnivariateBin? = get(point[0]) @@ -87,7 +87,7 @@ public class UnivariateHistogram private constructor(private val factory: (Doubl ) else -> { - val index = (0 until sorted.size).first { value > sorted[it] } + val index = sorted.indices.first { value > sorted[it] } val left = sorted[index] val right = sorted[index + 1] UnivariateBin((left + right) / 2, (right - left)) diff --git a/kmath-koma/build.gradle.kts b/kmath-koma/build.gradle.kts deleted file mode 100644 index 606113e75..000000000 --- a/kmath-koma/build.gradle.kts +++ /dev/null @@ -1,30 +0,0 @@ -plugins { id("ru.mipt.npm.mpp") } - -repositories.maven("http://dl.bintray.com/kyonifer/maven") - -kotlin.sourceSets { - commonMain { - dependencies { - api(project(":kmath-core")) - api("com.kyonifer:koma-core-api-common:0.12") - } - } - - jvmMain { - dependencies { - api("com.kyonifer:koma-core-api-jvm:0.12") - } - } - - jvmTest { - dependencies { - implementation("com.kyonifer:koma-core-ejml:0.12") - } - } - - jsMain { - dependencies { - api("com.kyonifer:koma-core-api-js:0.12") - } - } -} diff --git a/kmath-koma/src/commonMain/kotlin/scientifik.kmath.linear/KomaMatrix.kt b/kmath-koma/src/commonMain/kotlin/scientifik.kmath.linear/KomaMatrix.kt deleted file mode 100644 index 139b8cc8d..000000000 --- a/kmath-koma/src/commonMain/kotlin/scientifik.kmath.linear/KomaMatrix.kt +++ /dev/null @@ -1,105 +0,0 @@ -package scientifik.kmath.linear - -import koma.extensions.fill -import koma.matrix.MatrixFactory -import scientifik.kmath.operations.Space -import scientifik.kmath.operations.invoke -import scientifik.kmath.structures.Matrix -import scientifik.kmath.structures.NDStructure - -public class KomaMatrixContext( - private val factory: MatrixFactory>, - private val space: Space -) : MatrixContext { - public override fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> T): KomaMatrix = - KomaMatrix(factory.zeros(rows, columns).fill(initializer)) - - public fun Matrix.toKoma(): KomaMatrix = if (this is KomaMatrix) - this - else - produce(rowNum, colNum) { i, j -> get(i, j) } - - public fun Point.toKoma(): KomaVector = if (this is KomaVector) - this - else - KomaVector(factory.zeros(size, 1).fill { i, _ -> get(i) }) - - public override fun Matrix.dot(other: Matrix): KomaMatrix = - KomaMatrix(toKoma().origin * other.toKoma().origin) - - public override fun Matrix.dot(vector: Point): KomaVector = - KomaVector(toKoma().origin * vector.toKoma().origin) - - public override operator fun Matrix.unaryMinus(): KomaMatrix = - KomaMatrix(toKoma().origin.unaryMinus()) - - public override fun add(a: Matrix, b: Matrix): KomaMatrix = - KomaMatrix(a.toKoma().origin + b.toKoma().origin) - - public override operator fun Matrix.minus(b: Matrix): KomaMatrix = - KomaMatrix(toKoma().origin - b.toKoma().origin) - - public override fun multiply(a: Matrix, k: Number): Matrix = - produce(a.rowNum, a.colNum) { i, j -> space { a[i, j] * k } } - - public override operator fun Matrix.times(value: T): KomaMatrix = - KomaMatrix(toKoma().origin * value) - - public companion object -} - -public fun KomaMatrixContext.solve(a: Matrix, b: Matrix): KomaMatrix = - KomaMatrix(a.toKoma().origin.solve(b.toKoma().origin)) - -public fun KomaMatrixContext.solve(a: Matrix, b: Point): KomaVector = - KomaVector(a.toKoma().origin.solve(b.toKoma().origin)) - -public fun KomaMatrixContext.inverse(a: Matrix): KomaMatrix = - KomaMatrix(a.toKoma().origin.inv()) - -public class KomaMatrix(public val origin: koma.matrix.Matrix, features: Set? = null) : - FeaturedMatrix { - public override val rowNum: Int get() = origin.numRows() - public override val colNum: Int get() = origin.numCols() - - public override val shape: IntArray get() = intArrayOf(origin.numRows(), origin.numCols()) - - public override val features: Set = features ?: hashSetOf( - object : DeterminantFeature { - override val determinant: T get() = origin.det() - }, - - object : LUPDecompositionFeature { - private val lup by lazy { origin.LU() } - override val l: FeaturedMatrix get() = KomaMatrix(lup.second) - override val u: FeaturedMatrix get() = KomaMatrix(lup.third) - override val p: FeaturedMatrix get() = KomaMatrix(lup.first) - } - ) - - override fun suggestFeature(vararg features: MatrixFeature): FeaturedMatrix = - KomaMatrix(this.origin, this.features + features) - - override operator fun get(i: Int, j: Int): T = origin.getGeneric(i, j) - - override fun equals(other: Any?): Boolean { - return NDStructure.equals(this, other as? NDStructure<*> ?: return false) - } - - override fun hashCode(): Int { - var result = origin.hashCode() - result = 31 * result + features.hashCode() - return result - } -} - -public class KomaVector internal constructor(public val origin: koma.matrix.Matrix) : Point { - override val size: Int get() = origin.numRows() - - init { - require(origin.numCols() == 1) { error("Only single column matrices are allowed") } - } - - override operator fun get(index: Int): T = origin.getGeneric(index) - override operator fun iterator(): Iterator = origin.toIterable().iterator() -} diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/Distribution.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/Distribution.kt index e1f0e8d6e..3c4d0420f 100644 --- a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/Distribution.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/Distribution.kt @@ -73,5 +73,5 @@ public fun Sampler.sampleBuffer( /** * Generate a bunch of samples from real distributions */ -public fun Sampler.sampleBuffer(generator: RandomGenerator, size: Int) = +public fun Sampler.sampleBuffer(generator: RandomGenerator, size: Int): Chain> = sampleBuffer(generator, size, Buffer.Companion::real) diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/FactorizedDistribution.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/FactorizedDistribution.kt index ea526c058..05b62deb8 100644 --- a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/FactorizedDistribution.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/FactorizedDistribution.kt @@ -6,42 +6,38 @@ import scientifik.kmath.chains.SimpleChain /** * A multivariate distribution which takes a map of parameters */ -interface NamedDistribution : Distribution> +public interface NamedDistribution : Distribution> /** * A multivariate distribution that has independent distributions for separate axis */ -class FactorizedDistribution(val distributions: Collection>) : NamedDistribution { - - override fun probability(arg: Map): Double { - return distributions.fold(1.0) { acc, distr -> acc * distr.probability(arg) } - } +public class FactorizedDistribution(public val distributions: Collection>) : + NamedDistribution { + override fun probability(arg: Map): Double = + distributions.fold(1.0) { acc, distr -> acc * distr.probability(arg) } override fun sample(generator: RandomGenerator): Chain> { val chains = distributions.map { it.sample(generator) } - return SimpleChain> { - chains.fold(emptyMap()) { acc, chain -> acc + chain.next() } - } + return SimpleChain { chains.fold(emptyMap()) { acc, chain -> acc + chain.next() } } } } -class NamedDistributionWrapper(val name: String, val distribution: Distribution) : NamedDistribution { +public class NamedDistributionWrapper(public val name: String, public val distribution: Distribution) : + NamedDistribution { override fun probability(arg: Map): Double = distribution.probability( arg[name] ?: error("Argument with name $name not found in input parameters") ) override fun sample(generator: RandomGenerator): Chain> { val chain = distribution.sample(generator) - return SimpleChain { - mapOf(name to chain.next()) - } + return SimpleChain { mapOf(name to chain.next()) } } } -class DistributionBuilder{ +public class DistributionBuilder { private val distributions = ArrayList>() - infix fun String.to(distribution: Distribution){ - distributions.add(NamedDistributionWrapper(this,distribution)) + public infix fun String.to(distribution: Distribution) { + distributions.add(NamedDistributionWrapper(this, distribution)) } -} \ No newline at end of file +} diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/RandomChain.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/RandomChain.kt index 49163c701..4fc7fe241 100644 --- a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/RandomChain.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/RandomChain.kt @@ -5,10 +5,13 @@ import scientifik.kmath.chains.Chain /** * A possibly stateful chain producing random values. */ -class RandomChain(val generator: RandomGenerator, private val gen: suspend RandomGenerator.() -> R) : Chain { +public class RandomChain( + public val generator: RandomGenerator, + private val gen: suspend RandomGenerator.() -> R +) : Chain { override suspend fun next(): R = generator.gen() override fun fork(): Chain = RandomChain(generator.fork(), gen) } -fun RandomGenerator.chain(gen: suspend RandomGenerator.() -> R): RandomChain = RandomChain(this, gen) +public fun RandomGenerator.chain(gen: suspend RandomGenerator.() -> R): RandomChain = RandomChain(this, gen) diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/RandomGenerator.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/RandomGenerator.kt index 2a225fe47..ecf9beeb1 100644 --- a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/RandomGenerator.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/RandomGenerator.kt @@ -5,17 +5,15 @@ import kotlin.random.Random /** * A basic generator */ -interface RandomGenerator { - fun nextBoolean(): Boolean - - fun nextDouble(): Double - fun nextInt(): Int - fun nextInt(until: Int): Int - fun nextLong(): Long - fun nextLong(until: Long): Long - - fun fillBytes(array: ByteArray, fromIndex: Int = 0, toIndex: Int = array.size) - fun nextBytes(size: Int): ByteArray = ByteArray(size).also { fillBytes(it) } +public interface RandomGenerator { + public fun nextBoolean(): Boolean + public fun nextDouble(): Double + public fun nextInt(): Int + public fun nextInt(until: Int): Int + public fun nextLong(): Long + public fun nextLong(until: Long): Long + public fun fillBytes(array: ByteArray, fromIndex: Int = 0, toIndex: Int = array.size) + public fun nextBytes(size: Int): ByteArray = ByteArray(size).also { fillBytes(it) } /** * Create a new generator which is independent from current generator (operations on new generator do not affect this one @@ -24,32 +22,27 @@ interface RandomGenerator { * * The thread safety of this operation is not guaranteed since it could affect the state of the generator. */ - fun fork(): RandomGenerator + public fun fork(): RandomGenerator - companion object { - val default by lazy { DefaultGenerator() } + public companion object { + public val default: DefaultGenerator by lazy { DefaultGenerator() } - fun default(seed: Long) = DefaultGenerator(Random(seed)) + public fun default(seed: Long): DefaultGenerator = DefaultGenerator(Random(seed)) } } -inline class DefaultGenerator(val random: Random = Random) : RandomGenerator { - override fun nextBoolean(): Boolean = random.nextBoolean() +public inline class DefaultGenerator(public val random: Random = Random) : RandomGenerator { + public override fun nextBoolean(): Boolean = random.nextBoolean() + public override fun nextDouble(): Double = random.nextDouble() + public override fun nextInt(): Int = random.nextInt() + public override fun nextInt(until: Int): Int = random.nextInt(until) + public override fun nextLong(): Long = random.nextLong() + public override fun nextLong(until: Long): Long = random.nextLong(until) - override fun nextDouble(): Double = random.nextDouble() - - override fun nextInt(): Int = random.nextInt() - override fun nextInt(until: Int): Int = random.nextInt(until) - - override fun nextLong(): Long = random.nextLong() - - override fun nextLong(until: Long): Long = random.nextLong(until) - - override fun fillBytes(array: ByteArray, fromIndex: Int, toIndex: Int) { + public override fun fillBytes(array: ByteArray, fromIndex: Int, toIndex: Int) { random.nextBytes(array, fromIndex, toIndex) } - override fun nextBytes(size: Int): ByteArray = random.nextBytes(size) - - override fun fork(): RandomGenerator = RandomGenerator.default(random.nextLong()) -} \ No newline at end of file + public override fun nextBytes(size: Int): ByteArray = random.nextBytes(size) + public override fun fork(): RandomGenerator = RandomGenerator.default(random.nextLong()) +} diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/SamplerAlgebra.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/SamplerAlgebra.kt index 02f98439e..184665928 100644 --- a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/SamplerAlgebra.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/SamplerAlgebra.kt @@ -7,26 +7,25 @@ import scientifik.kmath.chains.zip import scientifik.kmath.operations.Space import scientifik.kmath.operations.invoke -class BasicSampler(val chainBuilder: (RandomGenerator) -> Chain) : Sampler { - override fun sample(generator: RandomGenerator): Chain = chainBuilder(generator) +public class BasicSampler(public val chainBuilder: (RandomGenerator) -> Chain) : Sampler { + public override fun sample(generator: RandomGenerator): Chain = chainBuilder(generator) } -class ConstantSampler(val value: T) : Sampler { - override fun sample(generator: RandomGenerator): Chain = ConstantChain(value) +public class ConstantSampler(public val value: T) : Sampler { + public override fun sample(generator: RandomGenerator): Chain = ConstantChain(value) } /** * A space for samplers. Allows to perform simple operations on distributions */ -class SamplerSpace(val space: Space) : Space> { +public class SamplerSpace(public val space: Space) : Space> { + public override val zero: Sampler = ConstantSampler(space.zero) - override val zero: Sampler = ConstantSampler(space.zero) - - override fun add(a: Sampler, b: Sampler): Sampler = BasicSampler { generator -> + public override fun add(a: Sampler, b: Sampler): Sampler = BasicSampler { generator -> a.sample(generator).zip(b.sample(generator)) { aValue, bValue -> space { aValue + bValue } } } - override fun multiply(a: Sampler, k: Number): Sampler = BasicSampler { generator -> + public override fun multiply(a: Sampler, k: Number): Sampler = BasicSampler { generator -> a.sample(generator).map { space { it * k.toDouble() } } } } diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/Statistic.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/Statistic.kt index c82d262bf..7811b88f6 100644 --- a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/Statistic.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/Statistic.kt @@ -6,7 +6,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.scanReduce +import kotlinx.coroutines.flow.runningReduce import scientifik.kmath.coroutines.mapParallel import scientifik.kmath.operations.* import scientifik.kmath.structures.Buffer @@ -16,8 +16,8 @@ import scientifik.kmath.structures.asSequence /** * A function, that transforms a buffer of random quantities to some resulting value */ -interface Statistic { - suspend operator fun invoke(data: Buffer): R +public interface Statistic { + public suspend operator fun invoke(data: Buffer): R } /** @@ -26,17 +26,17 @@ interface Statistic { * @param I - intermediate block type * @param R - result type */ -interface ComposableStatistic : Statistic { +public interface ComposableStatistic : Statistic { //compute statistic on a single block - suspend fun computeIntermediate(data: Buffer): I + public suspend fun computeIntermediate(data: Buffer): I //Compose two blocks - suspend fun composeIntermediate(first: I, second: I): I + public suspend fun composeIntermediate(first: I, second: I): I //Transform block to result - suspend fun toResult(intermediate: I): R + public suspend fun toResult(intermediate: I): R - override suspend fun invoke(data: Buffer): R = toResult(computeIntermediate(data)) + public override suspend fun invoke(data: Buffer): R = toResult(computeIntermediate(data)) } @FlowPreview @@ -46,7 +46,7 @@ private fun ComposableStatistic.flowIntermediate( dispatcher: CoroutineDispatcher = Dispatchers.Default ): Flow = flow .mapParallel(dispatcher) { computeIntermediate(it) } - .scanReduce(::composeIntermediate) + .runningReduce(::composeIntermediate) /** @@ -57,7 +57,7 @@ private fun ComposableStatistic.flowIntermediate( */ @FlowPreview @ExperimentalCoroutinesApi -fun ComposableStatistic.flow( +public fun ComposableStatistic.flow( flow: Flow>, dispatcher: CoroutineDispatcher = Dispatchers.Default ): Flow = flowIntermediate(flow, dispatcher).map(::toResult) @@ -65,32 +65,32 @@ fun ComposableStatistic.flow( /** * Arithmetic mean */ -class Mean(val space: Space) : ComposableStatistic, T> { - override suspend fun computeIntermediate(data: Buffer): Pair = +public class Mean(public val space: Space) : ComposableStatistic, T> { + public override suspend fun computeIntermediate(data: Buffer): Pair = space { sum(data.asIterable()) } to data.size - override suspend fun composeIntermediate(first: Pair, second: Pair): Pair = + public override suspend fun composeIntermediate(first: Pair, second: Pair): Pair = space { first.first + second.first } to (first.second + second.second) - override suspend fun toResult(intermediate: Pair): T = + public override suspend fun toResult(intermediate: Pair): T = space { intermediate.first / intermediate.second } - companion object { + public companion object { //TODO replace with optimized version which respects overflow - val real: Mean = Mean(RealField) - val int: Mean = Mean(IntRing) - val long: Mean = Mean(LongRing) + public val real: Mean = Mean(RealField) + public val int: Mean = Mean(IntRing) + public val long: Mean = Mean(LongRing) } } /** * Non-composable median */ -class Median(private val comparator: Comparator) : Statistic { - override suspend fun invoke(data: Buffer): T = +public class Median(private val comparator: Comparator) : Statistic { + public override suspend fun invoke(data: Buffer): T = data.asSequence().sortedWith(comparator).toList()[data.size / 2] //TODO check if this is correct - companion object { - val real: Median = Median(Comparator { a: Double, b: Double -> a.compareTo(b) }) + public companion object { + public val real: Median = Median { a: Double, b: Double -> a.compareTo(b) } } } diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/UniformDistribution.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/UniformDistribution.kt index 9d96bff59..10445219d 100644 --- a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/UniformDistribution.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/UniformDistribution.kt @@ -3,32 +3,20 @@ package scientifik.kmath.prob import scientifik.kmath.chains.Chain import scientifik.kmath.chains.SimpleChain -class UniformDistribution(val range: ClosedFloatingPointRange) : UnivariateDistribution { +public class UniformDistribution(public val range: ClosedFloatingPointRange) : UnivariateDistribution { + private val length: Double = range.endInclusive - range.start - private val length = range.endInclusive - range.start + override fun probability(arg: Double): Double = if (arg in range) 1.0 / length else 0.0 - override fun probability(arg: Double): Double { - return if (arg in range) { - return 1.0 / length - } else { - 0.0 - } - } + override fun sample(generator: RandomGenerator): Chain = + SimpleChain { range.start + generator.nextDouble() * length } - override fun sample(generator: RandomGenerator): Chain { - return SimpleChain { - range.start + generator.nextDouble() * length - } - } - - override fun cumulative(arg: Double): Double { - return when { - arg < range.start -> 0.0 - arg >= range.endInclusive -> 1.0 - else -> (arg - range.start) / length - } + override fun cumulative(arg: Double): Double = when { + arg < range.start -> 0.0 + arg >= range.endInclusive -> 1.0 + else -> (arg - range.start) / length } } -fun Distribution.Companion.uniform(range: ClosedFloatingPointRange): UniformDistribution = - UniformDistribution(range) \ No newline at end of file +public fun Distribution.Companion.uniform(range: ClosedFloatingPointRange): UniformDistribution = + UniformDistribution(range) diff --git a/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/distributions.kt b/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/distributions.kt index b3a37ada1..923317ed2 100644 --- a/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/distributions.kt +++ b/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/distributions.kt @@ -15,8 +15,8 @@ public abstract class ContinuousSamplerDistribution : Distribution { private inner class ContinuousSamplerChain(val generator: RandomGenerator) : BlockingRealChain() { private val sampler = buildCMSampler(generator) - public override fun nextDouble(): Double = sampler.sample() - public override fun fork(): Chain = ContinuousSamplerChain(generator.fork()) + override fun nextDouble(): Double = sampler.sample() + override fun fork(): Chain = ContinuousSamplerChain(generator.fork()) } protected abstract fun buildCMSampler(generator: RandomGenerator): ContinuousSampler @@ -28,8 +28,8 @@ public abstract class DiscreteSamplerDistribution : Distribution { private inner class ContinuousSamplerChain(val generator: RandomGenerator) : BlockingIntChain() { private val sampler = buildSampler(generator) - public override fun nextInt(): Int = sampler.sample() - public override fun fork(): Chain = ContinuousSamplerChain(generator.fork()) + override fun nextInt(): Int = sampler.sample() + override fun fork(): Chain = ContinuousSamplerChain(generator.fork()) } protected abstract fun buildSampler(generator: RandomGenerator): DiscreteSampler @@ -58,9 +58,7 @@ public fun Distribution.Companion.normal( return normalSampler(method, provider) } - override fun probability(arg: Double): Double { - return exp(-arg.pow(2) / 2) / sqrt(PI * 2) - } + override fun probability(arg: Double): Double = exp(-arg.pow(2) / 2) / sqrt(PI * 2) } public fun Distribution.Companion.normal( diff --git a/settings.gradle.kts b/settings.gradle.kts index 53e3ece3c..2e40b2abc 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -26,14 +26,12 @@ rootProject.name = "kmath" include( ":kmath-memory", ":kmath-core", - ":kmath-functions", +// ":kmath-functions", ":kmath-coroutines", ":kmath-histograms", ":kmath-commons", ":kmath-viktor", - ":kmath-koma", ":kmath-prob", - ":kmath-io", ":kmath-dimensions", ":kmath-for-real", ":kmath-geometry",