From 0aa86df577e19d7caf10fe194d698ae8d89f398b Mon Sep 17 00:00:00 2001 From: apomytkina Date: Sun, 15 May 2022 16:36:01 +0300 Subject: [PATCH] DoubleBuffer generator was moved to DoubleBuffer file and factory functions were added for LongArray --- .../kscience/kmath/structures/DoubleBuffer.kt | 70 +------------------ .../space/kscience/kmath/nd4j/arrays.kt | 10 +++ .../kmath/samplers/BoxMullerSampler.kt | 5 +- .../space/kscience/kmath/stat/RandomChain.kt | 3 +- .../kscience/kmath/stat/RandomGenerator.kt | 6 -- 5 files changed, 16 insertions(+), 78 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt index f4388a477..6573e325f 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt @@ -1,69 +1 @@ -/* - * Copyright 2018-2021 KMath contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package space.kscience.kmath.structures - -import kotlin.jvm.JvmInline - -/** - * Specialized [MutableBuffer] implementation over [DoubleArray]. - * - * @property array the underlying array. - */ -@JvmInline -public value class DoubleBuffer(public val array: DoubleArray) : MutableBuffer { - override val size: Int get() = array.size - - override operator fun get(index: Int): Double = array[index] - - override operator fun set(index: Int, value: Double) { - array[index] = value - } - - override operator fun iterator(): DoubleIterator = array.iterator() - - override fun copy(): DoubleBuffer = DoubleBuffer(array.copyOf()) - - override fun toString(): String = Buffer.toString(this) - - public companion object{ - public fun zero(size: Int): DoubleBuffer = DoubleArray(size).asBuffer() - } -} - -/** - * Creates a new [DoubleBuffer] with the specified [size], where each element is calculated by calling the specified - * [init] function. - * - * The function [init] is called for each array element sequentially starting from the first one. - * It should return the value for a buffer element given its index. - */ -public inline fun DoubleBuffer(size: Int, init: (Int) -> Double): DoubleBuffer = DoubleBuffer(DoubleArray(size) { init(it) }) - -/** - * Returns a new [DoubleBuffer] of given elements. - */ -public fun DoubleBuffer(vararg doubles: Double): DoubleBuffer = DoubleBuffer(doubles) - -/** - * Simplified [DoubleBuffer] to array comparison - */ -public fun DoubleBuffer.contentEquals(vararg doubles: Double): Boolean = array.contentEquals(doubles) - -/** - * Returns a new [DoubleArray] containing all the elements of this [Buffer]. - */ -public fun Buffer.toDoubleArray(): DoubleArray = when (this) { - is DoubleBuffer -> array.copyOf() - else -> DoubleArray(size, ::get) -} - -/** - * Returns [DoubleBuffer] over this array. - * - * @receiver the array. - * @return the new buffer. - */ -public fun DoubleArray.asBuffer(): DoubleBuffer = DoubleBuffer(this) +/* * Copyright 2018-2021 KMath contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package space.kscience.kmath.structures import kotlin.jvm.JvmInline import kotlin.random.Random.Default.nextDouble /** * Specialized [MutableBuffer] implementation over [DoubleArray]. * * @property array the underlying array. */ @JvmInline public value class DoubleBuffer(public val array: DoubleArray) : MutableBuffer { override val size: Int get() = array.size override operator fun get(index: Int): Double = array[index] override operator fun set(index: Int, value: Double) { array[index] = value } override operator fun iterator(): DoubleIterator = array.iterator() override fun copy(): DoubleBuffer = DoubleBuffer(array.copyOf()) override fun toString(): String = Buffer.toString(this) public companion object{ public fun zero(size: Int): DoubleBuffer = DoubleArray(size).asBuffer() } } /** * Creates a new [DoubleBuffer] with the specified [size], where each element is calculated by calling the specified * [init] function. * * The function [init] is called for each array element sequentially starting from the first one. * It should return the value for a buffer element given its index. */ public inline fun DoubleBuffer(size: Int, init: (Int) -> Double): DoubleBuffer = DoubleBuffer(DoubleArray(size) { init(it) }) /** * Returns a new [DoubleBuffer] of given elements. */ public fun DoubleBuffer(vararg doubles: Double): DoubleBuffer = DoubleBuffer(doubles) /** * Simplified [DoubleBuffer] to array comparison */ public fun DoubleBuffer.contentEquals(vararg doubles: Double): Boolean = array.contentEquals(doubles) /** * Returns a new [DoubleArray] containing all the elements of this [Buffer]. */ public fun Buffer.toDoubleArray(): DoubleArray = when (this) { is DoubleBuffer -> array.copyOf() else -> DoubleArray(size, ::get) } /** * A chunk of doubles of given [size]. */ public fun nextDoubleBuffer(size: Int): DoubleBuffer = DoubleBuffer(size) { nextDouble() } /** * Returns [DoubleBuffer] over this array. * * @receiver the array. * @return the new buffer. */ public fun DoubleArray.asBuffer(): DoubleBuffer = DoubleBuffer(this) \ No newline at end of file diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/arrays.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/arrays.kt index 3ca756600..db910f158 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/arrays.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/arrays.kt @@ -8,3 +8,13 @@ package space.kscience.kmath.nd4j import space.kscience.kmath.misc.toIntExact internal fun LongArray.toIntArray(): IntArray = IntArray(size) { this[it].toIntExact() } + +internal fun LongArray.linspace(start: Long, stop: Long) = Array(this.size) { + start + it * ((stop - start) / (this.size - 1)) +} + +internal fun LongArray.zeros() = Array(this.size) { 0 } + +internal fun LongArray.ones() = Array(this.size) { 1 } + +internal fun repeat(number: Long, size: Int) = LongArray(size) { number } \ No newline at end of file diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/BoxMullerSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/BoxMullerSampler.kt index b3c014553..9719cef0a 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/BoxMullerSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/BoxMullerSampler.kt @@ -8,6 +8,7 @@ package space.kscience.kmath.samplers import space.kscience.kmath.chains.BlockingDoubleChain import space.kscience.kmath.stat.RandomGenerator import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.nextDoubleBuffer import kotlin.math.* /** @@ -23,8 +24,8 @@ public object BoxMullerSampler : NormalizedGaussianSampler { var state = Double.NaN override fun nextBufferBlocking(size: Int): DoubleBuffer { - val xs = generator.nextDoubleBuffer(size) - val ys = generator.nextDoubleBuffer(size) + val xs = nextDoubleBuffer(size) + val ys = nextDoubleBuffer(size) return DoubleBuffer(size) { index -> if (state.isNaN()) { diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomChain.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomChain.kt index d4bc36b5b..ab05b3aa1 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomChain.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomChain.kt @@ -8,6 +8,7 @@ package space.kscience.kmath.stat import space.kscience.kmath.chains.BlockingDoubleChain import space.kscience.kmath.chains.Chain import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.nextDoubleBuffer /** * A possibly stateful chain producing random values. @@ -31,7 +32,7 @@ public fun RandomGenerator.chain(generator: suspend RandomGenerator.() -> R) * A type-specific double chunk random chain */ public class UniformDoubleChain(public val generator: RandomGenerator) : BlockingDoubleChain { - override fun nextBufferBlocking(size: Int): DoubleBuffer = generator.nextDoubleBuffer(size) + override fun nextBufferBlocking(size: Int): DoubleBuffer = nextDoubleBuffer(size) override suspend fun nextBuffer(size: Int): DoubleBuffer = nextBufferBlocking(size) override suspend fun fork(): UniformDoubleChain = UniformDoubleChain(generator.fork()) diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt index f280a78aa..ec16ab259 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt @@ -5,7 +5,6 @@ package space.kscience.kmath.stat -import space.kscience.kmath.structures.DoubleBuffer import kotlin.random.Random /** @@ -22,11 +21,6 @@ public interface RandomGenerator { */ public fun nextDouble(): Double - /** - * A chunk of doubles of given [size]. - */ - public fun nextDoubleBuffer(size: Int): DoubleBuffer = DoubleBuffer(size) { nextDouble() } - /** * Gets the next random `Int` from the random number generator. *