From 2b15d69f11513045a15a6405ff0148f0a0f2ce0f Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Thu, 10 Sep 2020 15:22:01 +0700 Subject: [PATCH] Enable functions module and unmark Polynomial inline --- .../scientifik/kmath/functions/Polynomial.kt | 8 +-- .../kmath/prob/RandomSourceGenerator.kt | 55 ++++++++----------- .../kmath/viktor/ViktorNDStructure.kt | 4 +- settings.gradle.kts | 2 +- 4 files changed, 30 insertions(+), 39 deletions(-) diff --git a/kmath-functions/src/commonMain/kotlin/scientifik/kmath/functions/Polynomial.kt b/kmath-functions/src/commonMain/kotlin/scientifik/kmath/functions/Polynomial.kt index 77196a121..6eadf4301 100644 --- a/kmath-functions/src/commonMain/kotlin/scientifik/kmath/functions/Polynomial.kt +++ b/kmath-functions/src/commonMain/kotlin/scientifik/kmath/functions/Polynomial.kt @@ -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(public val coefficients: List) { +public /*inline*/ class Polynomial(public val coefficients: List) { public constructor(vararg coefficients: T) : this(coefficients.toList()) } -public fun Polynomial.value(): Double = - coefficients.reduceIndexed { index: Int, acc: Double, d: Double -> acc + d.pow(index) } +public fun Polynomial.value(): Double = coefficients.reduceIndexed { index, acc, d -> acc + d.pow(index) } public fun > Polynomial.value(ring: C, arg: T): T = ring { if (coefficients.isEmpty()) return@ring zero @@ -26,7 +26,7 @@ public fun > Polynomial.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 } diff --git a/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/RandomSourceGenerator.kt b/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/RandomSourceGenerator.kt index f5a73a08b..eda36415a 100644 --- a/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/RandomSourceGenerator.kt +++ b/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/RandomSourceGenerator.kt @@ -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) diff --git a/kmath-viktor/src/main/kotlin/scientifik/kmath/viktor/ViktorNDStructure.kt b/kmath-viktor/src/main/kotlin/scientifik/kmath/viktor/ViktorNDStructure.kt index 64f2da4c1..cbd5357af 100644 --- a/kmath-viktor/src/main/kotlin/scientifik/kmath/viktor/ViktorNDStructure.kt +++ b/kmath-viktor/src/main/kotlin/scientifik/kmath/viktor/ViktorNDStructure.kt @@ -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 { 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) diff --git a/settings.gradle.kts b/settings.gradle.kts index 2e40b2abc..594c727cb 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -26,7 +26,7 @@ rootProject.name = "kmath" include( ":kmath-memory", ":kmath-core", -// ":kmath-functions", + ":kmath-functions", ":kmath-coroutines", ":kmath-histograms", ":kmath-commons",