Enable functions module and unmark Polynomial inline

This commit is contained in:
Iaroslav Postovalov 2020-09-10 15:22:01 +07:00
parent fc5ec8fed7
commit 2b15d69f11
No known key found for this signature in database
GPG Key ID: 70D5F4DCB0972F1B
4 changed files with 30 additions and 39 deletions

View File

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

View File

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

View File

@ -26,10 +26,10 @@ public fun F64Array.asStructure(): ViktorNDStructure = ViktorNDStructure(this)
@Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") @Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
public class ViktorNDField(public override val shape: IntArray) : NDField<Double, RealField, ViktorNDStructure> { public class ViktorNDField(public override val shape: IntArray) : NDField<Double, RealField, ViktorNDStructure> {
public override val zero: 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 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) public val strides: Strides = DefaultStrides(shape)

View File

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