From 55909aee0d1c929c239a4f837409e2c722c1790f Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Wed, 28 Oct 2020 18:36:00 +0700 Subject: [PATCH] Add additional constructor --- .../kotlin/kscience/kmath/structures/ViktorBenchmark.kt | 2 +- .../kmath/prob/distributions/NormalDistribution.kt | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/src/benchmarks/kotlin/kscience/kmath/structures/ViktorBenchmark.kt b/examples/src/benchmarks/kotlin/kscience/kmath/structures/ViktorBenchmark.kt index 464925ca0..df35b0b78 100644 --- a/examples/src/benchmarks/kotlin/kscience/kmath/structures/ViktorBenchmark.kt +++ b/examples/src/benchmarks/kotlin/kscience/kmath/structures/ViktorBenchmark.kt @@ -36,7 +36,7 @@ internal class ViktorBenchmark { @Benchmark fun rawViktor() { - val one = F64Array.full(init = 1.0, shape = *intArrayOf(dim, dim)) + val one = F64Array.full(init = 1.0, shape = intArrayOf(dim, dim)) var res = one repeat(n) { res = res + one } } diff --git a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/distributions/NormalDistribution.kt b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/distributions/NormalDistribution.kt index 095ac5ea9..57c39018d 100644 --- a/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/distributions/NormalDistribution.kt +++ b/kmath-prob/src/commonMain/kotlin/kscience/kmath/prob/distributions/NormalDistribution.kt @@ -5,9 +5,17 @@ import kscience.kmath.prob.RandomGenerator import kscience.kmath.prob.UnivariateDistribution import kscience.kmath.prob.internal.InternalErf import kscience.kmath.prob.samplers.GaussianSampler +import kscience.kmath.prob.samplers.NormalizedGaussianSampler +import kscience.kmath.prob.samplers.ZigguratNormalizedGaussianSampler import kotlin.math.* public inline class NormalDistribution(public val sampler: GaussianSampler) : UnivariateDistribution { + public constructor( + mean: Double, + standardDeviation: Double, + normalized: NormalizedGaussianSampler = ZigguratNormalizedGaussianSampler.of(), + ) : this(GaussianSampler.of(mean, standardDeviation, normalized)) + public override fun probability(arg: Double): Double { val x1 = (arg - sampler.mean) / sampler.standardDeviation return exp(-0.5 * x1 * x1 - (ln(sampler.standardDeviation) + 0.5 * ln(2 * PI)))