forked from kscience/kmath
minor update
This commit is contained in:
parent
651f8323f3
commit
5a8a45cf6a
@ -58,32 +58,35 @@ internal class DivExpession<T>(val context: Field<T>, val expr: Expression<T>, v
|
||||
override fun invoke(arguments: Map<String, T>): T = context.divide(expr.invoke(arguments), second.invoke(arguments))
|
||||
}
|
||||
|
||||
class ExpressionField<T>(val field: Field<T>) : Field<Expression<T>>, ExpressionContext<T> {
|
||||
|
||||
override val zero: Expression<T> = ConstantExpression(field.zero)
|
||||
|
||||
override val one: Expression<T> = ConstantExpression(field.one)
|
||||
open class ExpressionSpace<T>(val space: Space<T>) : Space<Expression<T>>, ExpressionContext<T> {
|
||||
override val zero: Expression<T> = ConstantExpression(space.zero)
|
||||
|
||||
override fun const(value: T): Expression<T> = ConstantExpression(value)
|
||||
|
||||
override fun variable(name: String, default: T?): Expression<T> = VariableExpression(name, default)
|
||||
|
||||
override fun add(a: Expression<T>, b: Expression<T>): Expression<T> = SumExpression(field, a, b)
|
||||
override fun add(a: Expression<T>, b: Expression<T>): Expression<T> = SumExpression(space, a, b)
|
||||
|
||||
override fun multiply(a: Expression<T>, k: Number): Expression<T> = ConstProductExpession(field, a, k)
|
||||
|
||||
override fun multiply(a: Expression<T>, b: Expression<T>): Expression<T> = ProductExpression(field, a, b)
|
||||
|
||||
override fun divide(a: Expression<T>, b: Expression<T>): Expression<T> = DivExpession(field, a, b)
|
||||
override fun multiply(a: Expression<T>, k: Number): Expression<T> = ConstProductExpession(space, a, k)
|
||||
|
||||
|
||||
operator fun Expression<T>.plus(arg: T) = this + const(arg)
|
||||
operator fun Expression<T>.minus(arg: T) = this - const(arg)
|
||||
operator fun Expression<T>.times(arg: T) = this * const(arg)
|
||||
operator fun Expression<T>.div(arg: T) = this / const(arg)
|
||||
|
||||
operator fun T.plus(arg: Expression<T>) = arg + this
|
||||
operator fun T.minus(arg: Expression<T>) = arg - this
|
||||
}
|
||||
|
||||
|
||||
class ExpressionField<T>(val field: Field<T>) : Field<Expression<T>>, ExpressionSpace<T>(field) {
|
||||
override val one: Expression<T> = ConstantExpression(field.one)
|
||||
override fun multiply(a: Expression<T>, b: Expression<T>): Expression<T> = ProductExpression(field, a, b)
|
||||
|
||||
override fun divide(a: Expression<T>, b: Expression<T>): Expression<T> = DivExpession(field, a, b)
|
||||
|
||||
operator fun Expression<T>.times(arg: T) = this * const(arg)
|
||||
operator fun Expression<T>.div(arg: T) = this / const(arg)
|
||||
|
||||
operator fun T.times(arg: Expression<T>) = arg * this
|
||||
operator fun T.div(arg: Expression<T>) = arg / this
|
||||
}
|
@ -2,7 +2,6 @@ plugins {
|
||||
`npm-multiplatform`
|
||||
}
|
||||
|
||||
// Just an example how we can collapse nested DSL for simple declarations
|
||||
kotlin.sourceSets.commonMain {
|
||||
dependencies {
|
||||
api(project(":kmath-core"))
|
||||
|
@ -5,12 +5,9 @@ import scientifik.kmath.chains.Chain
|
||||
|
||||
/**
|
||||
* A possibly stateful chain producing random values.
|
||||
* TODO make random chain properly fork generator
|
||||
*/
|
||||
class RandomChain<out R>(val generator: RandomGenerator, private val gen: suspend RandomGenerator.() -> R) : Chain<R> {
|
||||
private val atomicValue = atomic<R?>(null)
|
||||
|
||||
override suspend fun next(): R = generator.gen().also { atomicValue.lazySet(it) }
|
||||
override suspend fun next(): R = generator.gen()
|
||||
|
||||
override fun fork(): Chain<R> = RandomChain(generator.fork(), gen)
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package scientifik.kmath.prob
|
||||
|
||||
import kotlin.random.Random
|
||||
|
||||
/**
|
||||
* A basic generator
|
||||
*/
|
||||
@ -10,9 +12,30 @@ interface RandomGenerator {
|
||||
fun nextBlock(size: Int): ByteArray
|
||||
|
||||
/**
|
||||
* Fork the current state of generator
|
||||
* Create a new generator which is independent from current generator (operations on new generator do not affect this one
|
||||
* and vise versa). The statistical properties of new generator should be the same as for this one.
|
||||
* For pseudo-random generator, the fork is keeping the same sequence of numbers for given call order for each run.
|
||||
*
|
||||
* The thread safety of this operation is not guaranteed since it could affect the state of the generator.
|
||||
*/
|
||||
fun fork(): RandomGenerator
|
||||
|
||||
companion object
|
||||
companion object {
|
||||
val default by lazy { DefaultGenerator(Random.nextLong()) }
|
||||
}
|
||||
}
|
||||
|
||||
class DefaultGenerator(seed: Long?) : RandomGenerator {
|
||||
private val random = seed?.let { Random(it) } ?: Random
|
||||
|
||||
override fun nextDouble(): Double = random.nextDouble()
|
||||
|
||||
override fun nextInt(): Int = random.nextInt()
|
||||
|
||||
override fun nextLong(): Long = random.nextLong()
|
||||
|
||||
override fun nextBlock(size: Int): ByteArray = random.nextBytes(size)
|
||||
|
||||
override fun fork(): RandomGenerator = DefaultGenerator(nextLong())
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user