From d9b719fe5a7a731abd9bc5717ce26f096c73caab Mon Sep 17 00:00:00 2001 From: apomytkina Date: Sun, 15 May 2022 16:55:55 +0300 Subject: [PATCH 1/3] DoubleBuffer generator was moved to DoubleBuffer file and factory functions were added for LongArray --- .../space/kscience/kmath/structures/DoubleBuffer.kt | 6 ++++++ .../kscience/kmath/histogram/UniformHistogram1DTest.kt | 5 +++-- .../main/kotlin/space/kscience/kmath/nd4j/arrays.kt | 10 ++++++++++ .../space/kscience/kmath/samplers/BoxMullerSampler.kt | 5 +++-- .../kotlin/space/kscience/kmath/stat/RandomChain.kt | 3 ++- .../space/kscience/kmath/stat/RandomGenerator.kt | 5 ----- 6 files changed, 24 insertions(+), 10 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt index f4388a477..0bbbec138 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt @@ -6,6 +6,7 @@ package space.kscience.kmath.structures import kotlin.jvm.JvmInline +import kotlin.random.Random.Default.nextDouble /** * Specialized [MutableBuffer] implementation over [DoubleArray]. @@ -42,6 +43,11 @@ public value class DoubleBuffer(public val array: DoubleArray) : MutableBuffer Double): DoubleBuffer = DoubleBuffer(DoubleArray(size) { init(it) }) +/** + * A chunk of doubles of given [size]. + */ +public fun nextDoubleBuffer(size: Int): DoubleBuffer = DoubleBuffer(size) { nextDouble() } + /** * Returns a new [DoubleBuffer] of given elements. */ diff --git a/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/UniformHistogram1DTest.kt b/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/UniformHistogram1DTest.kt index 09bf3939d..86f149ecc 100644 --- a/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/UniformHistogram1DTest.kt +++ b/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/UniformHistogram1DTest.kt @@ -12,6 +12,7 @@ import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.stat.RandomGenerator import space.kscience.kmath.stat.nextBuffer +import space.kscience.kmath.structures.nextDoubleBuffer import kotlin.native.concurrent.ThreadLocal import kotlin.test.Test import kotlin.test.assertEquals @@ -35,7 +36,7 @@ internal class UniformHistogram1DTest { @Test fun rebinDown() = runTest { - val h1 = Histogram.uniform1D(DoubleField, 0.01).produce(generator.nextDoubleBuffer(10000)) + val h1 = Histogram.uniform1D(DoubleField, 0.01).produce(nextDoubleBuffer(10000)) val h2 = Histogram.uniform1D(DoubleField,0.03).produceFrom(h1) assertEquals(10000, h2.bins.sumOf { it.binValue }.toInt()) @@ -43,7 +44,7 @@ internal class UniformHistogram1DTest { @Test fun rebinUp() = runTest { - val h1 = Histogram.uniform1D(DoubleField, 0.03).produce(generator.nextDoubleBuffer(10000)) + val h1 = Histogram.uniform1D(DoubleField, 0.03).produce(nextDoubleBuffer(10000)) val h2 = Histogram.uniform1D(DoubleField,0.01).produceFrom(h1) assertEquals(10000, h2.bins.sumOf { it.binValue }.toInt()) diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/arrays.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/arrays.kt index 3ca756600..db910f158 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/arrays.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/arrays.kt @@ -8,3 +8,13 @@ package space.kscience.kmath.nd4j import space.kscience.kmath.misc.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 } \ No newline at end of file diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/BoxMullerSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/BoxMullerSampler.kt index b3c014553..9719cef0a 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/BoxMullerSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/BoxMullerSampler.kt @@ -8,6 +8,7 @@ package space.kscience.kmath.samplers import space.kscience.kmath.chains.BlockingDoubleChain import space.kscience.kmath.stat.RandomGenerator import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.nextDoubleBuffer import kotlin.math.* /** @@ -23,8 +24,8 @@ public object BoxMullerSampler : NormalizedGaussianSampler { var state = Double.NaN override fun nextBufferBlocking(size: Int): DoubleBuffer { - val xs = generator.nextDoubleBuffer(size) - val ys = generator.nextDoubleBuffer(size) + val xs = nextDoubleBuffer(size) + val ys = nextDoubleBuffer(size) return DoubleBuffer(size) { index -> if (state.isNaN()) { diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomChain.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomChain.kt index d4bc36b5b..ab05b3aa1 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomChain.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomChain.kt @@ -8,6 +8,7 @@ package space.kscience.kmath.stat import space.kscience.kmath.chains.BlockingDoubleChain import space.kscience.kmath.chains.Chain import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.nextDoubleBuffer /** * A possibly stateful chain producing random values. @@ -31,7 +32,7 @@ public fun RandomGenerator.chain(generator: suspend RandomGenerator.() -> R) * A type-specific double chunk random chain */ 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 fork(): UniformDoubleChain = UniformDoubleChain(generator.fork()) diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt index f280a78aa..cce92a877 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt @@ -22,11 +22,6 @@ public interface RandomGenerator { */ 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. * -- 2.34.1 From 2cdaa6989c467c9239a0948382c3ed661473f466 Mon Sep 17 00:00:00 2001 From: apomytkina Date: Sun, 22 May 2022 16:57:31 +0300 Subject: [PATCH 2/3] New Builder Functions Were Added --- .../space/kscience/kmath/nd4j/ArraysTest.kt | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/ArraysTest.kt diff --git a/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/ArraysTest.kt b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/ArraysTest.kt new file mode 100644 index 000000000..b1f258424 --- /dev/null +++ b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/ArraysTest.kt @@ -0,0 +1,43 @@ +/* + * 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.nd4j + +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals + +class ArraysTest { + @Test + fun checkLinspaceBuilder() { + val array = LongArray(3).linspace(15, 22) + assertEquals(array[0], 15) + assertEquals(array[1], 18) + assertEquals(array[2], 21) + } + + @Test + fun checkZerosBuilder() { + val array = LongArray(3).zeros() + assertEquals(array[0], 0) + assertEquals(array[1], 0) + assertEquals(array[2], 0) + } + + @Test + fun checkOnesBuilder() { + val array = LongArray(3).ones() + assertEquals(array[0], 1) + assertEquals(array[1], 1) + assertEquals(array[2], 1) + } + + @Test + fun checkRepeatBuilder() { + val array = repeat(5, 3) + assertEquals(array[0], 5) + assertEquals(array[1], 5) + assertEquals(array[2], 5) + } +} \ No newline at end of file -- 2.34.1 From 0e8eb44670181a828ac4b9858386b8f3204bef8b Mon Sep 17 00:00:00 2001 From: "anna.pomytkina" Date: Sun, 5 Jun 2022 13:36:00 +0300 Subject: [PATCH 3/3] new TensorFlow operations are added. Binary Logic functions were moved into separate interface from LinearAlgebra --- .../kscience/kmath/expressions/MstAlgebra.kt | 4 +-- .../kscience/kmath/operations/BinaryLogic.kt | 36 +++++++++++++++++++ .../kscience/kmath/operations/LogicAlgebra.kt | 31 +--------------- .../kscience/kmath/tensorflow/tfOperations.kt | 10 +++++- 4 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BinaryLogic.kt diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt index 4bd2a6c53..b7ee695d7 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt @@ -167,9 +167,9 @@ public object MstLogicAlgebra : LogicAlgebra { override fun bindSymbolOrNull(value: String): MST = super.bindSymbolOrNull(value) ?: StringSymbol(value) override fun const(boolean: Boolean): Symbol = if (boolean) { - LogicAlgebra.TRUE + BinaryLogic.TRUE } else { - LogicAlgebra.FALSE + BinaryLogic.FALSE } override fun MST.not(): MST = MST.Unary(Boolean::not.name, this) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BinaryLogic.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BinaryLogic.kt new file mode 100644 index 000000000..75cbeca05 --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BinaryLogic.kt @@ -0,0 +1,36 @@ +/* + * 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.operations + +import space.kscience.kmath.expressions.Symbol +import space.kscience.kmath.expressions.symbol + + interface BinaryLogic { + /** + * Logic 'not' + */ + public operator fun T.not(): T + + /** + * Logic 'and' + */ + public infix fun T.and(other: T): T + + /** + * Logic 'or' + */ + public infix fun T.or(other: T): T + + /** + * Logic 'xor' + */ + public infix fun T.xor(other: T): T + + companion object { + public val TRUE: Symbol by symbol + public val FALSE: Symbol by symbol + } +} \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt index 9037525e1..1af6afd87 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/LogicAlgebra.kt @@ -5,16 +5,13 @@ package space.kscience.kmath.operations -import space.kscience.kmath.expressions.Symbol -import space.kscience.kmath.expressions.symbol import space.kscience.kmath.misc.UnstableKMathAPI /** * An algebra for generic boolean logic */ @UnstableKMathAPI -public interface LogicAlgebra : Algebra { - +public interface LogicAlgebra : Algebra, BinaryLogic { /** * Represent constant [Boolean] as [T] */ @@ -38,32 +35,6 @@ public interface LogicAlgebra : Algebra { override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = { l, r -> binaryOperation(operation, l, r) } - - /** - * Logic 'not' - */ - public operator fun T.not(): T - - /** - * Logic 'and' - */ - public infix fun T.and(other: T): T - - /** - * Logic 'or' - */ - public infix fun T.or(other: T): T - - /** - * Logic 'xor' - */ - public infix fun T.xor(other: T): T - - - public companion object { - public val TRUE: Symbol by symbol - public val FALSE: Symbol by symbol - } } /** diff --git a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/tfOperations.kt b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/tfOperations.kt index f67c333ce..e37608adf 100644 --- a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/tfOperations.kt +++ b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/tfOperations.kt @@ -20,4 +20,12 @@ public fun TensorFlowAlgebra.sin( public fun TensorFlowAlgebra.cos( arg: StructureND, -): TensorFlowOutput where A : TrigonometricOperations, A : Ring = arg.operate { ops.math.cos(it) } \ No newline at end of file +): TensorFlowOutput where A : TrigonometricOperations, A : Ring = arg.operate { ops.math.cos(it) } + +public fun TensorFlowAlgebra.tan( + arg: StructureND, +): TensorFlowOutput where A : TrigonometricOperations, A : Ring = arg.operate { ops.math.tan(it) } + +public fun TensorFlowAlgebra.abs( + arg: StructureND, +): TensorFlowOutput where A : TrigonometricOperations, A : Ring = arg.operate { ops.math.abs(it) } \ No newline at end of file -- 2.34.1