From bc59f8b2875e5d3fd0e93b77d45f923a79db844f Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Mon, 8 Jun 2020 15:13:54 +0700 Subject: [PATCH] Merger kmath-prob and kmath-commons-rng-part --- kmath-commons-rng-part/build.gradle.kts | 2 - .../rng/sampling/SharedStateSampler.kt | 7 --- .../distribution/ContinuousSampler.kt | 5 -- .../sampling/distribution/DiscreteSampler.kt | 5 -- .../distribution/NormalizedGaussianSampler.kt | 3 -- .../SharedStateContinuousSampler.kt | 7 --- kmath-prob/build.gradle.kts | 15 ++---- .../commons/rng/UniformRandomProvider.kt | 2 +- .../rng/sampling/SharedStateSampler.kt | 7 +++ .../AhrensDieterExponentialSampler.kt | 14 +++-- .../BoxMullerNormalizedGaussianSampler.kt | 4 +- .../distribution/ContinuousSampler.kt | 5 ++ .../sampling/distribution/DiscreteSampler.kt | 5 ++ .../sampling/distribution/GaussianSampler.kt | 20 +++++-- .../sampling/distribution/InternalGamma.kt | 2 +- .../sampling/distribution/InternalUtils.kt | 33 ++++++++---- .../KempSmallMeanPoissonSampler.kt | 10 ++-- .../distribution/LargenMeanPoissonSampler.kt | 38 +++++++++---- .../MarsagliaNormalizedGaussianSampler.kt | 4 +- .../distribution/NormalizedGaussianSampler.kt | 4 ++ .../sampling/distribution/PoissonSampler.kt | 18 +++++-- .../rng/sampling/distribution/SamplerBase.kt | 4 +- .../SharedStateContinuousSampler.kt | 7 +++ .../SharedStateDiscreteSampler.kt | 4 +- .../distribution/SmallMeanPoissonSampler.kt | 10 ++-- .../ZigguratNormalizedGaussianSampler.kt | 22 +++++--- .../scientifik/kmath/prob/Distributions.kt | 53 ------------------- .../kmath/prob/RandomSourceGenerator.kt | 5 +- .../{Distributions.kt => DistributionsJVM.kt} | 14 +++-- .../kmath/prob/RandomSourceGenerator.kt | 2 +- settings.gradle.kts | 2 +- 31 files changed, 175 insertions(+), 158 deletions(-) delete mode 100644 kmath-commons-rng-part/build.gradle.kts delete mode 100644 kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/SharedStateSampler.kt delete mode 100644 kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/ContinuousSampler.kt delete mode 100644 kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/DiscreteSampler.kt delete mode 100644 kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/NormalizedGaussianSampler.kt delete mode 100644 kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/SharedStateContinuousSampler.kt rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/UniformRandomProvider.kt (90%) create mode 100644 kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/SharedStateSampler.kt rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.kt (88%) rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/sampling/distribution/BoxMullerNormalizedGaussianSampler.kt (92%) create mode 100644 kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/ContinuousSampler.kt create mode 100644 kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/DiscreteSampler.kt rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/sampling/distribution/GaussianSampler.kt (69%) rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/sampling/distribution/InternalGamma.kt (95%) rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/sampling/distribution/InternalUtils.kt (72%) rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/sampling/distribution/KempSmallMeanPoissonSampler.kt (88%) rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/sampling/distribution/LargenMeanPoissonSampler.kt (88%) rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.kt (94%) create mode 100644 kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/NormalizedGaussianSampler.kt rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/sampling/distribution/PoissonSampler.kt (64%) rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/sampling/distribution/SamplerBase.kt (78%) create mode 100644 kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/SharedStateContinuousSampler.kt rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/sampling/distribution/SharedStateDiscreteSampler.kt (52%) rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/sampling/distribution/SmallMeanPoissonSampler.kt (83%) rename {kmath-commons-rng-part/src/commonMain/kotlin/scientifik => kmath-prob/src/commonMain/kotlin/scientifik/kmath}/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.kt (86%) delete mode 100644 kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/Distributions.kt rename kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/{Distributions.kt => DistributionsJVM.kt} (82%) diff --git a/kmath-commons-rng-part/build.gradle.kts b/kmath-commons-rng-part/build.gradle.kts deleted file mode 100644 index 9d3cd0e7d..000000000 --- a/kmath-commons-rng-part/build.gradle.kts +++ /dev/null @@ -1,2 +0,0 @@ -plugins { id("scientifik.mpp") } -kotlin.sourceSets { commonMain.get().dependencies { api(project(":kmath-coroutines")) } } diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/SharedStateSampler.kt b/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/SharedStateSampler.kt deleted file mode 100644 index d32a646c2..000000000 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/SharedStateSampler.kt +++ /dev/null @@ -1,7 +0,0 @@ -package scientifik.commons.rng.sampling - -import scientifik.commons.rng.UniformRandomProvider - -interface SharedStateSampler { - fun withUniformRandomProvider(rng: UniformRandomProvider): R -} diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/ContinuousSampler.kt b/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/ContinuousSampler.kt deleted file mode 100644 index 4841672a2..000000000 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/ContinuousSampler.kt +++ /dev/null @@ -1,5 +0,0 @@ -package scientifik.commons.rng.sampling.distribution - -interface ContinuousSampler { - fun sample(): Double -} diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/DiscreteSampler.kt b/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/DiscreteSampler.kt deleted file mode 100644 index 8e8bccd18..000000000 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/DiscreteSampler.kt +++ /dev/null @@ -1,5 +0,0 @@ -package scientifik.commons.rng.sampling.distribution - -interface DiscreteSampler { - fun sample(): Int -} diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/NormalizedGaussianSampler.kt b/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/NormalizedGaussianSampler.kt deleted file mode 100644 index feff4d954..000000000 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/NormalizedGaussianSampler.kt +++ /dev/null @@ -1,3 +0,0 @@ -package scientifik.commons.rng.sampling.distribution - -interface NormalizedGaussianSampler : ContinuousSampler diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/SharedStateContinuousSampler.kt b/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/SharedStateContinuousSampler.kt deleted file mode 100644 index ddac4b7a7..000000000 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/SharedStateContinuousSampler.kt +++ /dev/null @@ -1,7 +0,0 @@ -package scientifik.commons.rng.sampling.distribution - -import scientifik.commons.rng.sampling.SharedStateSampler - -interface SharedStateContinuousSampler : ContinuousSampler, - SharedStateSampler { -} diff --git a/kmath-prob/build.gradle.kts b/kmath-prob/build.gradle.kts index 4ec254e82..4c1d7f949 100644 --- a/kmath-prob/build.gradle.kts +++ b/kmath-prob/build.gradle.kts @@ -3,17 +3,10 @@ plugins { } kotlin.sourceSets { - commonMain { - dependencies { - api(project(":kmath-coroutines")) - api(project(":kmath-commons-rng-part")) - } - } + commonMain.get().dependencies { api(project(":kmath-coroutines")) } - jvmMain { - dependencies { - api("org.apache.commons:commons-rng-sampling:1.3") - api("org.apache.commons:commons-rng-simple:1.3") - } + jvmMain.get().dependencies { + api("org.apache.commons:commons-rng-sampling:1.3") + api("org.apache.commons:commons-rng-simple:1.3") } } \ No newline at end of file diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/UniformRandomProvider.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/UniformRandomProvider.kt similarity index 90% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/UniformRandomProvider.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/UniformRandomProvider.kt index 2fbf7a0a2..bd6d30124 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/UniformRandomProvider.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/UniformRandomProvider.kt @@ -1,4 +1,4 @@ -package scientifik.commons.rng +package scientifik.kmath.commons.rng interface UniformRandomProvider { fun nextBytes(bytes: ByteArray) diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/SharedStateSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/SharedStateSampler.kt new file mode 100644 index 000000000..30ccefb54 --- /dev/null +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/SharedStateSampler.kt @@ -0,0 +1,7 @@ +package scientifik.kmath.commons.rng.sampling + +import scientifik.kmath.commons.rng.UniformRandomProvider + +interface SharedStateSampler { + fun withUniformRandomProvider(rng: UniformRandomProvider): R +} diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.kt similarity index 88% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.kt index 7aa061951..714cf2164 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.kt @@ -1,6 +1,6 @@ -package scientifik.commons.rng.sampling.distribution +package scientifik.kmath.commons.rng.sampling.distribution -import scientifik.commons.rng.UniformRandomProvider +import scientifik.kmath.commons.rng.UniformRandomProvider import kotlin.math.ln import kotlin.math.pow @@ -76,7 +76,11 @@ class AhrensDieterExponentialSampler : SamplerBase, fun of( rng: UniformRandomProvider, mean: Double - ): SharedStateContinuousSampler = AhrensDieterExponentialSampler(rng, mean) + ): SharedStateContinuousSampler = + AhrensDieterExponentialSampler( + rng, + mean + ) init { /** @@ -87,7 +91,9 @@ class AhrensDieterExponentialSampler : SamplerBase, var qi = 0.0 EXPONENTIAL_SA_QI.indices.forEach { i -> - qi += ln2.pow(i + 1.0) / InternalUtils.factorial(i + 1) + qi += ln2.pow(i + 1.0) / InternalUtils.factorial( + i + 1 + ) EXPONENTIAL_SA_QI[i] = qi } } diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/BoxMullerNormalizedGaussianSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/BoxMullerNormalizedGaussianSampler.kt similarity index 92% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/BoxMullerNormalizedGaussianSampler.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/BoxMullerNormalizedGaussianSampler.kt index 91b3314b5..0e927bb32 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/BoxMullerNormalizedGaussianSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/BoxMullerNormalizedGaussianSampler.kt @@ -1,6 +1,6 @@ -package scientifik.commons.rng.sampling.distribution +package scientifik.kmath.commons.rng.sampling.distribution -import scientifik.commons.rng.UniformRandomProvider +import scientifik.kmath.commons.rng.UniformRandomProvider import kotlin.math.* class BoxMullerNormalizedGaussianSampler( diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/ContinuousSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/ContinuousSampler.kt new file mode 100644 index 000000000..aea81cf92 --- /dev/null +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/ContinuousSampler.kt @@ -0,0 +1,5 @@ +package scientifik.kmath.commons.rng.sampling.distribution + +interface ContinuousSampler { + fun sample(): Double +} diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/DiscreteSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/DiscreteSampler.kt new file mode 100644 index 000000000..4de780424 --- /dev/null +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/DiscreteSampler.kt @@ -0,0 +1,5 @@ +package scientifik.kmath.commons.rng.sampling.distribution + +interface DiscreteSampler { + fun sample(): Int +} diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/GaussianSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/GaussianSampler.kt similarity index 69% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/GaussianSampler.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/GaussianSampler.kt index 424ea90fe..0b54a0ad1 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/GaussianSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/GaussianSampler.kt @@ -1,8 +1,9 @@ -package scientifik.commons.rng.sampling.distribution +package scientifik.kmath.commons.rng.sampling.distribution -import scientifik.commons.rng.UniformRandomProvider +import scientifik.kmath.commons.rng.UniformRandomProvider -class GaussianSampler : SharedStateContinuousSampler { +class GaussianSampler : + SharedStateContinuousSampler { private val mean: Double private val standardDeviation: Double private val normalized: NormalizedGaussianSampler @@ -24,7 +25,11 @@ class GaussianSampler : SharedStateContinuousSampler { ) { mean = source.mean standardDeviation = source.standardDeviation - normalized = InternalUtils.newNormalizedGaussianSampler(source.normalized, rng) + normalized = + InternalUtils.newNormalizedGaussianSampler( + source.normalized, + rng + ) } override fun sample(): Double = standardDeviation * normalized.sample() + mean @@ -40,6 +45,11 @@ class GaussianSampler : SharedStateContinuousSampler { normalized: NormalizedGaussianSampler, mean: Double, standardDeviation: Double - ): SharedStateContinuousSampler = GaussianSampler(normalized, mean, standardDeviation) + ): SharedStateContinuousSampler = + GaussianSampler( + normalized, + mean, + standardDeviation + ) } } diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/InternalGamma.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/InternalGamma.kt similarity index 95% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/InternalGamma.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/InternalGamma.kt index a75ba1608..79426d7ae 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/InternalGamma.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/InternalGamma.kt @@ -1,4 +1,4 @@ -package scientifik.commons.rng.sampling.distribution +package scientifik.kmath.commons.rng.sampling.distribution import kotlin.math.PI import kotlin.math.ln diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/InternalUtils.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/InternalUtils.kt similarity index 72% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/InternalUtils.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/InternalUtils.kt index 8134252aa..0f563b956 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/InternalUtils.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/InternalUtils.kt @@ -1,7 +1,7 @@ -package scientifik.commons.rng.sampling.distribution +package scientifik.kmath.commons.rng.sampling.distribution -import scientifik.commons.rng.UniformRandomProvider -import scientifik.commons.rng.sampling.SharedStateSampler +import scientifik.kmath.commons.rng.UniformRandomProvider +import scientifik.kmath.commons.rng.sampling.SharedStateSampler import kotlin.math.ln import kotlin.math.min @@ -63,30 +63,45 @@ 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) + cache.copyInto(logFactorials, + BEGIN_LOG_FACTORIALS, + BEGIN_LOG_FACTORIALS, endCopy) } // All values to be computed else - endCopy = BEGIN_LOG_FACTORIALS + endCopy = + BEGIN_LOG_FACTORIALS // Compute remaining values. (endCopy until numValues).forEach { i -> - if (i < FACTORIALS.size) logFactorials[i] = ln(FACTORIALS[i].toDouble()) else logFactorials[i] = + if (i < FACTORIALS.size) logFactorials[i] = ln( + FACTORIALS[i].toDouble()) else logFactorials[i] = logFactorials[i - 1] + ln(i.toDouble()) } } - fun withCache(cacheSize: Int): FactorialLog = FactorialLog(cacheSize, logFactorials) + fun withCache(cacheSize: Int): FactorialLog = + FactorialLog( + cacheSize, + logFactorials + ) fun value(n: Int): Double { if (n < logFactorials.size) return logFactorials[n] - return if (n < FACTORIALS.size) ln(FACTORIALS[n].toDouble()) else InternalGamma.logGamma(n + 1.0) + return if (n < FACTORIALS.size) ln( + FACTORIALS[n].toDouble()) else InternalGamma.logGamma( + n + 1.0 + ) } companion object { - fun create(): FactorialLog = FactorialLog(0, null) + fun create(): FactorialLog = + FactorialLog( + 0, + null + ) } } } diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/KempSmallMeanPoissonSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/KempSmallMeanPoissonSampler.kt similarity index 88% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/KempSmallMeanPoissonSampler.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/KempSmallMeanPoissonSampler.kt index 6501089b2..3072cf020 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/KempSmallMeanPoissonSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/KempSmallMeanPoissonSampler.kt @@ -1,6 +1,6 @@ -package scientifik.commons.rng.sampling.distribution +package scientifik.kmath.commons.rng.sampling.distribution -import scientifik.commons.rng.UniformRandomProvider +import scientifik.kmath.commons.rng.UniformRandomProvider import kotlin.math.exp class KempSmallMeanPoissonSampler private constructor( @@ -48,7 +48,11 @@ class KempSmallMeanPoissonSampler private constructor( val p0: Double = exp(-mean) // Probability must be positive. As mean increases then p(0) decreases. - if (p0 > 0) return KempSmallMeanPoissonSampler(rng, p0, mean) + if (p0 > 0) return KempSmallMeanPoissonSampler( + rng, + p0, + mean + ) throw IllegalArgumentException("No probability for mean: $mean") } } diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/LargenMeanPoissonSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/LargenMeanPoissonSampler.kt similarity index 88% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/LargenMeanPoissonSampler.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/LargenMeanPoissonSampler.kt index ce1e1e3b0..ae7d3f079 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/LargenMeanPoissonSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/LargenMeanPoissonSampler.kt @@ -1,9 +1,10 @@ -package scientifik.commons.rng.sampling.distribution +package scientifik.kmath.commons.rng.sampling.distribution -import scientifik.commons.rng.UniformRandomProvider +import scientifik.kmath.commons.rng.UniformRandomProvider import kotlin.math.* -class LargeMeanPoissonSampler : SharedStateDiscreteSampler { +class LargeMeanPoissonSampler : + SharedStateDiscreteSampler { private val rng: UniformRandomProvider private val exponential: SharedStateContinuousSampler private val gaussian: SharedStateContinuousSampler @@ -27,8 +28,13 @@ class LargeMeanPoissonSampler : SharedStateDiscreteSampler { // The algorithm is not valid if Math.floor(mean) is not an integer. require(mean <= MAX_MEAN) { "mean $mean > $MAX_MEAN" } this.rng = rng - gaussian = ZigguratNormalizedGaussianSampler(rng) - exponential = AhrensDieterExponentialSampler.of(rng, 1.0) + gaussian = + ZigguratNormalizedGaussianSampler(rng) + exponential = + AhrensDieterExponentialSampler.of( + rng, + 1.0 + ) // Plain constructor uses the uncached function. factorialLog = NO_CACHE_FACTORIAL_LOG!! // Cache values used in the algorithm @@ -49,7 +55,10 @@ class LargeMeanPoissonSampler : SharedStateDiscreteSampler { val lambdaFractional = mean - lambda smallMeanPoissonSampler = if (lambdaFractional < Double.MIN_VALUE) NO_SMALL_MEAN_POISSON_SAMPLER else // Not used. - KempSmallMeanPoissonSampler.of(rng, lambdaFractional) + KempSmallMeanPoissonSampler.of( + rng, + lambdaFractional + ) } internal constructor( @@ -59,8 +68,13 @@ class LargeMeanPoissonSampler : SharedStateDiscreteSampler { ) { require(!(lambdaFractional < 0 || lambdaFractional >= 1)) { "lambdaFractional must be in the range 0 (inclusive) to 1 (exclusive): $lambdaFractional" } this.rng = rng - gaussian = ZigguratNormalizedGaussianSampler(rng) - exponential = AhrensDieterExponentialSampler.of(rng, 1.0) + gaussian = + ZigguratNormalizedGaussianSampler(rng) + exponential = + AhrensDieterExponentialSampler.of( + rng, + 1.0 + ) // Plain constructor uses the uncached function. factorialLog = NO_CACHE_FACTORIAL_LOG!! // Use the state to initialise the algorithm @@ -79,7 +93,10 @@ class LargeMeanPoissonSampler : SharedStateDiscreteSampler { if (lambdaFractional < Double.MIN_VALUE) NO_SMALL_MEAN_POISSON_SAMPLER else // Not used. - KempSmallMeanPoissonSampler.of(rng, lambdaFractional) + KempSmallMeanPoissonSampler.of( + rng, + lambdaFractional + ) } /** @@ -222,7 +239,8 @@ class LargeMeanPoissonSampler : SharedStateDiscreteSampler { fun of( rng: UniformRandomProvider, mean: Double - ): SharedStateDiscreteSampler = LargeMeanPoissonSampler(rng, mean) + ): SharedStateDiscreteSampler = + LargeMeanPoissonSampler(rng, mean) init { // Create without a cache. diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.kt similarity index 94% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.kt index 26ee9ece9..ee346718e 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.kt @@ -1,6 +1,6 @@ -package scientifik.commons.rng.sampling.distribution +package scientifik.kmath.commons.rng.sampling.distribution -import scientifik.commons.rng.UniformRandomProvider +import scientifik.kmath.commons.rng.UniformRandomProvider import kotlin.math.ln import kotlin.math.sqrt diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/NormalizedGaussianSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/NormalizedGaussianSampler.kt new file mode 100644 index 000000000..c47b9b4e9 --- /dev/null +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/NormalizedGaussianSampler.kt @@ -0,0 +1,4 @@ +package scientifik.kmath.commons.rng.sampling.distribution + +interface NormalizedGaussianSampler : + ContinuousSampler diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/PoissonSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/PoissonSampler.kt similarity index 64% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/PoissonSampler.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/PoissonSampler.kt index 4a7f56f60..a440c3a7d 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/PoissonSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/PoissonSampler.kt @@ -1,11 +1,12 @@ -package scientifik.commons.rng.sampling.distribution +package scientifik.kmath.commons.rng.sampling.distribution -import scientifik.commons.rng.UniformRandomProvider +import scientifik.kmath.commons.rng.UniformRandomProvider class PoissonSampler( rng: UniformRandomProvider, mean: Double -) : SamplerBase(null), SharedStateDiscreteSampler { +) : SamplerBase(null), + SharedStateDiscreteSampler { private val poissonSamplerDelegate: SharedStateDiscreteSampler override fun sample(): Int = poissonSamplerDelegate.sample() @@ -22,11 +23,18 @@ class PoissonSampler( rng: UniformRandomProvider, mean: Double ): SharedStateDiscreteSampler =// Each sampler should check the input arguments. - if (mean < PIVOT) SmallMeanPoissonSampler.of(rng, mean) else LargeMeanPoissonSampler.of(rng, mean) + if (mean < PIVOT) SmallMeanPoissonSampler.of( + rng, + mean + ) else LargeMeanPoissonSampler.of( + rng, + mean + ) } init { // Delegate all work to specialised samplers. - poissonSamplerDelegate = of(rng, mean) + poissonSamplerDelegate = + of(rng, mean) } } \ No newline at end of file diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/SamplerBase.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/SamplerBase.kt similarity index 78% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/SamplerBase.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/SamplerBase.kt index 38df9eb4e..4170e9db3 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/SamplerBase.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/SamplerBase.kt @@ -1,6 +1,6 @@ -package scientifik.commons.rng.sampling.distribution +package scientifik.kmath.commons.rng.sampling.distribution -import scientifik.commons.rng.UniformRandomProvider +import scientifik.kmath.commons.rng.UniformRandomProvider @Deprecated("Since version 1.1. Class intended for internal use only.") open class SamplerBase protected constructor(private val rng: UniformRandomProvider?) { diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/SharedStateContinuousSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/SharedStateContinuousSampler.kt new file mode 100644 index 000000000..c833ce8de --- /dev/null +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/SharedStateContinuousSampler.kt @@ -0,0 +1,7 @@ +package scientifik.kmath.commons.rng.sampling.distribution + +import scientifik.kmath.commons.rng.sampling.SharedStateSampler + +interface SharedStateContinuousSampler : ContinuousSampler, + SharedStateSampler { +} diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/SharedStateDiscreteSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/SharedStateDiscreteSampler.kt similarity index 52% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/SharedStateDiscreteSampler.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/SharedStateDiscreteSampler.kt index 2d8adada6..7b17e4670 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/SharedStateDiscreteSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/SharedStateDiscreteSampler.kt @@ -1,6 +1,6 @@ -package scientifik.commons.rng.sampling.distribution +package scientifik.kmath.commons.rng.sampling.distribution -import scientifik.commons.rng.sampling.SharedStateSampler +import scientifik.kmath.commons.rng.sampling.SharedStateSampler interface SharedStateDiscreteSampler : DiscreteSampler, SharedStateSampler { // Composite interface diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/SmallMeanPoissonSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/SmallMeanPoissonSampler.kt similarity index 83% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/SmallMeanPoissonSampler.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/SmallMeanPoissonSampler.kt index a3188620f..a35dd277c 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/SmallMeanPoissonSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/SmallMeanPoissonSampler.kt @@ -1,10 +1,11 @@ -package scientifik.commons.rng.sampling.distribution +package scientifik.kmath.commons.rng.sampling.distribution -import scientifik.commons.rng.UniformRandomProvider +import scientifik.kmath.commons.rng.UniformRandomProvider import kotlin.math.ceil import kotlin.math.exp -class SmallMeanPoissonSampler : SharedStateDiscreteSampler { +class SmallMeanPoissonSampler : + SharedStateDiscreteSampler { private val p0: Double private val limit: Int private val rng: UniformRandomProvider @@ -53,6 +54,7 @@ class SmallMeanPoissonSampler : SharedStateDiscreteSampler { fun of( rng: UniformRandomProvider, mean: Double - ): SharedStateDiscreteSampler = SmallMeanPoissonSampler(rng, mean) + ): SharedStateDiscreteSampler = + SmallMeanPoissonSampler(rng, mean) } } \ No newline at end of file diff --git a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.kt similarity index 86% rename from kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.kt rename to kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.kt index a916121db..26e089599 100644 --- a/kmath-commons-rng-part/src/commonMain/kotlin/scientifik/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.kt @@ -1,6 +1,6 @@ -package scientifik.commons.rng.sampling.distribution +package scientifik.kmath.commons.rng.sampling.distribution -import scientifik.commons.rng.UniformRandomProvider +import scientifik.kmath.commons.rng.UniformRandomProvider import kotlin.math.* class ZigguratNormalizedGaussianSampler(private val rng: UniformRandomProvider) : @@ -26,9 +26,13 @@ class ZigguratNormalizedGaussianSampler(private val rng: UniformRandomProvider) init { // Filling the tables. - var d = R + var d = + R var t = d - var fd = gauss(d) + var fd = + gauss( + d + ) val q = V / fd K[0] = (d / q * MAX).toLong() K[1] = 0 @@ -39,7 +43,10 @@ class ZigguratNormalizedGaussianSampler(private val rng: UniformRandomProvider) (LAST - 1 downTo 1).forEach { i -> d = sqrt(-2 * ln(V / d + fd)) - fd = gauss(d) + fd = + gauss( + d + ) K[i + 1] = (d / t * MAX).toLong() t = d F[i] = fd @@ -80,7 +87,10 @@ class ZigguratNormalizedGaussianSampler(private val rng: UniformRandomProvider) // else // Try again. // This branch is called about 0.012362 times per sample. - if (F[iz] + rng.nextDouble() * (F[iz - 1] - F[iz]) < gauss(x)) x else sample() + if (F[iz] + rng.nextDouble() * (F[iz - 1] - F[iz]) < gauss( + x + ) + ) x else sample() } } diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/Distributions.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/Distributions.kt deleted file mode 100644 index fd8cc4116..000000000 --- a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/Distributions.kt +++ /dev/null @@ -1,53 +0,0 @@ -package scientifik.kmath.prob - -import scientifik.commons.rng.UniformRandomProvider -import scientifik.commons.rng.sampling.distribution.* -import scientifik.kmath.chains.BlockingIntChain -import scientifik.kmath.chains.BlockingRealChain -import scientifik.kmath.chains.Chain - -abstract class ContinuousSamplerDistribution : Distribution { - - private inner class ContinuousSamplerChain(val generator: RandomGenerator) : BlockingRealChain() { - private val sampler = buildCMSampler(generator) - - override fun nextDouble(): Double = sampler.sample() - - override fun fork(): Chain = ContinuousSamplerChain(generator.fork()) - } - - protected abstract fun buildCMSampler(generator: RandomGenerator): ContinuousSampler - - override fun sample(generator: RandomGenerator): BlockingRealChain = ContinuousSamplerChain(generator) -} - -abstract class DiscreteSamplerDistribution : Distribution { - - private inner class ContinuousSamplerChain(val generator: RandomGenerator) : BlockingIntChain() { - private val sampler = buildSampler(generator) - - override fun nextInt(): Int = sampler.sample() - - override fun fork(): Chain = ContinuousSamplerChain(generator.fork()) - } - - protected abstract fun buildSampler(generator: RandomGenerator): DiscreteSampler - - override fun sample(generator: RandomGenerator): BlockingIntChain = ContinuousSamplerChain(generator) -} - -enum class NormalSamplerMethod { - BoxMuller, - Marsaglia, - Ziggurat -} - -fun normalSampler(method: NormalSamplerMethod, provider: UniformRandomProvider): NormalizedGaussianSampler = - when (method) { - NormalSamplerMethod.BoxMuller -> BoxMullerNormalizedGaussianSampler( - provider - ) - NormalSamplerMethod.Marsaglia -> MarsagliaNormalizedGaussianSampler(provider) - NormalSamplerMethod.Ziggurat -> ZigguratNormalizedGaussianSampler(provider) - } - diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/RandomSourceGenerator.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/RandomSourceGenerator.kt index 7be8276f3..b4056cabc 100644 --- a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/RandomSourceGenerator.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/RandomSourceGenerator.kt @@ -1,9 +1,10 @@ package scientifik.kmath.prob -import scientifik.commons.rng.UniformRandomProvider +import scientifik.kmath.commons.rng.UniformRandomProvider -inline class RandomGeneratorProvider(val generator: RandomGenerator) : UniformRandomProvider { +inline class RandomGeneratorProvider(val generator: RandomGenerator) : + UniformRandomProvider { override fun nextBoolean(): Boolean = generator.nextBoolean() override fun nextFloat(): Float = generator.nextDouble().toFloat() diff --git a/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/Distributions.kt b/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/DistributionsJVM.kt similarity index 82% rename from kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/Distributions.kt rename to kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/DistributionsJVM.kt index aaeeb1b26..649cae961 100644 --- a/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/Distributions.kt +++ b/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/DistributionsJVM.kt @@ -1,9 +1,9 @@ package scientifik.kmath.prob -import scientifik.commons.rng.sampling.distribution.ContinuousSampler -import scientifik.commons.rng.sampling.distribution.DiscreteSampler -import scientifik.commons.rng.sampling.distribution.GaussianSampler -import scientifik.commons.rng.sampling.distribution.PoissonSampler +import scientifik.kmath.commons.rng.sampling.distribution.ContinuousSampler +import scientifik.kmath.commons.rng.sampling.distribution.DiscreteSampler +import scientifik.kmath.commons.rng.sampling.distribution.GaussianSampler +import scientifik.kmath.commons.rng.sampling.distribution.PoissonSampler import kotlin.math.PI import kotlin.math.exp import kotlin.math.pow @@ -33,7 +33,11 @@ fun Distribution.Companion.normal( override fun buildCMSampler(generator: RandomGenerator): ContinuousSampler { val provider = generator.asUniformRandomProvider() val normalizedSampler = normalSampler(method, provider) - return GaussianSampler(normalizedSampler, mean, sigma) + return GaussianSampler( + normalizedSampler, + mean, + sigma + ) } override fun probability(arg: Double): Double { diff --git a/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/RandomSourceGenerator.kt b/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/RandomSourceGenerator.kt index 8c4b59abb..4c8d630ae 100644 --- a/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/RandomSourceGenerator.kt +++ b/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/RandomSourceGenerator.kt @@ -1,7 +1,7 @@ package scientifik.kmath.prob import org.apache.commons.rng.simple.RandomSource -import scientifik.commons.rng.UniformRandomProvider +import scientifik.kmath.commons.rng.UniformRandomProvider class RandomSourceGenerator(val source: RandomSource, seed: Long?) : RandomGenerator { diff --git a/settings.gradle.kts b/settings.gradle.kts index f73a80994..5e584dc58 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -29,13 +29,13 @@ pluginManagement { } rootProject.name = "kmath" + include( ":kmath-memory", ":kmath-core", ":kmath-functions", // ":kmath-io", ":kmath-coroutines", - "kmath-commons-rng-part", ":kmath-histograms", ":kmath-commons", ":kmath-viktor",