Even more documentation comments and minor refactorings #144
@ -3,9 +3,9 @@ plugins {
|
|||||||
id("org.jetbrains.changelog") version "0.4.0"
|
id("org.jetbrains.changelog") version "0.4.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
val kmathVersion by extra("0.2.0-dev-1")
|
val kmathVersion: String by extra("0.2.0-dev-1")
|
||||||
val bintrayRepo by extra("kscience")
|
val bintrayRepo: String by extra("kscience")
|
||||||
val githubProject by extra("kmath")
|
val githubProject: String by extra("kmath")
|
||||||
|
|
||||||
allprojects {
|
allprojects {
|
||||||
repositories {
|
repositories {
|
||||||
|
@ -10,30 +10,29 @@ class ArrayBenchmark {
|
|||||||
@Benchmark
|
@Benchmark
|
||||||
fun benchmarkArrayRead() {
|
fun benchmarkArrayRead() {
|
||||||
var res = 0
|
var res = 0
|
||||||
for (i in 1.._root_ide_package_.kscience.kmath.structures.ArrayBenchmark.Companion.size) res += _root_ide_package_.kscience.kmath.structures.ArrayBenchmark.Companion.array[_root_ide_package_.kscience.kmath.structures.ArrayBenchmark.Companion.size - i]
|
for (i in 1..size) res += array[size - i]
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
fun benchmarkBufferRead() {
|
fun benchmarkBufferRead() {
|
||||||
var res = 0
|
var res = 0
|
||||||
for (i in 1.._root_ide_package_.kscience.kmath.structures.ArrayBenchmark.Companion.size) res += _root_ide_package_.kscience.kmath.structures.ArrayBenchmark.Companion.arrayBuffer.get(
|
for (i in 1..size) res += arrayBuffer.get(
|
||||||
_root_ide_package_.kscience.kmath.structures.ArrayBenchmark.Companion.size - i)
|
size - i
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
fun nativeBufferRead() {
|
fun nativeBufferRead() {
|
||||||
var res = 0
|
var res = 0
|
||||||
for (i in 1.._root_ide_package_.kscience.kmath.structures.ArrayBenchmark.Companion.size) res += _root_ide_package_.kscience.kmath.structures.ArrayBenchmark.Companion.nativeBuffer.get(
|
for (i in 1..size) res += nativeBuffer.get(
|
||||||
_root_ide_package_.kscience.kmath.structures.ArrayBenchmark.Companion.size - i)
|
size - i
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val size: Int = 1000
|
const val size: Int = 1000
|
||||||
val array: IntArray = IntArray(_root_ide_package_.kscience.kmath.structures.ArrayBenchmark.Companion.size) { it }
|
val array: IntArray = IntArray(size) { it }
|
||||||
val arrayBuffer: IntBuffer = IntBuffer.wrap(_root_ide_package_.kscience.kmath.structures.ArrayBenchmark.Companion.array)
|
val arrayBuffer: IntBuffer = IntBuffer.wrap(array)
|
||||||
|
val nativeBuffer: IntBuffer = IntBuffer.allocate(size).also { for (i in 0 until size) it.put(i, i) }
|
||||||
val nativeBuffer: IntBuffer = IntBuffer.allocate(_root_ide_package_.kscience.kmath.structures.ArrayBenchmark.Companion.size).also {
|
|
||||||
for (i in 0 until _root_ide_package_.kscience.kmath.structures.ArrayBenchmark.Companion.size) it.put(i, i)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ class BufferBenchmark {
|
|||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
fun genericRealBufferReadWrite() {
|
fun genericRealBufferReadWrite() {
|
||||||
val buffer = RealBuffer(size){it.toDouble()}
|
val buffer = RealBuffer(size) { it.toDouble() }
|
||||||
|
|
||||||
(0 until size).forEach {
|
(0 until size).forEach {
|
||||||
buffer[it]
|
buffer[it]
|
||||||
@ -20,7 +20,7 @@ class BufferBenchmark {
|
|||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
fun complexBufferReadWrite() {
|
fun complexBufferReadWrite() {
|
||||||
val buffer = MutableBuffer.complex(size / 2){Complex(it.toDouble(), -it.toDouble())}
|
val buffer = MutableBuffer.complex(size / 2) { Complex(it.toDouble(), -it.toDouble()) }
|
||||||
|
|
||||||
(0 until size / 2).forEach {
|
(0 until size / 2).forEach {
|
||||||
buffer[it]
|
buffer[it]
|
||||||
@ -28,6 +28,6 @@ class BufferBenchmark {
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val size = 100
|
const val size: Int = 100
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -40,11 +40,10 @@ class NDFieldBenchmark {
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val dim = 1000
|
const val dim: Int = 1000
|
||||||
val n = 100
|
const val n: Int = 100
|
||||||
|
val bufferedField: BufferedNDField<Double, RealField> = NDField.auto(RealField, dim, dim)
|
||||||
val bufferedField = NDField.auto(RealField, dim, dim)
|
val specializedField: RealNDField = NDField.real(dim, dim)
|
||||||
val specializedField = NDField.real(dim, dim)
|
val genericField: BoxingNDField<Double, RealField> = NDField.boxing(RealField, dim, dim)
|
||||||
val genericField = NDField.boxing(RealField, dim, dim)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,8 +10,8 @@ import org.openjdk.jmh.annotations.State
|
|||||||
|
|
||||||
@State(Scope.Benchmark)
|
@State(Scope.Benchmark)
|
||||||
class ViktorBenchmark {
|
class ViktorBenchmark {
|
||||||
final val dim = 1000
|
final val dim: Int = 1000
|
||||||
final val n = 100
|
final val n: Int = 100
|
||||||
|
|
||||||
// automatically build context most suited for given type.
|
// automatically build context most suited for given type.
|
||||||
final val autoField: BufferedNDField<Double, RealField> = NDField.auto(RealField, dim, dim)
|
final val autoField: BufferedNDField<Double, RealField> = NDField.auto(RealField, dim, dim)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
//package kscience.kmath.ast
|
package kscience.kmath.ast
|
||||||
//
|
//
|
||||||
//import kscience.kmath.asm.compile
|
//import kscience.kmath.asm.compile
|
||||||
//import kscience.kmath.expressions.Expression
|
//import kscience.kmath.expressions.Expression
|
||||||
|
@ -10,10 +10,8 @@ import org.apache.commons.rng.simple.RandomSource
|
|||||||
import java.time.Duration
|
import java.time.Duration
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
|
|
||||||
|
private fun runChain(): Duration {
|
||||||
private suspend fun runChain(): Duration {
|
|
||||||
val generator = RandomGenerator.fromSource(RandomSource.MT, 123L)
|
val generator = RandomGenerator.fromSource(RandomSource.MT, 123L)
|
||||||
|
|
||||||
val normal = Distribution.normal(NormalSamplerMethod.Ziggurat)
|
val normal = Distribution.normal(NormalSamplerMethod.Ziggurat)
|
||||||
val chain = normal.sample(generator) as BlockingRealChain
|
val chain = normal.sample(generator) as BlockingRealChain
|
||||||
|
|
||||||
@ -67,5 +65,4 @@ fun main() {
|
|||||||
println("Chain: ${chainJob.await()}")
|
println("Chain: ${chainJob.await()}")
|
||||||
println("Direct: ${directJob.await()}")
|
println("Direct: ${directJob.await()}")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
@ -12,24 +12,24 @@ fun main() {
|
|||||||
val structure = BufferNDStructure(strides, buffer)
|
val structure = BufferNDStructure(strides, buffer)
|
||||||
|
|
||||||
measureTimeMillis {
|
measureTimeMillis {
|
||||||
var res: Double = 0.0
|
var res = 0.0
|
||||||
strides.indices().forEach { res = structure[it] }
|
strides.indices().forEach { res = structure[it] }
|
||||||
} // warmup
|
} // warmup
|
||||||
|
|
||||||
val time1 = measureTimeMillis {
|
val time1 = measureTimeMillis {
|
||||||
var res: Double = 0.0
|
var res = 0.0
|
||||||
strides.indices().forEach { res = structure[it] }
|
strides.indices().forEach { res = structure[it] }
|
||||||
}
|
}
|
||||||
println("Structure reading finished in $time1 millis")
|
println("Structure reading finished in $time1 millis")
|
||||||
|
|
||||||
val time2 = measureTimeMillis {
|
val time2 = measureTimeMillis {
|
||||||
var res: Double = 0.0
|
var res = 0.0
|
||||||
strides.indices().forEach { res = buffer[strides.offset(it)] }
|
strides.indices().forEach { res = buffer[strides.offset(it)] }
|
||||||
}
|
}
|
||||||
println("Buffer reading finished in $time2 millis")
|
println("Buffer reading finished in $time2 millis")
|
||||||
|
|
||||||
val time3 = measureTimeMillis {
|
val time3 = measureTimeMillis {
|
||||||
var res: Double = 0.0
|
var res = 0.0
|
||||||
strides.indices().forEach { res = array[strides.offset(it)] }
|
strides.indices().forEach { res = array[strides.offset(it)] }
|
||||||
}
|
}
|
||||||
println("Array reading finished in $time3 millis")
|
println("Array reading finished in $time3 millis")
|
||||||
|
@ -32,7 +32,7 @@ internal class AsmBuilder<T> internal constructor(
|
|||||||
* Internal classloader of [AsmBuilder] with alias to define class from byte array.
|
* Internal classloader of [AsmBuilder] with alias to define class from byte array.
|
||||||
*/
|
*/
|
||||||
private class ClassLoader(parent: java.lang.ClassLoader) : java.lang.ClassLoader(parent) {
|
private class ClassLoader(parent: java.lang.ClassLoader) : java.lang.ClassLoader(parent) {
|
||||||
internal fun defineClass(name: String?, b: ByteArray): Class<*> = defineClass(name, b, 0, b.size)
|
fun defineClass(name: String?, b: ByteArray): Class<*> = defineClass(name, b, 0, b.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -94,7 +94,7 @@ public class DiffExpression(public val function: DerivativeStructureField.() ->
|
|||||||
* TODO make result [DiffExpression]
|
* TODO make result [DiffExpression]
|
||||||
*/
|
*/
|
||||||
public fun derivative(orders: Map<String, Int>): Expression<Double> = Expression { arguments ->
|
public fun derivative(orders: Map<String, Int>): Expression<Double> = Expression { arguments ->
|
||||||
(DerivativeStructureField(orders.values.max() ?: 0, arguments)) { function().deriv(orders) }
|
(DerivativeStructureField(orders.values.maxOrNull() ?: 0, arguments)) { function().deriv(orders) }
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO add gradient and maybe other vector operators
|
//TODO add gradient and maybe other vector operators
|
||||||
|
@ -4,7 +4,7 @@ import kscience.kmath.operations.*
|
|||||||
|
|
||||||
internal class FunctionalUnaryOperation<T>(val context: Algebra<T>, val name: String, private val expr: Expression<T>) :
|
internal class FunctionalUnaryOperation<T>(val context: Algebra<T>, val name: String, private val expr: Expression<T>) :
|
||||||
Expression<T> {
|
Expression<T> {
|
||||||
public override operator fun invoke(arguments: Map<String, T>): T =
|
override operator fun invoke(arguments: Map<String, T>): T =
|
||||||
context.unaryOperation(name, expr.invoke(arguments))
|
context.unaryOperation(name, expr.invoke(arguments))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -14,17 +14,17 @@ internal class FunctionalBinaryOperation<T>(
|
|||||||
val first: Expression<T>,
|
val first: Expression<T>,
|
||||||
val second: Expression<T>
|
val second: Expression<T>
|
||||||
) : Expression<T> {
|
) : Expression<T> {
|
||||||
public override operator fun invoke(arguments: Map<String, T>): T =
|
override operator fun invoke(arguments: Map<String, T>): T =
|
||||||
context.binaryOperation(name, first.invoke(arguments), second.invoke(arguments))
|
context.binaryOperation(name, first.invoke(arguments), second.invoke(arguments))
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class FunctionalVariableExpression<T>(val name: String, val default: T? = null) : Expression<T> {
|
internal class FunctionalVariableExpression<T>(val name: String, val default: T? = null) : Expression<T> {
|
||||||
public override operator fun invoke(arguments: Map<String, T>): T =
|
override operator fun invoke(arguments: Map<String, T>): T =
|
||||||
arguments[name] ?: default ?: error("Parameter not found: $name")
|
arguments[name] ?: default ?: error("Parameter not found: $name")
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class FunctionalConstantExpression<T>(val value: T) : Expression<T> {
|
internal class FunctionalConstantExpression<T>(val value: T) : Expression<T> {
|
||||||
public override operator fun invoke(arguments: Map<String, T>): T = value
|
override operator fun invoke(arguments: Map<String, T>): T = value
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class FunctionalConstProductExpression<T>(
|
internal class FunctionalConstProductExpression<T>(
|
||||||
@ -32,7 +32,7 @@ internal class FunctionalConstProductExpression<T>(
|
|||||||
private val expr: Expression<T>,
|
private val expr: Expression<T>,
|
||||||
val const: Number
|
val const: Number
|
||||||
) : Expression<T> {
|
) : Expression<T> {
|
||||||
public override operator fun invoke(arguments: Map<String, T>): T = context.multiply(expr.invoke(arguments), const)
|
override operator fun invoke(arguments: Map<String, T>): T = context.multiply(expr.invoke(arguments), const)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -139,16 +139,27 @@ public open class FunctionalExpressionField<T, A>(algebra: A) :
|
|||||||
public open class FunctionalExpressionExtendedField<T, A>(algebra: A) :
|
public open class FunctionalExpressionExtendedField<T, A>(algebra: A) :
|
||||||
FunctionalExpressionField<T, A>(algebra),
|
FunctionalExpressionField<T, A>(algebra),
|
||||||
ExtendedField<Expression<T>> where A : ExtendedField<T>, A : NumericAlgebra<T> {
|
ExtendedField<Expression<T>> where A : ExtendedField<T>, A : NumericAlgebra<T> {
|
||||||
public override fun sin(arg: Expression<T>): Expression<T> = unaryOperation(TrigonometricOperations.SIN_OPERATION, arg)
|
public override fun sin(arg: Expression<T>): Expression<T> =
|
||||||
public override fun cos(arg: Expression<T>): Expression<T> = unaryOperation(TrigonometricOperations.COS_OPERATION, arg)
|
unaryOperation(TrigonometricOperations.SIN_OPERATION, arg)
|
||||||
public override fun asin(arg: Expression<T>): Expression<T> = unaryOperation(TrigonometricOperations.ASIN_OPERATION, arg)
|
|
||||||
public override fun acos(arg: Expression<T>): Expression<T> = unaryOperation(TrigonometricOperations.ACOS_OPERATION, arg)
|
public override fun cos(arg: Expression<T>): Expression<T> =
|
||||||
public override fun atan(arg: Expression<T>): Expression<T> = unaryOperation(TrigonometricOperations.ATAN_OPERATION, arg)
|
unaryOperation(TrigonometricOperations.COS_OPERATION, arg)
|
||||||
|
|
||||||
|
public override fun asin(arg: Expression<T>): Expression<T> =
|
||||||
|
unaryOperation(TrigonometricOperations.ASIN_OPERATION, arg)
|
||||||
|
|
||||||
|
public override fun acos(arg: Expression<T>): Expression<T> =
|
||||||
|
unaryOperation(TrigonometricOperations.ACOS_OPERATION, arg)
|
||||||
|
|
||||||
|
public override fun atan(arg: Expression<T>): Expression<T> =
|
||||||
|
unaryOperation(TrigonometricOperations.ATAN_OPERATION, arg)
|
||||||
|
|
||||||
public override fun power(arg: Expression<T>, pow: Number): Expression<T> =
|
public override fun power(arg: Expression<T>, pow: Number): Expression<T> =
|
||||||
binaryOperation(PowerOperations.POW_OPERATION, arg, number(pow))
|
binaryOperation(PowerOperations.POW_OPERATION, arg, number(pow))
|
||||||
|
|
||||||
public override fun exp(arg: Expression<T>): Expression<T> = unaryOperation(ExponentialOperations.EXP_OPERATION, arg)
|
public override fun exp(arg: Expression<T>): Expression<T> =
|
||||||
|
unaryOperation(ExponentialOperations.EXP_OPERATION, arg)
|
||||||
|
|
||||||
public override fun ln(arg: Expression<T>): Expression<T> = unaryOperation(ExponentialOperations.LN_OPERATION, arg)
|
public override fun ln(arg: Expression<T>): Expression<T> = unaryOperation(ExponentialOperations.LN_OPERATION, arg)
|
||||||
|
|
||||||
public override fun unaryOperation(operation: String, arg: Expression<T>): Expression<T> =
|
public override fun unaryOperation(operation: String, arg: Expression<T>): Expression<T> =
|
||||||
|
@ -24,7 +24,11 @@ public interface FeaturedMatrix<T : Any> : Matrix<T> {
|
|||||||
public companion object
|
public companion object
|
||||||
}
|
}
|
||||||
|
|
||||||
public inline fun Structure2D.Companion.real(rows: Int, columns: Int, initializer: (Int, Int) -> Double): Matrix<Double> =
|
public inline fun Structure2D.Companion.real(
|
||||||
|
rows: Int,
|
||||||
|
columns: Int,
|
||||||
|
initializer: (Int, Int) -> Double
|
||||||
|
): Matrix<Double> =
|
||||||
MatrixContext.real.produce(rows, columns, initializer)
|
MatrixContext.real.produce(rows, columns, initializer)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
package kscience.kmath.misc
|
package kscience.kmath.misc
|
||||||
|
|
||||||
import kscience.kmath.linear.Point
|
import kscience.kmath.linear.Point
|
||||||
import kscience.kmath.operations.ExtendedField
|
import kscience.kmath.operations.*
|
||||||
import kscience.kmath.operations.Field
|
|
||||||
import kscience.kmath.operations.invoke
|
|
||||||
import kscience.kmath.operations.sum
|
|
||||||
import kscience.kmath.structures.asBuffer
|
import kscience.kmath.structures.asBuffer
|
||||||
import kotlin.contracts.InvocationKind
|
import kotlin.contracts.InvocationKind
|
||||||
import kotlin.contracts.contract
|
import kotlin.contracts.contract
|
||||||
@ -17,23 +14,37 @@ import kotlin.contracts.contract
|
|||||||
/**
|
/**
|
||||||
* Differentiable variable with value and derivative of differentiation ([deriv]) result
|
* Differentiable variable with value and derivative of differentiation ([deriv]) result
|
||||||
* with respect to this variable.
|
* with respect to this variable.
|
||||||
|
*
|
||||||
|
* @param T the non-nullable type of value.
|
||||||
|
* @property value The value of this variable.
|
||||||
*/
|
*/
|
||||||
public open class Variable<T : Any>(public val value: T)
|
public open class Variable<T : Any>(public val value: T)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents result of [deriv] call.
|
||||||
|
*
|
||||||
|
* @param T the non-nullable type of value.
|
||||||
|
* @param value the value of result.
|
||||||
|
* @property deriv The mapping of differentiated variables to their derivatives.
|
||||||
|
* @property context The field over [T].
|
||||||
|
*/
|
||||||
public class DerivationResult<T : Any>(
|
public class DerivationResult<T : Any>(
|
||||||
value: T,
|
value: T,
|
||||||
public val deriv: Map<Variable<T>, T>,
|
public val deriv: Map<Variable<T>, T>,
|
||||||
public val context: Field<T>
|
public val context: Field<T>
|
||||||
) : Variable<T>(value) {
|
) : Variable<T>(value) {
|
||||||
|
/**
|
||||||
|
* Returns derivative of [variable] or returns [Ring.zero] in [context].
|
||||||
|
*/
|
||||||
public fun deriv(variable: Variable<T>): T = deriv[variable] ?: context.zero
|
public fun deriv(variable: Variable<T>): T = deriv[variable] ?: context.zero
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* compute divergence
|
* Computes the divergence.
|
||||||
*/
|
*/
|
||||||
public fun div(): T = context { sum(deriv.values) }
|
public fun div(): T = context { sum(deriv.values) }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute a gradient for variables in given order
|
* Computes the gradient for variables in given order.
|
||||||
*/
|
*/
|
||||||
public fun grad(vararg variables: Variable<T>): Point<T> {
|
public fun grad(vararg variables: Variable<T>): Point<T> {
|
||||||
check(variables.isNotEmpty()) { "Variable order is not provided for gradient construction" }
|
check(variables.isNotEmpty()) { "Variable order is not provided for gradient construction" }
|
||||||
@ -53,6 +64,9 @@ public class DerivationResult<T : Any>(
|
|||||||
* assertEquals(17.0, y.x) // the value of result (y)
|
* assertEquals(17.0, y.x) // the value of result (y)
|
||||||
* assertEquals(9.0, x.d) // dy/dx
|
* assertEquals(9.0, x.d) // dy/dx
|
||||||
* ```
|
* ```
|
||||||
|
*
|
||||||
|
* @param body the action in [AutoDiffField] context returning [Variable] to differentiate with respect to.
|
||||||
|
* @return the result of differentiation.
|
||||||
*/
|
*/
|
||||||
public inline fun <T : Any, F : Field<T>> F.deriv(body: AutoDiffField<T, F>.() -> Variable<T>): DerivationResult<T> {
|
public inline fun <T : Any, F : Field<T>> F.deriv(body: AutoDiffField<T, F>.() -> Variable<T>): DerivationResult<T> {
|
||||||
contract { callsInPlace(body, InvocationKind.EXACTLY_ONCE) }
|
contract { callsInPlace(body, InvocationKind.EXACTLY_ONCE) }
|
||||||
@ -65,12 +79,15 @@ public inline fun <T : Any, F : Field<T>> F.deriv(body: AutoDiffField<T, F>.() -
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents field in context of which functions can be derived.
|
||||||
|
*/
|
||||||
public abstract class AutoDiffField<T : Any, F : Field<T>> : Field<Variable<T>> {
|
public abstract class AutoDiffField<T : Any, F : Field<T>> : Field<Variable<T>> {
|
||||||
public abstract val context: F
|
public abstract val context: F
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A variable accessing inner state of derivatives.
|
* A variable accessing inner state of derivatives.
|
||||||
* Use this function in inner builders to avoid creating additional derivative bindings
|
* Use this value in inner builders to avoid creating additional derivative bindings.
|
||||||
*/
|
*/
|
||||||
public abstract var Variable<T>.d: T
|
public abstract var Variable<T>.d: T
|
||||||
|
|
||||||
@ -87,6 +104,9 @@ public abstract class AutoDiffField<T : Any, F : Field<T>> : Field<Variable<T>>
|
|||||||
*/
|
*/
|
||||||
public abstract fun <R> derive(value: R, block: F.(R) -> Unit): R
|
public abstract fun <R> derive(value: R, block: F.(R) -> Unit): R
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
public abstract fun variable(value: T): Variable<T>
|
public abstract fun variable(value: T): Variable<T>
|
||||||
|
|
||||||
public inline fun variable(block: F.() -> T): Variable<T> = variable(context.block())
|
public inline fun variable(block: F.() -> T): Variable<T> = variable(context.block())
|
||||||
|
@ -299,7 +299,7 @@ public class BigInt internal constructor(
|
|||||||
|
|
||||||
for (i in mag.indices) {
|
for (i in mag.indices) {
|
||||||
val cur: ULong = carry + mag[i].toULong() * x.toULong()
|
val cur: ULong = carry + mag[i].toULong() * x.toULong()
|
||||||
result[i] = (cur and BASE.toULong()).toUInt()
|
result[i] = (cur and BASE).toUInt()
|
||||||
carry = cur shr BASE_SIZE
|
carry = cur shr BASE_SIZE
|
||||||
}
|
}
|
||||||
result[resultLength - 1] = (carry and BASE).toUInt()
|
result[resultLength - 1] = (carry and BASE).toUInt()
|
||||||
@ -316,7 +316,7 @@ public class BigInt internal constructor(
|
|||||||
|
|
||||||
for (j in mag2.indices) {
|
for (j in mag2.indices) {
|
||||||
val cur: ULong = result[i + j].toULong() + mag1[i].toULong() * mag2[j].toULong() + carry
|
val cur: ULong = result[i + j].toULong() + mag1[i].toULong() * mag2[j].toULong() + carry
|
||||||
result[i + j] = (cur and BASE.toULong()).toUInt()
|
result[i + j] = (cur and BASE).toUInt()
|
||||||
carry = cur shr BASE_SIZE
|
carry = cur shr BASE_SIZE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,7 +159,7 @@ public object ComplexField : ExtendedField<Complex>, Norm<Complex, Complex> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents complex number.
|
* Represents `double`-based complex number.
|
||||||
*
|
*
|
||||||
* @property re The real part.
|
* @property re The real part.
|
||||||
* @property im The imaginary part.
|
* @property im The imaginary part.
|
||||||
|
@ -48,7 +48,8 @@ public fun FlaggedBuffer<*>.isMissing(index: Int): Boolean = hasFlag(index, Valu
|
|||||||
/**
|
/**
|
||||||
* A real buffer which supports flags for each value like NaN or Missing
|
* A real buffer which supports flags for each value like NaN or Missing
|
||||||
*/
|
*/
|
||||||
public class FlaggedRealBuffer(public val values: DoubleArray, public val flags: ByteArray) : FlaggedBuffer<Double?>, Buffer<Double?> {
|
public class FlaggedRealBuffer(public val values: DoubleArray, public val flags: ByteArray) : FlaggedBuffer<Double?>,
|
||||||
|
Buffer<Double?> {
|
||||||
init {
|
init {
|
||||||
require(values.size == flags.size) { "Values and flags must have the same dimensions" }
|
require(values.size == flags.size) { "Values and flags must have the same dimensions" }
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ public interface Structure1D<T> : NDStructure<T>, Buffer<T> {
|
|||||||
/**
|
/**
|
||||||
* A 1D wrapper for nd-structure
|
* A 1D wrapper for nd-structure
|
||||||
*/
|
*/
|
||||||
private inline class Structure1DWrapper<T>(public val structure: NDStructure<T>) : Structure1D<T> {
|
private inline class Structure1DWrapper<T>(val structure: NDStructure<T>) : Structure1D<T> {
|
||||||
override val shape: IntArray get() = structure.shape
|
override val shape: IntArray get() = structure.shape
|
||||||
override val size: Int get() = structure.shape[0]
|
override val size: Int get() = structure.shape[0]
|
||||||
|
|
||||||
|
@ -21,7 +21,8 @@ internal class LazyDeferred<T>(val dispatcher: CoroutineDispatcher, val block: s
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class AsyncFlow<T> internal constructor(internal val deferredFlow: Flow<LazyDeferred<T>>) : Flow<T> {
|
public class AsyncFlow<T> internal constructor(internal val deferredFlow: Flow<LazyDeferred<T>>) : Flow<T> {
|
||||||
override suspend fun collect(collector: FlowCollector<T>): Unit = deferredFlow.collect { collector.emit((it.await())) }
|
override suspend fun collect(collector: FlowCollector<T>): Unit =
|
||||||
|
deferredFlow.collect { collector.emit((it.await())) }
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun <T, R> Flow<T>.async(
|
public fun <T, R> Flow<T>.async(
|
||||||
|
@ -4,7 +4,7 @@ import kscience.kmath.operations.Space
|
|||||||
|
|
||||||
public interface Vector
|
public interface Vector
|
||||||
|
|
||||||
public interface GeometrySpace<V: Vector>: Space<V> {
|
public interface GeometrySpace<V : Vector> : Space<V> {
|
||||||
/**
|
/**
|
||||||
* L2 distance
|
* L2 distance
|
||||||
*/
|
*/
|
||||||
|
@ -5,10 +5,7 @@ import kscience.kmath.histogram.fill
|
|||||||
import kscience.kmath.histogram.put
|
import kscience.kmath.histogram.put
|
||||||
import kscience.kmath.real.RealVector
|
import kscience.kmath.real.RealVector
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
import kotlin.test.Test
|
import kotlin.test.*
|
||||||
import kotlin.test.assertEquals
|
|
||||||
import kotlin.test.assertFalse
|
|
||||||
import kotlin.test.assertTrue
|
|
||||||
|
|
||||||
internal class MultivariateHistogramTest {
|
internal class MultivariateHistogramTest {
|
||||||
@Test
|
@Test
|
||||||
@ -18,7 +15,7 @@ internal class MultivariateHistogramTest {
|
|||||||
(-1.0..1.0)
|
(-1.0..1.0)
|
||||||
)
|
)
|
||||||
histogram.put(0.55, 0.55)
|
histogram.put(0.55, 0.55)
|
||||||
val bin = histogram.find { it.value.toInt() > 0 }!!
|
val bin = histogram.find { it.value.toInt() > 0 } ?: fail()
|
||||||
assertTrue { bin.contains(RealVector(0.55, 0.55)) }
|
assertTrue { bin.contains(RealVector(0.55, 0.55)) }
|
||||||
assertTrue { bin.contains(RealVector(0.6, 0.5)) }
|
assertTrue { bin.contains(RealVector(0.6, 0.5)) }
|
||||||
assertFalse { bin.contains(RealVector(-0.55, 0.55)) }
|
assertFalse { bin.contains(RealVector(-0.55, 0.55)) }
|
||||||
|
@ -32,7 +32,8 @@ public fun <T : Any> MemoryReader.read(spec: MemorySpec<T>, offset: Int): T = wi
|
|||||||
/**
|
/**
|
||||||
* Writes the object [value] with [spec] starting from [offset].
|
* Writes the object [value] with [spec] starting from [offset].
|
||||||
*/
|
*/
|
||||||
public fun <T : Any> MemoryWriter.write(spec: MemorySpec<T>, offset: Int, value: T): Unit = with(spec) { write(offset, value) }
|
public fun <T : Any> MemoryWriter.write(spec: MemorySpec<T>, offset: Int, value: T): Unit =
|
||||||
|
with(spec) { write(offset, value) }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads array of [size] objects mapped by [spec] at certain [offset].
|
* Reads array of [size] objects mapped by [spec] at certain [offset].
|
@ -100,7 +100,8 @@ public actual fun Memory.Companion.allocate(length: Int): Memory =
|
|||||||
* Wraps a [Memory] around existing [ByteArray]. This operation is unsafe since the array is not copied
|
* Wraps a [Memory] around existing [ByteArray]. This operation is unsafe since the array is not copied
|
||||||
* and could be mutated independently from the resulting [Memory].
|
* and could be mutated independently from the resulting [Memory].
|
||||||
*/
|
*/
|
||||||
public actual fun Memory.Companion.wrap(array: ByteArray): Memory = ByteBufferMemory(checkNotNull(ByteBuffer.wrap(array)))
|
public actual fun Memory.Companion.wrap(array: ByteArray): Memory =
|
||||||
|
ByteBufferMemory(checkNotNull(ByteBuffer.wrap(array)))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wraps this [ByteBuffer] to [Memory] object.
|
* Wraps this [ByteBuffer] to [Memory] object.
|
@ -62,7 +62,7 @@ public fun <T : Any> Sampler<T>.sampleBuffer(
|
|||||||
//clear list from previous run
|
//clear list from previous run
|
||||||
tmp.clear()
|
tmp.clear()
|
||||||
//Fill list
|
//Fill list
|
||||||
repeat(size){
|
repeat(size) {
|
||||||
tmp.add(chain.next())
|
tmp.add(chain.next())
|
||||||
}
|
}
|
||||||
//return new buffer with elements from tmp
|
//return new buffer with elements from tmp
|
||||||
|
@ -6,7 +6,7 @@ import kotlin.test.Test
|
|||||||
class SamplerTest {
|
class SamplerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun bufferSamplerTest(){
|
fun bufferSamplerTest() {
|
||||||
val sampler: Sampler<Double> =
|
val sampler: Sampler<Double> =
|
||||||
BasicSampler { it.chain { nextDouble() } }
|
BasicSampler { it.chain { nextDouble() } }
|
||||||
val data = sampler.sampleBuffer(RandomGenerator.default, 100)
|
val data = sampler.sampleBuffer(RandomGenerator.default, 100)
|
||||||
|
Loading…
Reference in New Issue
Block a user