From 612f6f00828e3595773d964902daa2c96e48fa4b Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Fri, 16 Oct 2020 16:49:47 +0700 Subject: [PATCH] Refactor, remove unused files, remove BasicSampler --- .../structures/StructureReadBenchmark.kt | 2 +- kmath-core/build.gradle.kts | 12 ++- .../kscience/kmath/operations/BigInt.kt | 23 ++--- .../kscience/kmath/structures/Buffers.kt | 2 +- .../kscience/kmath/structures/Structure2D.kt | 3 +- .../kmath/structures/NumberNDFieldTest.kt | 3 +- .../kotlin/kscience/kmath/chains/Chain.kt | 20 ++--- .../kscience/kmath/streaming/BufferFlow.kt | 2 +- .../kscience/kmath/streaming/RingBuffer.kt | 8 +- .../kscience/kmath/functions/Piecewise.kt | 6 +- .../kscience/kmath/prob/Distribution.kt | 23 ++++- .../kmath/prob/FactorizedDistribution.kt | 2 +- .../kscience/kmath/prob/RandomGenerator.kt | 2 + .../kscience/kmath/prob/SamplerAlgebra.kt | 17 ++-- .../kmath/prob/internal/InternalGamma.kt | 31 +++---- .../kmath/prob/internal/InternalUtils.kt | 24 ++---- .../samplers/AliasMethodDiscreteSampler.kt | 4 +- .../kmath/prob/samplers/InternalGamma.kt | 2 - .../kmath/prob/samplers/InternalUtils.kt | 2 - .../ZigguratNormalizedGaussianSampler.kt | 43 ++++------ .../kmath/prob/RandomSourceGenerator.kt | 83 ++++++++++++++++++- .../kotlin/kscience/kmath/prob/SamplerTest.kt | 7 +- 22 files changed, 194 insertions(+), 127 deletions(-) delete mode 100644 kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/InternalGamma.kt delete mode 100644 kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/InternalUtils.kt diff --git a/examples/src/main/kotlin/kscience/kmath/structures/StructureReadBenchmark.kt b/examples/src/main/kotlin/kscience/kmath/structures/StructureReadBenchmark.kt index 51fd4f956..c8c5d7b56 100644 --- a/examples/src/main/kotlin/kscience/kmath/structures/StructureReadBenchmark.kt +++ b/examples/src/main/kotlin/kscience/kmath/structures/StructureReadBenchmark.kt @@ -31,4 +31,4 @@ fun main() { strides.indices().forEach { res = array[strides.offset(it)] } } println("Array reading finished in $time3 millis") -} \ No newline at end of file +} diff --git a/kmath-core/build.gradle.kts b/kmath-core/build.gradle.kts index b56151abe..79cfce1f7 100644 --- a/kmath-core/build.gradle.kts +++ b/kmath-core/build.gradle.kts @@ -1,3 +1,5 @@ +import ru.mipt.npm.gradle.Maturity + plugins { id("ru.mipt.npm.mpp") id("ru.mipt.npm.native") @@ -11,36 +13,42 @@ kotlin.sourceSets.commonMain { readme { description = "Core classes, algebra definitions, basic linear algebra" - maturity = ru.mipt.npm.gradle.Maturity.DEVELOPMENT + maturity = Maturity.DEVELOPMENT propertyByTemplate("artifact", rootProject.file("docs/templates/ARTIFACT-TEMPLATE.md")) + feature( id = "algebras", description = "Algebraic structures: contexts and elements", ref = "src/commonMain/kotlin/kscience/kmath/operations/Algebra.kt" ) + feature( id = "nd", description = "Many-dimensional structures", ref = "src/commonMain/kotlin/kscience/kmath/structures/NDStructure.kt" ) + feature( id = "buffers", description = "One-dimensional structure", ref = "src/commonMain/kotlin/kscience/kmath/structures/Buffers.kt" ) + feature( id = "expressions", description = "Functional Expressions", ref = "src/commonMain/kotlin/kscience/kmath/expressions" ) + feature( id = "domains", description = "Domains", ref = "src/commonMain/kotlin/kscience/kmath/domains" ) + feature( id = "autodif", description = "Automatic differentiation", ref = "src/commonMain/kotlin/kscience/kmath/misc/AutoDiff.kt" ) -} \ No newline at end of file +} diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/operations/BigInt.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/operations/BigInt.kt index 20f289596..7af207edc 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/operations/BigInt.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/operations/BigInt.kt @@ -237,18 +237,18 @@ public class BigInt internal constructor( ) private fun compareMagnitudes(mag1: Magnitude, mag2: Magnitude): Int { - when { - mag1.size > mag2.size -> return 1 - mag1.size < mag2.size -> return -1 + return when { + mag1.size > mag2.size -> 1 + mag1.size < mag2.size -> -1 + else -> { - for (i in mag1.size - 1 downTo 0) { - if (mag1[i] > mag2[i]) { - return 1 - } else if (mag1[i] < mag2[i]) { - return -1 - } + for (i in mag1.size - 1 downTo 0) return when { + mag1[i] > mag2[i] -> 1 + mag1[i] < mag2[i] -> -1 + else -> continue } - return 0 + + 0 } } } @@ -298,10 +298,11 @@ public class BigInt internal constructor( var carry = 0uL for (i in mag.indices) { - val cur: ULong = carry + mag[i].toULong() * x.toULong() + val cur = carry + mag[i].toULong() * x.toULong() result[i] = (cur and BASE).toUInt() carry = cur shr BASE_SIZE } + result[resultLength - 1] = (carry and BASE).toUInt() return stripLeadingZeros(result) diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Buffers.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Buffers.kt index 5174eb314..1806e3559 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Buffers.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Buffers.kt @@ -71,7 +71,7 @@ public interface Buffer { @Suppress("UNCHECKED_CAST") public inline fun auto(type: KClass, size: Int, initializer: (Int) -> T): Buffer = when (type) { - Double::class -> RealBuffer(size) { initializer(it) as Double } as Buffer + Double::class -> real(size) { initializer(it) as Double } as Buffer Short::class -> ShortBuffer(size) { initializer(it) as Short } as Buffer Int::class -> IntBuffer(size) { initializer(it) as Int } as Buffer Long::class -> LongBuffer(size) { initializer(it) as Long } as Buffer diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Structure2D.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Structure2D.kt index 25fdf3f3d..99c1de9bf 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Structure2D.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/structures/Structure2D.kt @@ -22,7 +22,7 @@ public interface Structure2D : NDStructure { override fun elements(): Sequence> = sequence { for (i in (0 until rowNum)) - for (j in (0 until colNum)) yield(intArrayOf(i, j) to get(i, j)) + for (j in (0 until colNum)) yield(intArrayOf(i, j) to this@Structure2D[i, j]) } public companion object @@ -35,7 +35,6 @@ private inline class Structure2DWrapper(val structure: NDStructure) : Stru override val shape: IntArray get() = structure.shape override operator fun get(i: Int, j: Int): T = structure[i, j] - override fun elements(): Sequence> = structure.elements() } diff --git a/kmath-core/src/commonTest/kotlin/kscience/kmath/structures/NumberNDFieldTest.kt b/kmath-core/src/commonTest/kotlin/kscience/kmath/structures/NumberNDFieldTest.kt index f5e008ef3..4320821e6 100644 --- a/kmath-core/src/commonTest/kotlin/kscience/kmath/structures/NumberNDFieldTest.kt +++ b/kmath-core/src/commonTest/kotlin/kscience/kmath/structures/NumberNDFieldTest.kt @@ -29,12 +29,11 @@ class NumberNDFieldTest { val array = real2D(3, 3) { i, j -> (i * 10 + j).toDouble() } - for (i in 0..2) { + for (i in 0..2) for (j in 0..2) { val expected = (i * 10 + j).toDouble() assertEquals(expected, array[i, j], "Error at index [$i, $j]") } - } } @Test diff --git a/kmath-coroutines/src/commonMain/kotlin/kscience/kmath/chains/Chain.kt b/kmath-coroutines/src/commonMain/kotlin/kscience/kmath/chains/Chain.kt index 8c15e52c7..9b9f3e509 100644 --- a/kmath-coroutines/src/commonMain/kotlin/kscience/kmath/chains/Chain.kt +++ b/kmath-coroutines/src/commonMain/kotlin/kscience/kmath/chains/Chain.kt @@ -63,12 +63,10 @@ public class MarkovChain(private val seed: suspend () -> R, private public fun value(): R? = value - public override suspend fun next(): R { - mutex.withLock { - val newValue = gen(value ?: seed()) - value = newValue - return newValue - } + public override suspend fun next(): R = mutex.withLock { + val newValue = gen(value ?: seed()) + value = newValue + newValue } public override fun fork(): Chain = MarkovChain(seed = { value ?: seed() }, gen = gen) @@ -90,12 +88,10 @@ public class StatefulChain( public fun value(): R? = value - public override suspend fun next(): R { - mutex.withLock { - val newValue = state.gen(value ?: state.seed()) - value = newValue - return newValue - } + public override suspend fun next(): R = mutex.withLock { + val newValue = state.gen(value ?: state.seed()) + value = newValue + newValue } public override fun fork(): Chain = StatefulChain(forkState(state), seed, forkState, gen) diff --git a/kmath-coroutines/src/commonMain/kotlin/kscience/kmath/streaming/BufferFlow.kt b/kmath-coroutines/src/commonMain/kotlin/kscience/kmath/streaming/BufferFlow.kt index 328a7807c..7be86b6e7 100644 --- a/kmath-coroutines/src/commonMain/kotlin/kscience/kmath/streaming/BufferFlow.kt +++ b/kmath-coroutines/src/commonMain/kotlin/kscience/kmath/streaming/BufferFlow.kt @@ -28,7 +28,7 @@ public fun Flow.chunked(bufferSize: Int, bufferFactory: BufferFactory) var counter = 0 this@chunked.collect { element -> - list.add(element) + list += element counter++ if (counter == bufferSize) { diff --git a/kmath-coroutines/src/commonMain/kotlin/kscience/kmath/streaming/RingBuffer.kt b/kmath-coroutines/src/commonMain/kotlin/kscience/kmath/streaming/RingBuffer.kt index 385bbaae2..a59979238 100644 --- a/kmath-coroutines/src/commonMain/kotlin/kscience/kmath/streaming/RingBuffer.kt +++ b/kmath-coroutines/src/commonMain/kotlin/kscience/kmath/streaming/RingBuffer.kt @@ -48,11 +48,9 @@ public class RingBuffer( /** * A safe snapshot operation */ - public suspend fun snapshot(): Buffer { - mutex.withLock { - val copy = buffer.copy() - return VirtualBuffer(size) { i -> copy[startIndex.forward(i)] as T } - } + public suspend fun snapshot(): Buffer = mutex.withLock { + val copy = buffer.copy() + VirtualBuffer(size) { i -> copy[startIndex.forward(i)] as T } } public suspend fun push(element: T) { diff --git a/kmath-functions/src/commonMain/kotlin/kscience/kmath/functions/Piecewise.kt b/kmath-functions/src/commonMain/kotlin/kscience/kmath/functions/Piecewise.kt index a8c020c05..6dab9820d 100644 --- a/kmath-functions/src/commonMain/kotlin/kscience/kmath/functions/Piecewise.kt +++ b/kmath-functions/src/commonMain/kotlin/kscience/kmath/functions/Piecewise.kt @@ -23,8 +23,8 @@ public class OrderedPiecewisePolynomial>(delimiter: T) : */ public fun putRight(right: T, piece: Polynomial) { require(right > delimiters.last()) { "New delimiter should be to the right of old one" } - delimiters.add(right) - pieces.add(piece) + delimiters += right + pieces += piece } public fun putLeft(left: T, piece: Polynomial) { @@ -52,4 +52,4 @@ public class OrderedPiecewisePolynomial>(delimiter: T) : public fun , C : Ring> PiecewisePolynomial.value(ring: C, arg: T): T? = findPiece(arg)?.value(ring, arg) -public fun , C : Ring> PiecewisePolynomial.asFunction(ring: C): (T) -> T? = { value(ring, it) } \ No newline at end of file +public fun , C : Ring> PiecewisePolynomial.asFunction(ring: C): (T) -> T? = { value(ring, it) } diff --git a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/Distribution.kt b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/Distribution.kt index bbb1de1e3..6c64fb6f5 100644 --- a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/Distribution.kt +++ b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/Distribution.kt @@ -5,8 +5,18 @@ import kscience.kmath.chains.Chain import kscience.kmath.chains.collect import kscience.kmath.structures.Buffer import kscience.kmath.structures.BufferFactory +import kscience.kmath.structures.IntBuffer +/** + * Sampler that generates chains of values of type [T]. + */ public fun interface Sampler { + /** + * Generates a chain of samples. + * + * @param generator the randomness provider. + * @return the new chain. + */ public fun sample(generator: RandomGenerator): Chain } @@ -59,16 +69,25 @@ public fun Sampler.sampleBuffer( //clear list from previous run tmp.clear() //Fill list - repeat(size) { tmp.add(chain.next()) } + repeat(size) { tmp += chain.next() } //return new buffer with elements from tmp bufferFactory(size) { tmp[it] } } } +/** + * Samples one value from this [Sampler]. + */ public suspend fun Sampler.next(generator: RandomGenerator): T = sample(generator).first() /** - * Generate a bunch of samples from real distributions + * Generates [size] real samples and chunks them into some buffers. */ public fun Sampler.sampleBuffer(generator: RandomGenerator, size: Int): Chain> = sampleBuffer(generator, size, Buffer.Companion::real) + +/** + * Generates [size] integer samples and chunks them into some buffers. + */ +public fun Sampler.sampleBuffer(generator: RandomGenerator, size: Int): Chain> = + sampleBuffer(generator, size, ::IntBuffer) diff --git a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/FactorizedDistribution.kt b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/FactorizedDistribution.kt index 128b284be..cc1f0efab 100644 --- a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/FactorizedDistribution.kt +++ b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/FactorizedDistribution.kt @@ -38,6 +38,6 @@ public class DistributionBuilder { private val distributions = ArrayList>() public infix fun String.to(distribution: Distribution) { - distributions.add(NamedDistributionWrapper(this, distribution)) + distributions += NamedDistributionWrapper(this, distribution) } } diff --git a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/RandomGenerator.kt b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/RandomGenerator.kt index 2dd4ce51e..ec660fe46 100644 --- a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/RandomGenerator.kt +++ b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/RandomGenerator.kt @@ -82,6 +82,8 @@ public interface RandomGenerator { /** * Implements [RandomGenerator] by delegating all operations to [Random]. + * + * @property random the underlying [Random] object. */ public inline class DefaultGenerator(public val random: Random = Random) : RandomGenerator { public override fun nextBoolean(): Boolean = random.nextBoolean() diff --git a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/SamplerAlgebra.kt b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/SamplerAlgebra.kt index e363ba30b..7660f2ea0 100644 --- a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/SamplerAlgebra.kt +++ b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/SamplerAlgebra.kt @@ -7,25 +7,28 @@ import kscience.kmath.chains.zip import kscience.kmath.operations.Space import kscience.kmath.operations.invoke -public class BasicSampler(public val chainBuilder: (RandomGenerator) -> Chain) : Sampler { - public override fun sample(generator: RandomGenerator): Chain = chainBuilder(generator) -} - +/** + * Implements [Sampler] by sampling only certain [value]. + * + * @property value the value to sample. + */ 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 + * A space of samplers. Allows to perform simple operations on distributions. + * + * @property space the space to provide addition and scalar multiplication for [T]. */ public class SamplerSpace(public val space: Space) : Space> { public override val zero: Sampler = ConstantSampler(space.zero) - public override fun add(a: Sampler, b: Sampler): Sampler = BasicSampler { generator -> + public override fun add(a: Sampler, b: Sampler): Sampler = Sampler { generator -> a.sample(generator).zip(b.sample(generator)) { aValue, bValue -> space { aValue + bValue } } } - public override fun multiply(a: Sampler, k: Number): Sampler = BasicSampler { generator -> + public override fun multiply(a: Sampler, k: Number): Sampler = Sampler { generator -> a.sample(generator).map { space { it * k.toDouble() } } } } diff --git a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/internal/InternalGamma.kt b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/internal/InternalGamma.kt index 7480d2396..8a73659db 100644 --- a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/internal/InternalGamma.kt +++ b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/internal/InternalGamma.kt @@ -98,28 +98,21 @@ internal object InternalGamma { private const val INV_GAMMA1P_M1_C12 = .113302723198169588237412962033074E-05 private const val INV_GAMMA1P_M1_C13 = -.205633841697760710345015413002057E-06 - fun logGamma(x: Double): Double { - val ret: Double + fun logGamma(x: Double): Double = when { + x.isNaN() || x <= 0.0 -> Double.NaN + x < 0.5 -> logGamma1p(x) - ln(x) + x <= 2.5 -> logGamma1p(x - 0.5 - 0.5) - when { - x.isNaN() || x <= 0.0 -> ret = Double.NaN - x < 0.5 -> return logGamma1p(x) - ln(x) - x <= 2.5 -> return logGamma1p(x - 0.5 - 0.5) - - x <= 8.0 -> { - val n = floor(x - 1.5).toInt() - var prod = 1.0 - (1..n).forEach { i -> prod *= x - i } - return logGamma1p(x - (n + 1)) + ln(prod) - } - - else -> { - val tmp = x + LANCZOS_G + .5 - ret = (x + .5) * ln(tmp) - tmp + HALF_LOG_2_PI + ln(lanczos(x) / x) - } + x <= 8.0 -> { + val n = floor(x - 1.5).toInt() + val prod = (1..n).fold(1.0, { prod, i -> prod * (x - i) }) + logGamma1p(x - (n + 1)) + ln(prod) } - return ret + else -> { + val tmp = x + LANCZOS_G + .5 + (x + .5) * ln(tmp) - tmp + HALF_LOG_2_PI + ln(lanczos(x) / x) + } } private fun regularizedGammaP( diff --git a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/internal/InternalUtils.kt b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/internal/InternalUtils.kt index 655e284c0..837c9796c 100644 --- a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/internal/InternalUtils.kt +++ b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/internal/InternalUtils.kt @@ -20,24 +20,17 @@ internal object InternalUtils { fun validateProbabilities(probabilities: DoubleArray?): Double { require(!(probabilities == null || probabilities.isEmpty())) { "Probabilities must not be empty." } - var sumProb = 0.0 - probabilities.forEach { prob -> - validateProbability(prob) - sumProb += prob + val sumProb = probabilities.sumByDouble { prob -> + require(!(prob < 0 || prob.isInfinite() || prob.isNaN())) { "Invalid probability: $prob" } + prob } require(!(sumProb.isInfinite() || sumProb <= 0)) { "Invalid sum of probabilities: $sumProb" } return sumProb } - private fun validateProbability(probability: Double): Unit = - require(!(probability < 0 || probability.isInfinite() || probability.isNaN())) { "Invalid probability: $probability" } - - class FactorialLog private constructor( - numValues: Int, - cache: DoubleArray? - ) { + class FactorialLog private constructor(numValues: Int, cache: DoubleArray?) { private val logFactorials: DoubleArray = DoubleArray(numValues) init { @@ -46,14 +39,15 @@ internal object InternalUtils { if (cache != null && cache.size > BEGIN_LOG_FACTORIALS) { // Copy available values. endCopy = min(cache.size, numValues) + cache.copyInto( logFactorials, BEGIN_LOG_FACTORIALS, BEGIN_LOG_FACTORIALS, endCopy ) - } + } else // All values to be computed - else endCopy = BEGIN_LOG_FACTORIALS + endCopy = BEGIN_LOG_FACTORIALS // Compute remaining values. (endCopy until numValues).forEach { i -> @@ -65,9 +59,7 @@ internal object InternalUtils { } fun value(n: Int): Double { - if (n < logFactorials.size) - return logFactorials[n] - + if (n < logFactorials.size) return logFactorials[n] return if (n < FACTORIALS.size) ln(FACTORIALS[n].toDouble()) else InternalGamma.logGamma(n + 1.0) } diff --git a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/AliasMethodDiscreteSampler.kt b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/AliasMethodDiscreteSampler.kt index f04b5bcb0..db78a41a8 100644 --- a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/AliasMethodDiscreteSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/AliasMethodDiscreteSampler.kt @@ -42,7 +42,7 @@ public open class AliasMethodDiscreteSampler private constructor( protected val alias: IntArray ) : Sampler { - private class SmallTableAliasMethodDiscreteSampler internal constructor( + private class SmallTableAliasMethodDiscreteSampler( probability: LongArray, alias: IntArray ) : AliasMethodDiscreteSampler(probability, alias) { @@ -71,7 +71,7 @@ public open class AliasMethodDiscreteSampler private constructor( } } - override fun sample(generator: RandomGenerator): Chain = generator.chain { + public override fun sample(generator: RandomGenerator): Chain = generator.chain { // This implements the algorithm as per Vose (1991): // v = uniform() in [0, 1) // j = uniform(n) in [0, n) diff --git a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/InternalGamma.kt b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/InternalGamma.kt deleted file mode 100644 index d970d1447..000000000 --- a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/InternalGamma.kt +++ /dev/null @@ -1,2 +0,0 @@ -package kscience.kmath.prob.samplers - diff --git a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/InternalUtils.kt b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/InternalUtils.kt deleted file mode 100644 index d970d1447..000000000 --- a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/InternalUtils.kt +++ /dev/null @@ -1,2 +0,0 @@ -package kscience.kmath.prob.samplers - diff --git a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/ZigguratNormalizedGaussianSampler.kt b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/ZigguratNormalizedGaussianSampler.kt index c9103ba86..60407e604 100644 --- a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/ZigguratNormalizedGaussianSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/samplers/ZigguratNormalizedGaussianSampler.kt @@ -26,35 +26,24 @@ public class ZigguratNormalizedGaussianSampler private constructor() : public override fun sample(generator: RandomGenerator): Chain = generator.chain { sampleOne(this) } public override fun toString(): String = "Ziggurat normalized Gaussian deviate" - private fun fix( - generator: RandomGenerator, - hz: Long, - iz: Int - ): Double { - var x: Double - var y: Double - x = hz * W[iz] + private fun fix(generator: RandomGenerator, hz: Long, iz: Int): Double { + var x = hz * W[iz] - return if (iz == 0) { - // Base strip. - // This branch is called about 5.7624515E-4 times per sample. - do { - y = -ln(generator.nextDouble()) - x = -ln(generator.nextDouble()) * ONE_OVER_R - } while (y + y < x * x) + return when { + iz == 0 -> { + var y: Double - val out = R + x - if (hz > 0) out else -out - } else { - // Wedge of other strips. - // This branch is called about 0.027323 times per sample. - // else - // Try again. - // This branch is called about 0.012362 times per sample. - if (F[iz] + generator.nextDouble() * (F[iz - 1] - F[iz]) < gauss( - x - ) - ) x else sampleOne(generator) + do { + y = -ln(generator.nextDouble()) + x = -ln(generator.nextDouble()) * ONE_OVER_R + } while (y + y < x * x) + + val out = R + x + if (hz > 0) out else -out + } + + F[iz] + generator.nextDouble() * (F[iz - 1] - F[iz]) < gauss(x) -> x + else -> sampleOne(generator) } } diff --git a/kmath-prob/src/jvmMain/kotlin/kscience/kmath/prob/RandomSourceGenerator.kt b/kmath-prob/src/jvmMain/kotlin/kscience/kmath/prob/RandomSourceGenerator.kt index 9cdde6b4b..e6a7ac1f1 100644 --- a/kmath-prob/src/jvmMain/kotlin/kscience/kmath/prob/RandomSourceGenerator.kt +++ b/kmath-prob/src/jvmMain/kotlin/kscience/kmath/prob/RandomSourceGenerator.kt @@ -3,10 +3,14 @@ package kscience.kmath.prob import org.apache.commons.rng.UniformRandomProvider import org.apache.commons.rng.simple.RandomSource -public class RandomSourceGenerator(public val source: RandomSource, seed: Long?) : RandomGenerator { - internal val random: UniformRandomProvider = seed?.let { - RandomSource.create(source, seed) - } ?: RandomSource.create(source) +/** + * Implements [RandomGenerator] by delegating all operations to [RandomSource]. + * + * @property source the underlying [RandomSource] object. + */ +public class RandomSourceGenerator internal constructor(public val source: RandomSource, seed: Long?) : RandomGenerator { + internal val random: UniformRandomProvider = seed?.let { RandomSource.create(source, seed) } + ?: RandomSource.create(source) public override fun nextBoolean(): Boolean = random.nextBoolean() public override fun nextDouble(): Double = random.nextDouble() @@ -23,19 +27,84 @@ public class RandomSourceGenerator(public val source: RandomSource, seed: Long?) public override fun fork(): RandomGenerator = RandomSourceGenerator(source, nextLong()) } +/** + * Implements [UniformRandomProvider] by delegating all operations to [RandomGenerator]. + * + * @property generator the underlying [RandomGenerator] object. + */ public inline class RandomGeneratorProvider(public val generator: RandomGenerator) : UniformRandomProvider { + /** + * Generates a [Boolean] value. + * + * @return the next random value. + */ public override fun nextBoolean(): Boolean = generator.nextBoolean() + + /** + * Generates a [Float] value between 0 and 1. + * + * @return the next random value between 0 and 1. + */ public override fun nextFloat(): Float = generator.nextDouble().toFloat() + + /** + * Generates [Byte] values and places them into a user-supplied array. + * + * The number of random bytes produced is equal to the length of the the byte array. + * + * @param bytes byte array in which to put the random bytes. + */ public override fun nextBytes(bytes: ByteArray): Unit = generator.fillBytes(bytes) + /** + * Generates [Byte] values and places them into a user-supplied array. + * + * The array is filled with bytes extracted from random integers. This implies that the number of random bytes + * generated may be larger than the length of the byte array. + * + * @param bytes the array in which to put the generated bytes. + * @param start the index at which to start inserting the generated bytes. + * @param len the number of bytes to insert. + */ public override fun nextBytes(bytes: ByteArray, start: Int, len: Int) { generator.fillBytes(bytes, start, start + len) } + /** + * Generates an [Int] value. + * + * @return the next random value. + */ public override fun nextInt(): Int = generator.nextInt() + + /** + * Generates an [Int] value between 0 (inclusive) and the specified value (exclusive). + * + * @param n the bound on the random number to be returned. Must be positive. + * @return a random integer between 0 (inclusive) and [n] (exclusive). + */ public override fun nextInt(n: Int): Int = generator.nextInt(n) + + /** + * Generates a [Double] value between 0 and 1. + * + * @return the next random value between 0 and 1. + */ public override fun nextDouble(): Double = generator.nextDouble() + + /** + * Generates a [Long] value. + * + * @return the next random value. + */ public override fun nextLong(): Long = generator.nextLong() + + /** + * Generates a [Long] value between 0 (inclusive) and the specified value (exclusive). + * + * @param n Bound on the random number to be returned. Must be positive. + * @return a random long value between 0 (inclusive) and [n] (exclusive). + */ public override fun nextLong(n: Long): Long = generator.nextLong(n) } @@ -48,8 +117,14 @@ public fun RandomGenerator.asUniformRandomProvider(): UniformRandomProvider = if else RandomGeneratorProvider(this) +/** + * Returns [RandomSourceGenerator] with given [RandomSource] and [seed]. + */ public fun RandomGenerator.Companion.fromSource(source: RandomSource, seed: Long? = null): RandomSourceGenerator = RandomSourceGenerator(source, seed) +/** + * Returns [RandomSourceGenerator] with [RandomSource.MT] algorithm and given [seed]. + */ public fun RandomGenerator.Companion.mersenneTwister(seed: Long? = null): RandomSourceGenerator = fromSource(RandomSource.MT, seed) diff --git a/kmath-prob/src/jvmTest/kotlin/kscience/kmath/prob/SamplerTest.kt b/kmath-prob/src/jvmTest/kotlin/kscience/kmath/prob/SamplerTest.kt index 75db5c402..0ca5a8aeb 100644 --- a/kmath-prob/src/jvmTest/kotlin/kscience/kmath/prob/SamplerTest.kt +++ b/kmath-prob/src/jvmTest/kotlin/kscience/kmath/prob/SamplerTest.kt @@ -7,11 +7,8 @@ class SamplerTest { @Test fun bufferSamplerTest() { - val sampler: Sampler = - BasicSampler { it.chain { nextDouble() } } + val sampler = Sampler { it.chain { nextDouble() } } val data = sampler.sampleBuffer(RandomGenerator.default, 100) - runBlocking { - println(data.next()) - } + runBlocking { println(data.next()) } } } \ No newline at end of file