DoubleBuffer generator was moved to DoubleBuffer file and factory functions were added for LongArray

This commit is contained in:
apomytkina 2022-05-15 16:36:01 +03:00
parent f273ca6684
commit 0aa86df577
5 changed files with 16 additions and 78 deletions

View File

@ -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 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<Double> { 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<Double>.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)
* 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<Double> {
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<Double>.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)

View File

@ -8,3 +8,13 @@ package space.kscience.kmath.nd4j
import space.kscience.kmath.misc.toIntExact import space.kscience.kmath.misc.toIntExact
internal fun LongArray.toIntArray(): IntArray = IntArray(size) { this[it].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 }

View File

@ -8,6 +8,7 @@ package space.kscience.kmath.samplers
import space.kscience.kmath.chains.BlockingDoubleChain import space.kscience.kmath.chains.BlockingDoubleChain
import space.kscience.kmath.stat.RandomGenerator import space.kscience.kmath.stat.RandomGenerator
import space.kscience.kmath.structures.DoubleBuffer import space.kscience.kmath.structures.DoubleBuffer
import space.kscience.kmath.structures.nextDoubleBuffer
import kotlin.math.* import kotlin.math.*
/** /**
@ -23,8 +24,8 @@ public object BoxMullerSampler : NormalizedGaussianSampler {
var state = Double.NaN var state = Double.NaN
override fun nextBufferBlocking(size: Int): DoubleBuffer { override fun nextBufferBlocking(size: Int): DoubleBuffer {
val xs = generator.nextDoubleBuffer(size) val xs = nextDoubleBuffer(size)
val ys = generator.nextDoubleBuffer(size) val ys = nextDoubleBuffer(size)
return DoubleBuffer(size) { index -> return DoubleBuffer(size) { index ->
if (state.isNaN()) { if (state.isNaN()) {

View File

@ -8,6 +8,7 @@ package space.kscience.kmath.stat
import space.kscience.kmath.chains.BlockingDoubleChain import space.kscience.kmath.chains.BlockingDoubleChain
import space.kscience.kmath.chains.Chain import space.kscience.kmath.chains.Chain
import space.kscience.kmath.structures.DoubleBuffer import space.kscience.kmath.structures.DoubleBuffer
import space.kscience.kmath.structures.nextDoubleBuffer
/** /**
* A possibly stateful chain producing random values. * A possibly stateful chain producing random values.
@ -31,7 +32,7 @@ public fun <R> RandomGenerator.chain(generator: suspend RandomGenerator.() -> R)
* A type-specific double chunk random chain * A type-specific double chunk random chain
*/ */
public class UniformDoubleChain(public val generator: RandomGenerator) : BlockingDoubleChain { 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 nextBuffer(size: Int): DoubleBuffer = nextBufferBlocking(size)
override suspend fun fork(): UniformDoubleChain = UniformDoubleChain(generator.fork()) override suspend fun fork(): UniformDoubleChain = UniformDoubleChain(generator.fork())

View File

@ -5,7 +5,6 @@
package space.kscience.kmath.stat package space.kscience.kmath.stat
import space.kscience.kmath.structures.DoubleBuffer
import kotlin.random.Random import kotlin.random.Random
/** /**
@ -22,11 +21,6 @@ public interface RandomGenerator {
*/ */
public fun nextDouble(): Double 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. * Gets the next random `Int` from the random number generator.
* *