forked from kscience/kmath
Compare commits
3 Commits
dev
...
apomytkina
Author | SHA1 | Date | |
---|---|---|---|
|
0e8eb44670 | ||
|
2cdaa6989c | ||
|
d9b719fe5a |
@ -167,9 +167,9 @@ public object MstLogicAlgebra : LogicAlgebra<MST> {
|
|||||||
override fun bindSymbolOrNull(value: String): MST = super.bindSymbolOrNull(value) ?: StringSymbol(value)
|
override fun bindSymbolOrNull(value: String): MST = super.bindSymbolOrNull(value) ?: StringSymbol(value)
|
||||||
|
|
||||||
override fun const(boolean: Boolean): Symbol = if (boolean) {
|
override fun const(boolean: Boolean): Symbol = if (boolean) {
|
||||||
LogicAlgebra.TRUE
|
BinaryLogic.TRUE
|
||||||
} else {
|
} else {
|
||||||
LogicAlgebra.FALSE
|
BinaryLogic.FALSE
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun MST.not(): MST = MST.Unary(Boolean::not.name, this)
|
override fun MST.not(): MST = MST.Unary(Boolean::not.name, this)
|
||||||
|
@ -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<T : Any> {
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
}
|
||||||
|
}
|
@ -5,16 +5,13 @@
|
|||||||
|
|
||||||
package space.kscience.kmath.operations
|
package space.kscience.kmath.operations
|
||||||
|
|
||||||
import space.kscience.kmath.expressions.Symbol
|
|
||||||
import space.kscience.kmath.expressions.symbol
|
|
||||||
import space.kscience.kmath.misc.UnstableKMathAPI
|
import space.kscience.kmath.misc.UnstableKMathAPI
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An algebra for generic boolean logic
|
* An algebra for generic boolean logic
|
||||||
*/
|
*/
|
||||||
@UnstableKMathAPI
|
@UnstableKMathAPI
|
||||||
public interface LogicAlgebra<T : Any> : Algebra<T> {
|
public interface LogicAlgebra<T : Any> : Algebra<T>, BinaryLogic<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represent constant [Boolean] as [T]
|
* Represent constant [Boolean] as [T]
|
||||||
*/
|
*/
|
||||||
@ -38,32 +35,6 @@ public interface LogicAlgebra<T : Any> : Algebra<T> {
|
|||||||
override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = { l, r ->
|
override fun binaryOperationFunction(operation: String): (left: T, right: T) -> T = { l, r ->
|
||||||
binaryOperation(operation, 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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
package space.kscience.kmath.structures
|
package space.kscience.kmath.structures
|
||||||
|
|
||||||
import kotlin.jvm.JvmInline
|
import kotlin.jvm.JvmInline
|
||||||
|
import kotlin.random.Random.Default.nextDouble
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specialized [MutableBuffer] implementation over [DoubleArray].
|
* Specialized [MutableBuffer] implementation over [DoubleArray].
|
||||||
@ -42,6 +43,11 @@ public value class DoubleBuffer(public val array: DoubleArray) : MutableBuffer<D
|
|||||||
*/
|
*/
|
||||||
public inline fun DoubleBuffer(size: Int, init: (Int) -> Double): DoubleBuffer = DoubleBuffer(DoubleArray(size) { init(it) })
|
public inline fun DoubleBuffer(size: Int, init: (Int) -> 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.
|
* Returns a new [DoubleBuffer] of given elements.
|
||||||
*/
|
*/
|
||||||
|
@ -12,6 +12,7 @@ import space.kscience.kmath.misc.UnstableKMathAPI
|
|||||||
import space.kscience.kmath.operations.DoubleField
|
import space.kscience.kmath.operations.DoubleField
|
||||||
import space.kscience.kmath.stat.RandomGenerator
|
import space.kscience.kmath.stat.RandomGenerator
|
||||||
import space.kscience.kmath.stat.nextBuffer
|
import space.kscience.kmath.stat.nextBuffer
|
||||||
|
import space.kscience.kmath.structures.nextDoubleBuffer
|
||||||
import kotlin.native.concurrent.ThreadLocal
|
import kotlin.native.concurrent.ThreadLocal
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
@ -35,7 +36,7 @@ internal class UniformHistogram1DTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun rebinDown() = runTest {
|
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)
|
val h2 = Histogram.uniform1D(DoubleField,0.03).produceFrom(h1)
|
||||||
|
|
||||||
assertEquals(10000, h2.bins.sumOf { it.binValue }.toInt())
|
assertEquals(10000, h2.bins.sumOf { it.binValue }.toInt())
|
||||||
@ -43,7 +44,7 @@ internal class UniformHistogram1DTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun rebinUp() = runTest {
|
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)
|
val h2 = Histogram.uniform1D(DoubleField,0.01).produceFrom(h1)
|
||||||
|
|
||||||
assertEquals(10000, h2.bins.sumOf { it.binValue }.toInt())
|
assertEquals(10000, h2.bins.sumOf { it.binValue }.toInt())
|
||||||
|
@ -8,3 +8,13 @@ package space.kscience.kmath.nd4j
|
|||||||
import space.kscience.kmath.misc.toIntExact
|
import space.kscience.kmath.misc.toIntExact
|
||||||
|
|
||||||
internal fun LongArray.toIntArray(): IntArray = IntArray(size) { this[it].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 }
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,7 @@ package space.kscience.kmath.samplers
|
|||||||
import space.kscience.kmath.chains.BlockingDoubleChain
|
import space.kscience.kmath.chains.BlockingDoubleChain
|
||||||
import space.kscience.kmath.stat.RandomGenerator
|
import space.kscience.kmath.stat.RandomGenerator
|
||||||
import space.kscience.kmath.structures.DoubleBuffer
|
import space.kscience.kmath.structures.DoubleBuffer
|
||||||
|
import space.kscience.kmath.structures.nextDoubleBuffer
|
||||||
import kotlin.math.*
|
import kotlin.math.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -23,8 +24,8 @@ public object BoxMullerSampler : NormalizedGaussianSampler {
|
|||||||
var state = Double.NaN
|
var state = Double.NaN
|
||||||
|
|
||||||
override fun nextBufferBlocking(size: Int): DoubleBuffer {
|
override fun nextBufferBlocking(size: Int): DoubleBuffer {
|
||||||
val xs = generator.nextDoubleBuffer(size)
|
val xs = nextDoubleBuffer(size)
|
||||||
val ys = generator.nextDoubleBuffer(size)
|
val ys = nextDoubleBuffer(size)
|
||||||
|
|
||||||
return DoubleBuffer(size) { index ->
|
return DoubleBuffer(size) { index ->
|
||||||
if (state.isNaN()) {
|
if (state.isNaN()) {
|
||||||
|
@ -8,6 +8,7 @@ package space.kscience.kmath.stat
|
|||||||
import space.kscience.kmath.chains.BlockingDoubleChain
|
import space.kscience.kmath.chains.BlockingDoubleChain
|
||||||
import space.kscience.kmath.chains.Chain
|
import space.kscience.kmath.chains.Chain
|
||||||
import space.kscience.kmath.structures.DoubleBuffer
|
import space.kscience.kmath.structures.DoubleBuffer
|
||||||
|
import space.kscience.kmath.structures.nextDoubleBuffer
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A possibly stateful chain producing random values.
|
* A possibly stateful chain producing random values.
|
||||||
@ -31,7 +32,7 @@ public fun <R> RandomGenerator.chain(generator: suspend RandomGenerator.() -> R)
|
|||||||
* A type-specific double chunk random chain
|
* A type-specific double chunk random chain
|
||||||
*/
|
*/
|
||||||
public class UniformDoubleChain(public val generator: RandomGenerator) : BlockingDoubleChain {
|
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 nextBuffer(size: Int): DoubleBuffer = nextBufferBlocking(size)
|
||||||
|
|
||||||
override suspend fun fork(): UniformDoubleChain = UniformDoubleChain(generator.fork())
|
override suspend fun fork(): UniformDoubleChain = UniformDoubleChain(generator.fork())
|
||||||
|
@ -22,11 +22,6 @@ public interface RandomGenerator {
|
|||||||
*/
|
*/
|
||||||
public fun nextDouble(): Double
|
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.
|
* Gets the next random `Int` from the random number generator.
|
||||||
*
|
*
|
||||||
|
@ -21,3 +21,11 @@ public fun <T, TT : TNumber, A> TensorFlowAlgebra<T, TT, A>.sin(
|
|||||||
public fun <T, TT : TNumber, A> TensorFlowAlgebra<T, TT, A>.cos(
|
public fun <T, TT : TNumber, A> TensorFlowAlgebra<T, TT, A>.cos(
|
||||||
arg: StructureND<T>,
|
arg: StructureND<T>,
|
||||||
): TensorFlowOutput<T, TT> where A : TrigonometricOperations<T>, A : Ring<T> = arg.operate { ops.math.cos(it) }
|
): TensorFlowOutput<T, TT> where A : TrigonometricOperations<T>, A : Ring<T> = arg.operate { ops.math.cos(it) }
|
||||||
|
|
||||||
|
public fun <T, TT : TNumber, A> TensorFlowAlgebra<T, TT, A>.tan(
|
||||||
|
arg: StructureND<T>,
|
||||||
|
): TensorFlowOutput<T, TT> where A : TrigonometricOperations<T>, A : Ring<T> = arg.operate { ops.math.tan(it) }
|
||||||
|
|
||||||
|
public fun <T, TT : TNumber, A> TensorFlowAlgebra<T, TT, A>.abs(
|
||||||
|
arg: StructureND<T>,
|
||||||
|
): TensorFlowOutput<T, TT> where A : TrigonometricOperations<T>, A : Ring<T> = arg.operate { ops.math.abs(it) }
|
Loading…
Reference in New Issue
Block a user