Dev #194

Merged
altavir merged 266 commits from dev into master 2021-01-20 17:32:32 +03:00
4 changed files with 30 additions and 39 deletions
Showing only changes of commit 2b15d69f11 - Show all commits

View File

@ -8,16 +8,16 @@ import kotlin.contracts.contract
import kotlin.math.max
import kotlin.math.pow
// TODO make `inline`, when KT-41771 gets fixed
/**
* Polynomial coefficients without fixation on specific context they are applied to
* @param coefficients constant is the leftmost coefficient
*/
public inline class Polynomial<T : Any>(public val coefficients: List<T>) {
public /*inline*/ class Polynomial<T : Any>(public val coefficients: List<T>) {
public constructor(vararg coefficients: T) : this(coefficients.toList())
}
public fun Polynomial<Double>.value(): Double =
coefficients.reduceIndexed { index: Int, acc: Double, d: Double -> acc + d.pow(index) }
public fun Polynomial<Double>.value(): Double = coefficients.reduceIndexed { index, acc, d -> acc + d.pow(index) }
public fun <T : Any, C : Ring<T>> Polynomial<T>.value(ring: C, arg: T): T = ring {
if (coefficients.isEmpty()) return@ring zero
@ -26,7 +26,7 @@ public fun <T : Any, C : Ring<T>> Polynomial<T>.value(ring: C, arg: T): T = ring
for (index in 1 until coefficients.size) {
res += coefficients[index] * powerArg
//recalculating power on each step to avoid power costs on long polynomials
// recalculating power on each step to avoid power costs on long polynomials
powerArg *= arg
}

View File

@ -3,65 +3,56 @@ package scientifik.kmath.prob
import org.apache.commons.rng.UniformRandomProvider
import org.apache.commons.rng.simple.RandomSource
class RandomSourceGenerator(val source: RandomSource, seed: Long?) : RandomGenerator {
public class RandomSourceGenerator(public val source: RandomSource, seed: Long?) : RandomGenerator {
internal val random: UniformRandomProvider = seed?.let {
RandomSource.create(source, seed)
} ?: RandomSource.create(source)
override fun nextBoolean(): Boolean = random.nextBoolean()
public override fun nextBoolean(): Boolean = random.nextBoolean()
public override fun nextDouble(): Double = random.nextDouble()
public override fun nextInt(): Int = random.nextInt()
public override fun nextInt(until: Int): Int = random.nextInt(until)
public override fun nextLong(): Long = random.nextLong()
public override fun nextLong(until: Long): Long = random.nextLong(until)
override fun nextDouble(): Double = random.nextDouble()
override fun nextInt(): Int = random.nextInt()
override fun nextInt(until: Int): Int = random.nextInt(until)
override fun nextLong(): Long = random.nextLong()
override fun nextLong(until: Long): Long = random.nextLong(until)
override fun fillBytes(array: ByteArray, fromIndex: Int, toIndex: Int) {
public override fun fillBytes(array: ByteArray, fromIndex: Int, toIndex: Int) {
require(toIndex > fromIndex)
random.nextBytes(array, fromIndex, toIndex - fromIndex)
}
override fun fork(): RandomGenerator = RandomSourceGenerator(source, nextLong())
public override fun fork(): RandomGenerator = RandomSourceGenerator(source, nextLong())
}
inline class RandomGeneratorProvider(val generator: RandomGenerator) : UniformRandomProvider {
override fun nextBoolean(): Boolean = generator.nextBoolean()
public inline class RandomGeneratorProvider(public val generator: RandomGenerator) : UniformRandomProvider {
public override fun nextBoolean(): Boolean = generator.nextBoolean()
public override fun nextFloat(): Float = generator.nextDouble().toFloat()
override fun nextFloat(): Float = generator.nextDouble().toFloat()
override fun nextBytes(bytes: ByteArray) {
public override fun nextBytes(bytes: ByteArray) {
generator.fillBytes(bytes)
}
override fun nextBytes(bytes: ByteArray, start: Int, len: Int) {
public override fun nextBytes(bytes: ByteArray, start: Int, len: Int) {
generator.fillBytes(bytes, start, start + len)
}
override fun nextInt(): Int = generator.nextInt()
override fun nextInt(n: Int): Int = generator.nextInt(n)
override fun nextDouble(): Double = generator.nextDouble()
override fun nextLong(): Long = generator.nextLong()
override fun nextLong(n: Long): Long = generator.nextLong(n)
public override fun nextInt(): Int = generator.nextInt()
public override fun nextInt(n: Int): Int = generator.nextInt(n)
public override fun nextDouble(): Double = generator.nextDouble()
public override fun nextLong(): Long = generator.nextLong()
public override fun nextLong(n: Long): Long = generator.nextLong(n)
}
/**
* Represent this [RandomGenerator] as commons-rng [UniformRandomProvider] preserving and mirroring its current state.
* Getting new value from one of those changes the state of another.
*/
fun RandomGenerator.asUniformRandomProvider(): UniformRandomProvider = if (this is RandomSourceGenerator) {
public fun RandomGenerator.asUniformRandomProvider(): UniformRandomProvider = if (this is RandomSourceGenerator)
random
} else {
else
RandomGeneratorProvider(this)
}
fun RandomGenerator.Companion.fromSource(source: RandomSource, seed: Long? = null): RandomSourceGenerator =
public fun RandomGenerator.Companion.fromSource(source: RandomSource, seed: Long? = null): RandomSourceGenerator =
RandomSourceGenerator(source, seed)
fun RandomGenerator.Companion.mersenneTwister(seed: Long? = null): RandomSourceGenerator =
public fun RandomGenerator.Companion.mersenneTwister(seed: Long? = null): RandomSourceGenerator =
fromSource(RandomSource.MT, seed)

View File

@ -26,10 +26,10 @@ public fun F64Array.asStructure(): ViktorNDStructure = ViktorNDStructure(this)
@Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
public class ViktorNDField(public override val shape: IntArray) : NDField<Double, RealField, ViktorNDStructure> {
public override val zero: ViktorNDStructure
get() = F64Array.full(init = 0.0, shape = *shape).asStructure()
get() = F64Array.full(init = 0.0, shape = shape).asStructure()
public override val one: ViktorNDStructure
get() = F64Array.full(init = 1.0, shape = *shape).asStructure()
get() = F64Array.full(init = 1.0, shape = shape).asStructure()
public val strides: Strides = DefaultStrides(shape)

View File

@ -26,7 +26,7 @@ rootProject.name = "kmath"
include(
":kmath-memory",
":kmath-core",
// ":kmath-functions",
":kmath-functions",
":kmath-coroutines",
":kmath-histograms",
":kmath-commons",