From 8ae9a071ef362f630ead28b30fce80680238955b Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Wed, 9 Sep 2020 21:12:18 +0700 Subject: [PATCH] Add more explicit public modifiers --- .../commons/expressions/DiffExpression.kt | 78 +++++++++---------- .../kmath/commons/linear/CMMatrix.kt | 68 ++++++++-------- .../kmath/commons/linear/CMSolver.kt | 33 ++++---- .../random/CMRandomGeneratorWrapper.kt | 24 +++--- .../commons/transform/Transformations.kt | 27 +++---- .../kmath/commons/expressions/AutoDiffTest.kt | 10 ++- .../scientifik/kmath/operations/BigInt.kt | 30 +++---- .../kotlin/scientifik/kmath/chains/Chain.kt | 15 ++-- .../scientifik/kmath/streaming/BufferFlow.kt | 7 +- .../scientifik/kmath/chains/ChainExt.kt | 5 +- .../kmath/structures/LazyNDStructure.kt | 4 +- .../kmath/streaming/BufferFlowTest.kt | 2 +- .../kmath/streaming/RingBufferTest.kt | 2 +- .../dimensions/DMatrixContextTest.kt | 4 +- .../kotlin/scientifik/kmath/dimensions/dim.kt | 24 +++--- .../kotlin/scientifik/kmath/dimensions/dim.kt | 20 +++-- .../scientifik/kmath/real/RealVector.kt | 14 ++-- .../scientific.kmath.real/RealMatrixTest.kt | 2 +- .../scientifik/kmath/linear/VectorTest.kt | 2 +- .../kmath/geometry/Euclidean2DSpace.kt | 47 +++++------ .../kmath/geometry/Euclidean3DSpace.kt | 41 +++++----- .../kmath/geometry/GeometrySpace.kt | 8 +- .../kotlin/scientifik/kmath/geometry/Line.kt | 6 +- .../kmath/geometry/ReferenceFrame.kt | 3 +- .../scientifik.kmath.linear/KomaMatrix.kt | 53 ++++++------- kmath-prob/build.gradle.kts | 4 + .../kmath/prob/CommonsDistributionsTest.kt | 5 +- .../scientifik/kmath/prob/StatisticTest.kt | 9 ++- .../kmath/viktor/ViktorNDStructure.kt | 70 +++++++++-------- 29 files changed, 298 insertions(+), 319 deletions(-) diff --git a/kmath-commons/src/main/kotlin/scientifik/kmath/commons/expressions/DiffExpression.kt b/kmath-commons/src/main/kotlin/scientifik/kmath/commons/expressions/DiffExpression.kt index a6f905471..d556eb762 100644 --- a/kmath-commons/src/main/kotlin/scientifik/kmath/commons/expressions/DiffExpression.kt +++ b/kmath-commons/src/main/kotlin/scientifik/kmath/commons/expressions/DiffExpression.kt @@ -15,8 +15,8 @@ public class DerivativeStructureField( public val order: Int, public val parameters: Map ) : ExtendedField { - override val zero: DerivativeStructure by lazy { DerivativeStructure(order, parameters.size) } - override val one: DerivativeStructure by lazy { DerivativeStructure(order, parameters.size, 1.0) } + public override val zero: DerivativeStructure by lazy { DerivativeStructure(order, parameters.size) } + public override val one: DerivativeStructure by lazy { DerivativeStructure(order, parameters.size, 1.0) } private val variables: Map = parameters.mapValues { (key, value) -> DerivativeStructure(parameters.size, order, parameters.keys.indexOf(key), value) @@ -40,46 +40,43 @@ public class DerivativeStructureField( } public fun DerivativeStructure.deriv(vararg orders: Pair): Double = deriv(mapOf(*orders)) + public override fun add(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.add(b) - override fun add(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.add(b) - - override fun multiply(a: DerivativeStructure, k: Number): DerivativeStructure = when (k) { + public override fun multiply(a: DerivativeStructure, k: Number): DerivativeStructure = when (k) { is Double -> a.multiply(k) is Int -> a.multiply(k) else -> a.multiply(k.toDouble()) } - override fun multiply(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.multiply(b) + public override fun multiply(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.multiply(b) + public override fun divide(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.divide(b) + public override fun sin(arg: DerivativeStructure): DerivativeStructure = arg.sin() + public override fun cos(arg: DerivativeStructure): DerivativeStructure = arg.cos() + public override fun tan(arg: DerivativeStructure): DerivativeStructure = arg.tan() + public override fun asin(arg: DerivativeStructure): DerivativeStructure = arg.asin() + public override fun acos(arg: DerivativeStructure): DerivativeStructure = arg.acos() + public override fun atan(arg: DerivativeStructure): DerivativeStructure = arg.atan() + public override fun sinh(arg: DerivativeStructure): DerivativeStructure = arg.sinh() + public override fun cosh(arg: DerivativeStructure): DerivativeStructure = arg.cosh() + public override fun tanh(arg: DerivativeStructure): DerivativeStructure = arg.tanh() + public override fun asinh(arg: DerivativeStructure): DerivativeStructure = arg.asinh() + public override fun acosh(arg: DerivativeStructure): DerivativeStructure = arg.acosh() + public override fun atanh(arg: DerivativeStructure): DerivativeStructure = arg.atanh() - override fun divide(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.divide(b) - - override fun sin(arg: DerivativeStructure): DerivativeStructure = arg.sin() - override fun cos(arg: DerivativeStructure): DerivativeStructure = arg.cos() - override fun tan(arg: DerivativeStructure): DerivativeStructure = arg.tan() - override fun asin(arg: DerivativeStructure): DerivativeStructure = arg.asin() - override fun acos(arg: DerivativeStructure): DerivativeStructure = arg.acos() - override fun atan(arg: DerivativeStructure): DerivativeStructure = arg.atan() - override fun sinh(arg: DerivativeStructure): DerivativeStructure = arg.sinh() - override fun cosh(arg: DerivativeStructure): DerivativeStructure = arg.cosh() - override fun tanh(arg: DerivativeStructure): DerivativeStructure = arg.tanh() - override fun asinh(arg: DerivativeStructure): DerivativeStructure = arg.asinh() - override fun acosh(arg: DerivativeStructure): DerivativeStructure = arg.acosh() - override fun atanh(arg: DerivativeStructure): DerivativeStructure = arg.atanh() - - override fun power(arg: DerivativeStructure, pow: Number): DerivativeStructure = when (pow) { + public override fun power(arg: DerivativeStructure, pow: Number): DerivativeStructure = when (pow) { is Double -> arg.pow(pow) is Int -> arg.pow(pow) else -> arg.pow(pow.toDouble()) } public fun power(arg: DerivativeStructure, pow: DerivativeStructure): DerivativeStructure = arg.pow(pow) - override fun exp(arg: DerivativeStructure): DerivativeStructure = arg.exp() - override fun ln(arg: DerivativeStructure): DerivativeStructure = arg.log() + public override fun exp(arg: DerivativeStructure): DerivativeStructure = arg.exp() + public override fun ln(arg: DerivativeStructure): DerivativeStructure = arg.log() - override operator fun DerivativeStructure.plus(b: Number): DerivativeStructure = add(b.toDouble()) - override operator fun DerivativeStructure.minus(b: Number): DerivativeStructure = subtract(b.toDouble()) - override operator fun Number.plus(b: DerivativeStructure): DerivativeStructure = b + this - override operator fun Number.minus(b: DerivativeStructure): DerivativeStructure = b - this + public override operator fun DerivativeStructure.plus(b: Number): DerivativeStructure = add(b.toDouble()) + public override operator fun DerivativeStructure.minus(b: Number): DerivativeStructure = subtract(b.toDouble()) + public override operator fun Number.plus(b: DerivativeStructure): DerivativeStructure = b + this + public override operator fun Number.minus(b: DerivativeStructure): DerivativeStructure = b - this } /** @@ -87,10 +84,10 @@ public class DerivativeStructureField( */ public class DiffExpression(public val function: DerivativeStructureField.() -> DerivativeStructure) : Expression { - override operator fun invoke(arguments: Map): Double = DerivativeStructureField( + public override operator fun invoke(arguments: Map): Double = DerivativeStructureField( 0, arguments - ).run(function).value + ).function().value /** * Get the derivative expression with given orders @@ -110,25 +107,22 @@ public fun DiffExpression.derivative(name: String): Expression = derivat * A context for [DiffExpression] (not to be confused with [DerivativeStructure]) */ public object DiffExpressionAlgebra : ExpressionAlgebra, Field { - override fun variable(name: String, default: Double?): DiffExpression = + public override val zero: DiffExpression = DiffExpression { 0.0.const() } + public override val one: DiffExpression = DiffExpression { 1.0.const() } + + public override fun variable(name: String, default: Double?): DiffExpression = DiffExpression { variable(name, default?.const()) } - override fun const(value: Double): DiffExpression = - DiffExpression { value.const() } + public override fun const(value: Double): DiffExpression = DiffExpression { value.const() } - override fun add(a: DiffExpression, b: DiffExpression): DiffExpression = + public override fun add(a: DiffExpression, b: DiffExpression): DiffExpression = DiffExpression { a.function(this) + b.function(this) } - override val zero: DiffExpression = DiffExpression { 0.0.const() } + public override fun multiply(a: DiffExpression, k: Number): DiffExpression = DiffExpression { a.function(this) * k } - override fun multiply(a: DiffExpression, k: Number): DiffExpression = - DiffExpression { a.function(this) * k } - - override val one: DiffExpression = DiffExpression { 1.0.const() } - - override fun multiply(a: DiffExpression, b: DiffExpression): DiffExpression = + public override fun multiply(a: DiffExpression, b: DiffExpression): DiffExpression = DiffExpression { a.function(this) * b.function(this) } - override fun divide(a: DiffExpression, b: DiffExpression): DiffExpression = + public override fun divide(a: DiffExpression, b: DiffExpression): DiffExpression = DiffExpression { a.function(this) / b.function(this) } } diff --git a/kmath-commons/src/main/kotlin/scientifik/kmath/commons/linear/CMMatrix.kt b/kmath-commons/src/main/kotlin/scientifik/kmath/commons/linear/CMMatrix.kt index f0bbdbe65..72288a85e 100644 --- a/kmath-commons/src/main/kotlin/scientifik/kmath/commons/linear/CMMatrix.kt +++ b/kmath-commons/src/main/kotlin/scientifik/kmath/commons/linear/CMMatrix.kt @@ -5,32 +5,32 @@ import scientifik.kmath.linear.* import scientifik.kmath.structures.Matrix import scientifik.kmath.structures.NDStructure -class CMMatrix(val origin: RealMatrix, features: Set? = null) : +public class CMMatrix(public val origin: RealMatrix, features: Set? = null) : FeaturedMatrix { - override val rowNum: Int get() = origin.rowDimension - override val colNum: Int get() = origin.columnDimension + public override val rowNum: Int get() = origin.rowDimension + public override val colNum: Int get() = origin.columnDimension - override val features: Set = features ?: sequence { + public override val features: Set = features ?: sequence { if (origin is DiagonalMatrix) yield(DiagonalFeature) }.toHashSet() - override fun suggestFeature(vararg features: MatrixFeature): CMMatrix = + public override fun suggestFeature(vararg features: MatrixFeature): CMMatrix = CMMatrix(origin, this.features + features) - override operator fun get(i: Int, j: Int): Double = origin.getEntry(i, j) + public override operator fun get(i: Int, j: Int): Double = origin.getEntry(i, j) - override fun equals(other: Any?): Boolean { + public override fun equals(other: Any?): Boolean { return NDStructure.equals(this, other as? NDStructure<*> ?: return false) } - override fun hashCode(): Int { + public override fun hashCode(): Int { var result = origin.hashCode() result = 31 * result + features.hashCode() return result } } -fun Matrix.toCM(): CMMatrix = if (this is CMMatrix) { +public fun Matrix.toCM(): CMMatrix = if (this is CMMatrix) { this } else { //TODO add feature analysis @@ -38,56 +38,56 @@ fun Matrix.toCM(): CMMatrix = if (this is CMMatrix) { CMMatrix(Array2DRowRealMatrix(array)) } -fun RealMatrix.asMatrix(): CMMatrix = CMMatrix(this) +public fun RealMatrix.asMatrix(): CMMatrix = CMMatrix(this) -class CMVector(val origin: RealVector) : Point { - override val size: Int get() = origin.dimension +public class CMVector(public val origin: RealVector) : Point { + public override val size: Int get() = origin.dimension - override operator fun get(index: Int): Double = origin.getEntry(index) + public override operator fun get(index: Int): Double = origin.getEntry(index) - override operator fun iterator(): Iterator = origin.toArray().iterator() + public override operator fun iterator(): Iterator = origin.toArray().iterator() } -fun Point.toCM(): CMVector = if (this is CMVector) this else { +public fun Point.toCM(): CMVector = if (this is CMVector) this else { val array = DoubleArray(size) { this[it] } CMVector(ArrayRealVector(array)) } -fun RealVector.toPoint(): CMVector = CMVector(this) +public fun RealVector.toPoint(): CMVector = CMVector(this) -object CMMatrixContext : MatrixContext { - override fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> Double): CMMatrix { +public object CMMatrixContext : MatrixContext { + public override fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> Double): CMMatrix { val array = Array(rows) { i -> DoubleArray(columns) { j -> initializer(i, j) } } return CMMatrix(Array2DRowRealMatrix(array)) } - override fun Matrix.dot(other: Matrix): CMMatrix = - CMMatrix(this.toCM().origin.multiply(other.toCM().origin)) + public override fun Matrix.dot(other: Matrix): CMMatrix = + CMMatrix(toCM().origin.multiply(other.toCM().origin)) - override fun Matrix.dot(vector: Point): CMVector = - CMVector(this.toCM().origin.preMultiply(vector.toCM().origin)) + public override fun Matrix.dot(vector: Point): CMVector = + CMVector(toCM().origin.preMultiply(vector.toCM().origin)) - override operator fun Matrix.unaryMinus(): CMMatrix = + public override operator fun Matrix.unaryMinus(): CMMatrix = produce(rowNum, colNum) { i, j -> -get(i, j) } - override fun add(a: Matrix, b: Matrix): CMMatrix = + public override fun add(a: Matrix, b: Matrix): CMMatrix = CMMatrix(a.toCM().origin.multiply(b.toCM().origin)) - override operator fun Matrix.minus(b: Matrix): CMMatrix = - CMMatrix(this.toCM().origin.subtract(b.toCM().origin)) + public override operator fun Matrix.minus(b: Matrix): CMMatrix = + CMMatrix(toCM().origin.subtract(b.toCM().origin)) - override fun multiply(a: Matrix, k: Number): CMMatrix = + public override fun multiply(a: Matrix, k: Number): CMMatrix = CMMatrix(a.toCM().origin.scalarMultiply(k.toDouble())) - override operator fun Matrix.times(value: Double): Matrix = + public override operator fun Matrix.times(value: Double): Matrix = produce(rowNum, colNum) { i, j -> get(i, j) * value } } -operator fun CMMatrix.plus(other: CMMatrix): CMMatrix = - CMMatrix(this.origin.add(other.origin)) +public operator fun CMMatrix.plus(other: CMMatrix): CMMatrix = + CMMatrix(origin.add(other.origin)) -operator fun CMMatrix.minus(other: CMMatrix): CMMatrix = - CMMatrix(this.origin.subtract(other.origin)) +public operator fun CMMatrix.minus(other: CMMatrix): CMMatrix = + CMMatrix(origin.subtract(other.origin)) -infix fun CMMatrix.dot(other: CMMatrix): CMMatrix = - CMMatrix(this.origin.multiply(other.origin)) +public infix fun CMMatrix.dot(other: CMMatrix): CMMatrix = + CMMatrix(origin.multiply(other.origin)) diff --git a/kmath-commons/src/main/kotlin/scientifik/kmath/commons/linear/CMSolver.kt b/kmath-commons/src/main/kotlin/scientifik/kmath/commons/linear/CMSolver.kt index 77b688e31..1e79aff21 100644 --- a/kmath-commons/src/main/kotlin/scientifik/kmath/commons/linear/CMSolver.kt +++ b/kmath-commons/src/main/kotlin/scientifik/kmath/commons/linear/CMSolver.kt @@ -4,7 +4,7 @@ import org.apache.commons.math3.linear.* import scientifik.kmath.linear.Point import scientifik.kmath.structures.Matrix -enum class CMDecomposition { +public enum class CMDecomposition { LUP, QR, RRQR, @@ -12,29 +12,30 @@ enum class CMDecomposition { CHOLESKY } +public fun CMMatrixContext.solver( + a: Matrix, + decomposition: CMDecomposition = CMDecomposition.LUP +): DecompositionSolver = when (decomposition) { + CMDecomposition.LUP -> LUDecomposition(a.toCM().origin).solver + CMDecomposition.RRQR -> RRQRDecomposition(a.toCM().origin).solver + CMDecomposition.QR -> QRDecomposition(a.toCM().origin).solver + CMDecomposition.EIGEN -> EigenDecomposition(a.toCM().origin).solver + CMDecomposition.CHOLESKY -> CholeskyDecomposition(a.toCM().origin).solver +} -fun CMMatrixContext.solver(a: Matrix, decomposition: CMDecomposition = CMDecomposition.LUP) = - when (decomposition) { - CMDecomposition.LUP -> LUDecomposition(a.toCM().origin).solver - CMDecomposition.RRQR -> RRQRDecomposition(a.toCM().origin).solver - CMDecomposition.QR -> QRDecomposition(a.toCM().origin).solver - CMDecomposition.EIGEN -> EigenDecomposition(a.toCM().origin).solver - CMDecomposition.CHOLESKY -> CholeskyDecomposition(a.toCM().origin).solver - } - -fun CMMatrixContext.solve( +public fun CMMatrixContext.solve( a: Matrix, b: Matrix, decomposition: CMDecomposition = CMDecomposition.LUP -) = solver(a, decomposition).solve(b.toCM().origin).asMatrix() +): CMMatrix = solver(a, decomposition).solve(b.toCM().origin).asMatrix() -fun CMMatrixContext.solve( +public fun CMMatrixContext.solve( a: Matrix, b: Point, decomposition: CMDecomposition = CMDecomposition.LUP -) = solver(a, decomposition).solve(b.toCM().origin).toPoint() +): CMVector = solver(a, decomposition).solve(b.toCM().origin).toPoint() -fun CMMatrixContext.inverse( +public fun CMMatrixContext.inverse( a: Matrix, decomposition: CMDecomposition = CMDecomposition.LUP -) = solver(a, decomposition).inverse.asMatrix() +): CMMatrix = solver(a, decomposition).inverse.asMatrix() diff --git a/kmath-commons/src/main/kotlin/scientifik/kmath/commons/random/CMRandomGeneratorWrapper.kt b/kmath-commons/src/main/kotlin/scientifik/kmath/commons/random/CMRandomGeneratorWrapper.kt index cb2b5dd9c..3e60d7cdd 100644 --- a/kmath-commons/src/main/kotlin/scientifik/kmath/commons/random/CMRandomGeneratorWrapper.kt +++ b/kmath-commons/src/main/kotlin/scientifik/kmath/commons/random/CMRandomGeneratorWrapper.kt @@ -2,32 +2,32 @@ package scientifik.kmath.commons.random import scientifik.kmath.prob.RandomGenerator -class CMRandomGeneratorWrapper(val factory: (IntArray) -> RandomGenerator) : +public class CMRandomGeneratorWrapper(public val factory: (IntArray) -> RandomGenerator) : org.apache.commons.math3.random.RandomGenerator { private var generator: RandomGenerator = factory(intArrayOf()) - override fun nextBoolean(): Boolean = generator.nextBoolean() - override fun nextFloat(): Float = generator.nextDouble().toFloat() + public override fun nextBoolean(): Boolean = generator.nextBoolean() + public override fun nextFloat(): Float = generator.nextDouble().toFloat() - override fun setSeed(seed: Int) { + public override fun setSeed(seed: Int) { generator = factory(intArrayOf(seed)) } - override fun setSeed(seed: IntArray) { + public override fun setSeed(seed: IntArray) { generator = factory(seed) } - override fun setSeed(seed: Long) { + public override fun setSeed(seed: Long) { setSeed(seed.toInt()) } - override fun nextBytes(bytes: ByteArray) { + public override fun nextBytes(bytes: ByteArray) { generator.fillBytes(bytes) } - override fun nextInt(): Int = generator.nextInt() - override fun nextInt(n: Int): Int = generator.nextInt(n) - override fun nextGaussian(): Double = TODO() - override fun nextDouble(): Double = generator.nextDouble() - override fun nextLong(): Long = generator.nextLong() + public override fun nextInt(): Int = generator.nextInt() + public override fun nextInt(n: Int): Int = generator.nextInt(n) + public override fun nextGaussian(): Double = TODO() + public override fun nextDouble(): Double = generator.nextDouble() + public override fun nextLong(): Long = generator.nextLong() } diff --git a/kmath-commons/src/main/kotlin/scientifik/kmath/commons/transform/Transformations.kt b/kmath-commons/src/main/kotlin/scientifik/kmath/commons/transform/Transformations.kt index eb1b5b69a..4af0d2b72 100644 --- a/kmath-commons/src/main/kotlin/scientifik/kmath/commons/transform/Transformations.kt +++ b/kmath-commons/src/main/kotlin/scientifik/kmath/commons/transform/Transformations.kt @@ -13,8 +13,7 @@ import scientifik.kmath.structures.* /** * Streaming and buffer transformations */ -object Transformations { - +public object Transformations { private fun Buffer.toArray(): Array = Array(size) { org.apache.commons.math3.complex.Complex(get(it).re, get(it).im) } @@ -32,35 +31,35 @@ object Transformations { Complex(value.real, value.imaginary) } - fun fourier( + public fun fourier( normalization: DftNormalization = DftNormalization.STANDARD, direction: TransformType = TransformType.FORWARD ): SuspendBufferTransform = { FastFourierTransformer(normalization).transform(it.toArray(), direction).asBuffer() } - fun realFourier( + public fun realFourier( normalization: DftNormalization = DftNormalization.STANDARD, direction: TransformType = TransformType.FORWARD ): SuspendBufferTransform = { FastFourierTransformer(normalization).transform(it.asArray(), direction).asBuffer() } - fun sine( + public fun sine( normalization: DstNormalization = DstNormalization.STANDARD_DST_I, direction: TransformType = TransformType.FORWARD ): SuspendBufferTransform = { FastSineTransformer(normalization).transform(it.asArray(), direction).asBuffer() } - fun cosine( + public fun cosine( normalization: DctNormalization = DctNormalization.STANDARD_DCT_I, direction: TransformType = TransformType.FORWARD ): SuspendBufferTransform = { FastCosineTransformer(normalization).transform(it.asArray(), direction).asBuffer() } - fun hadamard( + public fun hadamard( direction: TransformType = TransformType.FORWARD ): SuspendBufferTransform = { FastHadamardTransformer().transform(it.asArray(), direction).asBuffer() @@ -71,7 +70,7 @@ object Transformations { * Process given [Flow] with commons-math fft transformation */ @FlowPreview -fun Flow>.FFT( +public fun Flow>.FFT( normalization: DftNormalization = DftNormalization.STANDARD, direction: TransformType = TransformType.FORWARD ): Flow> { @@ -81,7 +80,7 @@ fun Flow>.FFT( @FlowPreview @JvmName("realFFT") -fun Flow>.FFT( +public fun Flow>.FFT( normalization: DftNormalization = DftNormalization.STANDARD, direction: TransformType = TransformType.FORWARD ): Flow> { @@ -90,20 +89,18 @@ fun Flow>.FFT( } /** - * Process a continous flow of real numbers in FFT splitting it in chunks of [bufferSize]. + * Process a continuous flow of real numbers in FFT splitting it in chunks of [bufferSize]. */ @FlowPreview @JvmName("realFFT") -fun Flow.FFT( +public fun Flow.FFT( bufferSize: Int = Int.MAX_VALUE, normalization: DftNormalization = DftNormalization.STANDARD, direction: TransformType = TransformType.FORWARD -): Flow { - return chunked(bufferSize).FFT(normalization,direction).spread() -} +): Flow = chunked(bufferSize).FFT(normalization, direction).spread() /** * Map a complex flow into real flow by taking real part of each number */ @FlowPreview -fun Flow.real(): Flow = map{it.re} \ No newline at end of file +public fun Flow.real(): Flow = map { it.re } diff --git a/kmath-commons/src/test/kotlin/scientifik/kmath/commons/expressions/AutoDiffTest.kt b/kmath-commons/src/test/kotlin/scientifik/kmath/commons/expressions/AutoDiffTest.kt index bbdcff2fc..3d8a1d5f0 100644 --- a/kmath-commons/src/test/kotlin/scientifik/kmath/commons/expressions/AutoDiffTest.kt +++ b/kmath-commons/src/test/kotlin/scientifik/kmath/commons/expressions/AutoDiffTest.kt @@ -6,12 +6,16 @@ import kotlin.contracts.contract import kotlin.test.Test import kotlin.test.assertEquals -inline fun diff(order: Int, vararg parameters: Pair, block: DerivativeStructureField.() -> R): R { +internal inline fun diff( + order: Int, + vararg parameters: Pair, + block: DerivativeStructureField.() -> R +): R { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return DerivativeStructureField(order, mapOf(*parameters)).run(block) } -class AutoDiffTest { +internal class AutoDiffTest { @Test fun derivativeStructureFieldTest() { val res = diff(3, "x" to 1.0, "y" to 1.0) { @@ -33,4 +37,4 @@ class AutoDiffTest { assertEquals(10.0, f("x" to 1.0, "y" to 2.0)) assertEquals(6.0, f.derivative("x")("x" to 1.0, "y" to 2.0)) } -} \ No newline at end of file +} diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/BigInt.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/BigInt.kt index cbee88e38..fbb60026e 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/BigInt.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/BigInt.kt @@ -41,10 +41,10 @@ public class BigInt internal constructor( private val magnitude: Magnitude ) : Comparable { public override fun compareTo(other: BigInt): Int = when { - (this.sign == 0.toByte()) and (other.sign == 0.toByte()) -> 0 - this.sign < other.sign -> -1 - this.sign > other.sign -> 1 - else -> this.sign * compareMagnitudes(this.magnitude, other.magnitude) + (sign == 0.toByte()) and (other.sign == 0.toByte()) -> 0 + sign < other.sign -> -1 + sign > other.sign -> 1 + else -> sign * compareMagnitudes(magnitude, other.magnitude) } public override fun equals(other: Any?): Boolean = @@ -123,7 +123,7 @@ public class BigInt internal constructor( return Pair(BigInt((this.sign * other.sign).toByte(), q.magnitude), r) } - public operator fun div(other: BigInt): BigInt = this.division(other).first + public operator fun div(other: BigInt): BigInt = division(other).first public infix fun shl(i: Int): BigInt { if (this == ZERO) return ZERO @@ -132,16 +132,16 @@ public class BigInt internal constructor( val relShift = i % BASE_SIZE val shiftLeft = { x: UInt -> if (relShift >= 32) 0U else x shl relShift } val shiftRight = { x: UInt -> if (BASE_SIZE - relShift >= 32) 0U else x shr (BASE_SIZE - relShift) } - val newMagnitude = Magnitude(this.magnitude.size + fullShifts) + val newMagnitude = Magnitude(magnitude.size + fullShifts) - for (j in this.magnitude.indices) { + for (j in magnitude.indices) { newMagnitude[j + fullShifts - 1] = shiftLeft(this.magnitude[j]) if (j != 0) newMagnitude[j + fullShifts - 1] = newMagnitude[j + fullShifts - 1] or shiftRight(this.magnitude[j - 1]) } - newMagnitude[this.magnitude.size + fullShifts - 1] = shiftRight(this.magnitude.last()) + newMagnitude[magnitude.size + fullShifts - 1] = shiftRight(magnitude.last()) return BigInt(this.sign, stripLeadingZeros(newMagnitude)) } @@ -153,13 +153,13 @@ public class BigInt internal constructor( val shiftRight = { x: UInt -> if (relShift >= 32) 0U else x shr relShift } val shiftLeft = { x: UInt -> if (BASE_SIZE - relShift >= 32) 0U else x shl (BASE_SIZE - relShift) } if (this.magnitude.size - fullShifts <= 0) return ZERO - val newMagnitude: Magnitude = Magnitude(this.magnitude.size - fullShifts) + val newMagnitude: Magnitude = Magnitude(magnitude.size - fullShifts) - for (j in fullShifts until this.magnitude.size) { - newMagnitude[j - fullShifts] = shiftRight(this.magnitude[j]) + for (j in fullShifts until magnitude.size) { + newMagnitude[j - fullShifts] = shiftRight(magnitude[j]) - if (j != this.magnitude.size - 1) - newMagnitude[j - fullShifts] = newMagnitude[j - fullShifts] or shiftLeft(this.magnitude[j + 1]) + if (j != magnitude.size - 1) + newMagnitude[j - fullShifts] = newMagnitude[j - fullShifts] or shiftLeft(magnitude[j + 1]) } return BigInt(this.sign, stripLeadingZeros(newMagnitude)) @@ -168,11 +168,11 @@ public class BigInt internal constructor( public infix fun or(other: BigInt): BigInt { if (this == ZERO) return other if (other == ZERO) return this - val resSize = max(this.magnitude.size, other.magnitude.size) + val resSize = max(magnitude.size, other.magnitude.size) val newMagnitude: Magnitude = Magnitude(resSize) for (i in 0 until resSize) { - if (i < this.magnitude.size) newMagnitude[i] = newMagnitude[i] or this.magnitude[i] + if (i < magnitude.size) newMagnitude[i] = newMagnitude[i] or magnitude[i] if (i < other.magnitude.size) newMagnitude[i] = newMagnitude[i] or other.magnitude[i] } diff --git a/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/chains/Chain.kt b/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/chains/Chain.kt index e899f8ece..bf79c1601 100644 --- a/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/chains/Chain.kt +++ b/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/chains/Chain.kt @@ -149,13 +149,16 @@ public fun Chain.collect(mapper: suspend (Chain) -> R): Chain = override fun fork(): Chain = this@collect.fork().collect(mapper) } -public fun Chain.collectWithState(state: S, stateFork: (S) -> S, mapper: suspend S.(Chain) -> R): Chain = - object : Chain { - override suspend fun next(): R = state.mapper(this@collectWithState) +public fun Chain.collectWithState( + state: S, + stateFork: (S) -> S, + mapper: suspend S.(Chain) -> R +): Chain = object : Chain { + override suspend fun next(): R = state.mapper(this@collectWithState) - override fun fork(): Chain = - this@collectWithState.fork().collectWithState(stateFork(state), stateFork, mapper) - } + override fun fork(): Chain = + this@collectWithState.fork().collectWithState(stateFork(state), stateFork, mapper) +} /** * Zip two chains together using given transformation diff --git a/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/streaming/BufferFlow.kt b/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/streaming/BufferFlow.kt index ae1860974..991642787 100644 --- a/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/streaming/BufferFlow.kt +++ b/kmath-coroutines/src/commonMain/kotlin/scientifik/kmath/streaming/BufferFlow.kt @@ -49,10 +49,8 @@ public fun Flow.chunked(bufferSize: Int): Flow = flow { require(bufferSize > 0) { "Resulting chunk size must be more than zero" } if (this@chunked is BlockingRealChain) { - //performance optimization for blocking primitive chain - while (true) { - emit(nextBlock(bufferSize).asBuffer()) - } + // performance optimization for blocking primitive chain + while (true) emit(nextBlock(bufferSize).asBuffer()) } else { val array = DoubleArray(bufferSize) var counter = 0 @@ -60,6 +58,7 @@ public fun Flow.chunked(bufferSize: Int): Flow = flow { this@chunked.collect { element -> array[counter] = element counter++ + if (counter == bufferSize) { val buffer = RealBuffer(array) emit(buffer) diff --git a/kmath-coroutines/src/jvmMain/kotlin/scientifik/kmath/chains/ChainExt.kt b/kmath-coroutines/src/jvmMain/kotlin/scientifik/kmath/chains/ChainExt.kt index 5686b0ac0..c0df59645 100644 --- a/kmath-coroutines/src/jvmMain/kotlin/scientifik/kmath/chains/ChainExt.kt +++ b/kmath-coroutines/src/jvmMain/kotlin/scientifik/kmath/chains/ChainExt.kt @@ -5,13 +5,12 @@ import kotlinx.coroutines.runBlocking /** * Represent a chain as regular iterator (uses blocking calls) */ -operator fun Chain.iterator(): Iterator = object : Iterator { +public operator fun Chain.iterator(): Iterator = object : Iterator { override fun hasNext(): Boolean = true - override fun next(): R = runBlocking { next() } } /** * Represent a chain as a sequence */ -fun Chain.asSequence(): Sequence = Sequence { this@asSequence.iterator() } \ No newline at end of file +public fun Chain.asSequence(): Sequence = Sequence { this@asSequence.iterator() } diff --git a/kmath-coroutines/src/jvmMain/kotlin/scientifik/kmath/structures/LazyNDStructure.kt b/kmath-coroutines/src/jvmMain/kotlin/scientifik/kmath/structures/LazyNDStructure.kt index a607d2d85..82950159b 100644 --- a/kmath-coroutines/src/jvmMain/kotlin/scientifik/kmath/structures/LazyNDStructure.kt +++ b/kmath-coroutines/src/jvmMain/kotlin/scientifik/kmath/structures/LazyNDStructure.kt @@ -37,10 +37,10 @@ public class LazyNDStructure( } public fun NDStructure.deferred(index: IntArray): Deferred = - if (this is LazyNDStructure) this.deferred(index) else CompletableDeferred(get(index)) + if (this is LazyNDStructure) deferred(index) else CompletableDeferred(get(index)) public suspend fun NDStructure.await(index: IntArray): T = - if (this is LazyNDStructure) this.await(index) else get(index) + if (this is LazyNDStructure) await(index) else get(index) /** * PENDING would benefit from KEEP-176 diff --git a/kmath-coroutines/src/jvmTest/kotlin/scientifik/kmath/streaming/BufferFlowTest.kt b/kmath-coroutines/src/jvmTest/kotlin/scientifik/kmath/streaming/BufferFlowTest.kt index 427349072..d17e4d86b 100644 --- a/kmath-coroutines/src/jvmTest/kotlin/scientifik/kmath/streaming/BufferFlowTest.kt +++ b/kmath-coroutines/src/jvmTest/kotlin/scientifik/kmath/streaming/BufferFlowTest.kt @@ -14,7 +14,7 @@ import kotlin.test.Test @ExperimentalCoroutinesApi @InternalCoroutinesApi @FlowPreview -class BufferFlowTest { +internal class BufferFlowTest { val dispatcher: CoroutineDispatcher = Executors.newFixedThreadPool(4).asCoroutineDispatcher() @Test diff --git a/kmath-coroutines/src/jvmTest/kotlin/scientifik/kmath/streaming/RingBufferTest.kt b/kmath-coroutines/src/jvmTest/kotlin/scientifik/kmath/streaming/RingBufferTest.kt index c84ef89ef..630749861 100644 --- a/kmath-coroutines/src/jvmTest/kotlin/scientifik/kmath/streaming/RingBufferTest.kt +++ b/kmath-coroutines/src/jvmTest/kotlin/scientifik/kmath/streaming/RingBufferTest.kt @@ -6,7 +6,7 @@ import scientifik.kmath.structures.asSequence import kotlin.test.Test import kotlin.test.assertEquals -class RingBufferTest { +internal class RingBufferTest { @Test fun push() { val buffer = RingBuffer.build(20, Double.NaN) diff --git a/kmath-dimensions/src/commonTest/kotlin/scientifik/dimensions/DMatrixContextTest.kt b/kmath-dimensions/src/commonTest/kotlin/scientifik/dimensions/DMatrixContextTest.kt index 8dabdeeac..dc7acdcc2 100644 --- a/kmath-dimensions/src/commonTest/kotlin/scientifik/dimensions/DMatrixContextTest.kt +++ b/kmath-dimensions/src/commonTest/kotlin/scientifik/dimensions/DMatrixContextTest.kt @@ -5,7 +5,7 @@ import scientifik.kmath.dimensions.D3 import scientifik.kmath.dimensions.DMatrixContext import kotlin.test.Test -class DMatrixContextTest { +internal class DMatrixContextTest { @Test fun testDimensionSafeMatrix() { val res = with(DMatrixContext.real) { @@ -26,4 +26,4 @@ class DMatrixContextTest { m1.transpose() + m2 } } -} \ No newline at end of file +} diff --git a/kmath-dimensions/src/jsMain/kotlin/scientifik/kmath/dimensions/dim.kt b/kmath-dimensions/src/jsMain/kotlin/scientifik/kmath/dimensions/dim.kt index e5cc451d1..139f77e92 100644 --- a/kmath-dimensions/src/jsMain/kotlin/scientifik/kmath/dimensions/dim.kt +++ b/kmath-dimensions/src/jsMain/kotlin/scientifik/kmath/dimensions/dim.kt @@ -2,21 +2,17 @@ package scientifik.kmath.dimensions import kotlin.reflect.KClass -private val dimensionMap = hashMapOf( - 1u to D1, - 2u to D2, - 3u to D3 -) +private val dimensionMap: MutableMap = hashMapOf(1u to D1, 2u to D2, 3u to D3) @Suppress("UNCHECKED_CAST") -public actual fun Dimension.Companion.resolve(type: KClass): D { - return dimensionMap.entries.find { it.value::class == type }?.value as? D ?: error("Can't resolve dimension $type") -} +public actual fun Dimension.Companion.resolve(type: KClass): D = dimensionMap + .entries + .map(MutableMap.MutableEntry::value) + .find { it::class == type } as? D + ?: error("Can't resolve dimension $type") -public actual fun Dimension.Companion.of(dim: UInt): Dimension { - return dimensionMap.getOrPut(dim) { - object : Dimension { - override val dim: UInt get() = dim - } +public actual fun Dimension.Companion.of(dim: UInt): Dimension = dimensionMap.getOrPut(dim) { + object : Dimension { + override val dim: UInt get() = dim } -} \ No newline at end of file +} diff --git a/kmath-dimensions/src/jvmMain/kotlin/scientifik/kmath/dimensions/dim.kt b/kmath-dimensions/src/jvmMain/kotlin/scientifik/kmath/dimensions/dim.kt index 42310cb3a..714133ecb 100644 --- a/kmath-dimensions/src/jvmMain/kotlin/scientifik/kmath/dimensions/dim.kt +++ b/kmath-dimensions/src/jvmMain/kotlin/scientifik/kmath/dimensions/dim.kt @@ -2,17 +2,15 @@ package scientifik.kmath.dimensions import kotlin.reflect.KClass -public actual fun Dimension.Companion.resolve(type: KClass): D{ - return type.objectInstance ?: error("No object instance for dimension class") -} +public actual fun Dimension.Companion.resolve(type: KClass): D = + type.objectInstance ?: error("No object instance for dimension class") -public actual fun Dimension.Companion.of(dim: UInt): Dimension{ - return when(dim){ - 1u -> D1 - 2u -> D2 - 3u -> D3 - else -> object : Dimension { - override val dim: UInt get() = dim - } +public actual fun Dimension.Companion.of(dim: UInt): Dimension = when (dim) { + 1u -> D1 + 2u -> D2 + 3u -> D3 + + else -> object : Dimension { + override val dim: UInt get() = dim } } \ No newline at end of file diff --git a/kmath-for-real/src/commonMain/kotlin/scientifik/kmath/real/RealVector.kt b/kmath-for-real/src/commonMain/kotlin/scientifik/kmath/real/RealVector.kt index 8c6d5fae4..474a701ca 100644 --- a/kmath-for-real/src/commonMain/kotlin/scientifik/kmath/real/RealVector.kt +++ b/kmath-for-real/src/commonMain/kotlin/scientifik/kmath/real/RealVector.kt @@ -14,11 +14,11 @@ import kotlin.math.sqrt public typealias RealPoint = Point -public fun DoubleArray.asVector(): RealVector = RealVector(this.asBuffer()) -public fun List.asVector(): RealVector = RealVector(this.asBuffer()) +public fun DoubleArray.asVector(): RealVector = RealVector(asBuffer()) +public fun List.asVector(): RealVector = RealVector(asBuffer()) public object VectorL2Norm : Norm, Double> { - override fun norm(arg: Point): Double = sqrt(arg.asIterable().sumByDouble { it.toDouble() }) + override fun norm(arg: Point): Double = sqrt(arg.asIterable().sumByDouble(Number::toDouble)) } public inline class RealVector(private val point: Point) : @@ -27,13 +27,9 @@ public inline class RealVector(private val point: Point) : public override val context: VectorSpace get() = space(point.size) public override fun unwrap(): RealPoint = point - public override fun RealPoint.wrap(): RealVector = RealVector(this) - - - override operator fun get(index: Int): Double = point[index] - - override operator fun iterator(): Iterator = point.iterator() + public override operator fun get(index: Int): Double = point[index] + public override operator fun iterator(): Iterator = point.iterator() public companion object { private val spaceCache: MutableMap> = hashMapOf() diff --git a/kmath-for-real/src/commonTest/kotlin/scientific.kmath.real/RealMatrixTest.kt b/kmath-for-real/src/commonTest/kotlin/scientific.kmath.real/RealMatrixTest.kt index 8918fb300..baf1db8b5 100644 --- a/kmath-for-real/src/commonTest/kotlin/scientific.kmath.real/RealMatrixTest.kt +++ b/kmath-for-real/src/commonTest/kotlin/scientific.kmath.real/RealMatrixTest.kt @@ -8,7 +8,7 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue -class RealMatrixTest { +internal class RealMatrixTest { @Test fun testSum() { val m = realMatrix(10, 10) { i, j -> (i + j).toDouble() } diff --git a/kmath-for-real/src/commonTest/kotlin/scientifik/kmath/linear/VectorTest.kt b/kmath-for-real/src/commonTest/kotlin/scientifik/kmath/linear/VectorTest.kt index ef7f40afe..e24037953 100644 --- a/kmath-for-real/src/commonTest/kotlin/scientifik/kmath/linear/VectorTest.kt +++ b/kmath-for-real/src/commonTest/kotlin/scientifik/kmath/linear/VectorTest.kt @@ -5,7 +5,7 @@ import scientifik.kmath.real.RealVector import kotlin.test.Test import kotlin.test.assertEquals -class VectorTest { +internal class VectorTest { @Test fun testSum() { val vector1 = RealVector(5) { it.toDouble() } diff --git a/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/Euclidean2DSpace.kt b/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/Euclidean2DSpace.kt index f0dc49882..a5318b709 100644 --- a/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/Euclidean2DSpace.kt +++ b/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/Euclidean2DSpace.kt @@ -5,28 +5,28 @@ import scientifik.kmath.operations.SpaceElement import scientifik.kmath.operations.invoke import kotlin.math.sqrt +public interface Vector2D : Point, Vector, SpaceElement { + public val x: Double + public val y: Double + public override val context: Euclidean2DSpace get() = Euclidean2DSpace + public override val size: Int get() = 2 -interface Vector2D : Point, Vector, SpaceElement { - val x: Double - val y: Double - override val context: Euclidean2DSpace get() = Euclidean2DSpace - override val size: Int get() = 2 - - override operator fun get(index: Int): Double = when (index) { + public override operator fun get(index: Int): Double = when (index) { 1 -> x 2 -> y else -> error("Accessing outside of point bounds") } - override operator fun iterator(): Iterator = listOf(x, y).iterator() - override fun unwrap(): Vector2D = this - override fun Vector2D.wrap(): Vector2D = this + public override operator fun iterator(): Iterator = listOf(x, y).iterator() + public override fun unwrap(): Vector2D = this + public override fun Vector2D.wrap(): Vector2D = this } -val Vector2D.r: Double get() = Euclidean2DSpace { sqrt(norm()) } +public val Vector2D.r: Double + get() = Euclidean2DSpace { sqrt(norm()) } @Suppress("FunctionName") -fun Vector2D(x: Double, y: Double): Vector2D = Vector2DImpl(x, y) +public fun Vector2D(x: Double, y: Double): Vector2D = Vector2DImpl(x, y) private data class Vector2DImpl( override val x: Double, @@ -36,19 +36,12 @@ private data class Vector2DImpl( /** * 2D Euclidean space */ -object Euclidean2DSpace : GeometrySpace { - fun Vector2D.norm(): Double = sqrt(x * x + y * y) +public object Euclidean2DSpace : GeometrySpace { + public override val zero: Vector2D by lazy { Vector2D(0.0, 0.0) } - override fun Vector2D.distanceTo(other: Vector2D): Double = (this - other).norm() - - override fun add(a: Vector2D, b: Vector2D): Vector2D = - Vector2D(a.x + b.x, a.y + b.y) - - override fun multiply(a: Vector2D, k: Number): Vector2D = - Vector2D(a.x * k.toDouble(), a.y * k.toDouble()) - - override val zero: Vector2D = Vector2D(0.0, 0.0) - - override fun Vector2D.dot(other: Vector2D): Double = - x * other.x + y * other.y -} \ No newline at end of file + public fun Vector2D.norm(): Double = sqrt(x * x + y * y) + public override fun Vector2D.distanceTo(other: Vector2D): Double = (this - other).norm() + public override fun add(a: Vector2D, b: Vector2D): Vector2D = Vector2D(a.x + b.x, a.y + b.y) + public override fun multiply(a: Vector2D, k: Number): Vector2D = Vector2D(a.x * k.toDouble(), a.y * k.toDouble()) + public override fun Vector2D.dot(other: Vector2D): Double = x * other.x + y * other.y +} diff --git a/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/Euclidean3DSpace.kt b/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/Euclidean3DSpace.kt index 3748e58c7..f695c0f64 100644 --- a/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/Euclidean3DSpace.kt +++ b/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/Euclidean3DSpace.kt @@ -5,32 +5,29 @@ import scientifik.kmath.operations.SpaceElement import scientifik.kmath.operations.invoke import kotlin.math.sqrt +public interface Vector3D : Point, Vector, SpaceElement { + public val x: Double + public val y: Double + public val z: Double + public override val context: Euclidean3DSpace get() = Euclidean3DSpace + public override val size: Int get() = 3 -interface Vector3D : Point, Vector, SpaceElement { - val x: Double - val y: Double - val z: Double - override val context: Euclidean3DSpace get() = Euclidean3DSpace - override val size: Int get() = 3 - - override operator fun get(index: Int): Double = when (index) { + public override operator fun get(index: Int): Double = when (index) { 1 -> x 2 -> y 3 -> z else -> error("Accessing outside of point bounds") } - override operator fun iterator(): Iterator = listOf(x, y, z).iterator() - - override fun unwrap(): Vector3D = this - - override fun Vector3D.wrap(): Vector3D = this + public override operator fun iterator(): Iterator = listOf(x, y, z).iterator() + public override fun unwrap(): Vector3D = this + public override fun Vector3D.wrap(): Vector3D = this } @Suppress("FunctionName") -fun Vector3D(x: Double, y: Double, z: Double): Vector3D = Vector3DImpl(x, y, z) +public fun Vector3D(x: Double, y: Double, z: Double): Vector3D = Vector3DImpl(x, y, z) -val Vector3D.r: Double get() = Euclidean3DSpace { sqrt(norm()) } +public val Vector3D.r: Double get() = Euclidean3DSpace { sqrt(norm()) } private data class Vector3DImpl( override val x: Double, @@ -38,19 +35,19 @@ private data class Vector3DImpl( override val z: Double ) : Vector3D -object Euclidean3DSpace : GeometrySpace { - override val zero: Vector3D = Vector3D(0.0, 0.0, 0.0) +public object Euclidean3DSpace : GeometrySpace { + public override val zero: Vector3D by lazy { Vector3D(0.0, 0.0, 0.0) } - fun Vector3D.norm(): Double = sqrt(x * x + y * y + z * z) + public fun Vector3D.norm(): Double = sqrt(x * x + y * y + z * z) - override fun Vector3D.distanceTo(other: Vector3D): Double = (this - other).norm() + public override fun Vector3D.distanceTo(other: Vector3D): Double = (this - other).norm() - override fun add(a: Vector3D, b: Vector3D): Vector3D = + public override fun add(a: Vector3D, b: Vector3D): Vector3D = Vector3D(a.x + b.x, a.y + b.y, a.z + b.z) - override fun multiply(a: Vector3D, k: Number): Vector3D = + public override fun multiply(a: Vector3D, k: Number): Vector3D = Vector3D(a.x * k.toDouble(), a.y * k.toDouble(), a.z * k.toDouble()) - override fun Vector3D.dot(other: Vector3D): Double = + public override fun Vector3D.dot(other: Vector3D): Double = x * other.x + y * other.y + z * other.z } diff --git a/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/GeometrySpace.kt b/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/GeometrySpace.kt index b65a8dd3a..6798a6291 100644 --- a/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/GeometrySpace.kt +++ b/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/GeometrySpace.kt @@ -2,16 +2,16 @@ package scientifik.kmath.geometry import scientifik.kmath.operations.Space -interface Vector +public interface Vector -interface GeometrySpace: Space { +public interface GeometrySpace: Space { /** * L2 distance */ - fun V.distanceTo(other: V): Double + public fun V.distanceTo(other: V): Double /** * Scalar product */ - infix fun V.dot(other: V): Double + public infix fun V.dot(other: V): Double } \ No newline at end of file diff --git a/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/Line.kt b/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/Line.kt index d802a103f..a529f7f1b 100644 --- a/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/Line.kt +++ b/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/Line.kt @@ -1,6 +1,6 @@ package scientifik.kmath.geometry -data class Line(val base: V, val direction: V) +public data class Line(val base: V, val direction: V) -typealias Line2D = Line -typealias Line3D = Line +public typealias Line2D = Line +public typealias Line3D = Line diff --git a/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/ReferenceFrame.kt b/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/ReferenceFrame.kt index 420e38ce2..49bf3793c 100644 --- a/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/ReferenceFrame.kt +++ b/kmath-geometry/src/commonMain/kotlin/scientifik/kmath/geometry/ReferenceFrame.kt @@ -1,4 +1,3 @@ package scientifik.kmath.geometry -interface ReferenceFrame { -} \ No newline at end of file +public interface ReferenceFrame diff --git a/kmath-koma/src/commonMain/kotlin/scientifik.kmath.linear/KomaMatrix.kt b/kmath-koma/src/commonMain/kotlin/scientifik.kmath.linear/KomaMatrix.kt index bd8fa782a..139b8cc8d 100644 --- a/kmath-koma/src/commonMain/kotlin/scientifik.kmath.linear/KomaMatrix.kt +++ b/kmath-koma/src/commonMain/kotlin/scientifik.kmath.linear/KomaMatrix.kt @@ -7,67 +7,64 @@ import scientifik.kmath.operations.invoke import scientifik.kmath.structures.Matrix import scientifik.kmath.structures.NDStructure -class KomaMatrixContext( +public class KomaMatrixContext( private val factory: MatrixFactory>, private val space: Space ) : MatrixContext { - - override fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> T): KomaMatrix = + public override fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> T): KomaMatrix = KomaMatrix(factory.zeros(rows, columns).fill(initializer)) - fun Matrix.toKoma(): KomaMatrix = if (this is KomaMatrix) { + public fun Matrix.toKoma(): KomaMatrix = if (this is KomaMatrix) this - } else { + else produce(rowNum, colNum) { i, j -> get(i, j) } - } - fun Point.toKoma(): KomaVector = if (this is KomaVector) { + public fun Point.toKoma(): KomaVector = if (this is KomaVector) this - } else { + else KomaVector(factory.zeros(size, 1).fill { i, _ -> get(i) }) - } - - override fun Matrix.dot(other: Matrix): KomaMatrix = + public override fun Matrix.dot(other: Matrix): KomaMatrix = KomaMatrix(toKoma().origin * other.toKoma().origin) - override fun Matrix.dot(vector: Point): KomaVector = + public override fun Matrix.dot(vector: Point): KomaVector = KomaVector(toKoma().origin * vector.toKoma().origin) - override operator fun Matrix.unaryMinus(): KomaMatrix = + public override operator fun Matrix.unaryMinus(): KomaMatrix = KomaMatrix(toKoma().origin.unaryMinus()) - override fun add(a: Matrix, b: Matrix): KomaMatrix = + public override fun add(a: Matrix, b: Matrix): KomaMatrix = KomaMatrix(a.toKoma().origin + b.toKoma().origin) - override operator fun Matrix.minus(b: Matrix): KomaMatrix = + public override operator fun Matrix.minus(b: Matrix): KomaMatrix = KomaMatrix(toKoma().origin - b.toKoma().origin) - override fun multiply(a: Matrix, k: Number): Matrix = + public override fun multiply(a: Matrix, k: Number): Matrix = produce(a.rowNum, a.colNum) { i, j -> space { a[i, j] * k } } - override operator fun Matrix.times(value: T): KomaMatrix = + public override operator fun Matrix.times(value: T): KomaMatrix = KomaMatrix(toKoma().origin * value) - companion object + public companion object } -fun KomaMatrixContext.solve(a: Matrix, b: Matrix) = +public fun KomaMatrixContext.solve(a: Matrix, b: Matrix): KomaMatrix = KomaMatrix(a.toKoma().origin.solve(b.toKoma().origin)) -fun KomaMatrixContext.solve(a: Matrix, b: Point) = +public fun KomaMatrixContext.solve(a: Matrix, b: Point): KomaVector = KomaVector(a.toKoma().origin.solve(b.toKoma().origin)) -fun KomaMatrixContext.inverse(a: Matrix) = +public fun KomaMatrixContext.inverse(a: Matrix): KomaMatrix = KomaMatrix(a.toKoma().origin.inv()) -class KomaMatrix(val origin: koma.matrix.Matrix, features: Set? = null) : FeaturedMatrix { - override val rowNum: Int get() = origin.numRows() - override val colNum: Int get() = origin.numCols() +public class KomaMatrix(public val origin: koma.matrix.Matrix, features: Set? = null) : + FeaturedMatrix { + public override val rowNum: Int get() = origin.numRows() + public override val colNum: Int get() = origin.numCols() - override val shape: IntArray get() = intArrayOf(origin.numRows(), origin.numCols()) + public override val shape: IntArray get() = intArrayOf(origin.numRows(), origin.numCols()) - override val features: Set = features ?: hashSetOf( + public override val features: Set = features ?: hashSetOf( object : DeterminantFeature { override val determinant: T get() = origin.det() }, @@ -94,11 +91,9 @@ class KomaMatrix(val origin: koma.matrix.Matrix, features: Set internal constructor(val origin: koma.matrix.Matrix) : Point { +public class KomaVector internal constructor(public val origin: koma.matrix.Matrix) : Point { override val size: Int get() = origin.numRows() init { diff --git a/kmath-prob/build.gradle.kts b/kmath-prob/build.gradle.kts index 4c9663e5f..795a8760e 100644 --- a/kmath-prob/build.gradle.kts +++ b/kmath-prob/build.gradle.kts @@ -13,4 +13,8 @@ kotlin.sourceSets { api("org.apache.commons:commons-rng-simple:1.3") } } + + jvmTest { + languageSettings.useExperimentalAnnotation("kotlinx.coroutines.FlowPreview") + } } diff --git a/kmath-prob/src/jvmTest/kotlin/scientifik/kmath/prob/CommonsDistributionsTest.kt b/kmath-prob/src/jvmTest/kotlin/scientifik/kmath/prob/CommonsDistributionsTest.kt index 7638c695e..0680083ce 100644 --- a/kmath-prob/src/jvmTest/kotlin/scientifik/kmath/prob/CommonsDistributionsTest.kt +++ b/kmath-prob/src/jvmTest/kotlin/scientifik/kmath/prob/CommonsDistributionsTest.kt @@ -6,7 +6,7 @@ import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test -class CommonsDistributionsTest { +internal class CommonsDistributionsTest { @Test fun testNormalDistributionSuspend() { val distribution = Distribution.normal(7.0, 2.0) @@ -24,5 +24,4 @@ class CommonsDistributionsTest { val sample = distribution.sample(generator).nextBlock(1000) Assertions.assertEquals(7.0, sample.average(), 0.1) } - -} \ No newline at end of file +} diff --git a/kmath-prob/src/jvmTest/kotlin/scientifik/kmath/prob/StatisticTest.kt b/kmath-prob/src/jvmTest/kotlin/scientifik/kmath/prob/StatisticTest.kt index 2613f71d5..30789a004 100644 --- a/kmath-prob/src/jvmTest/kotlin/scientifik/kmath/prob/StatisticTest.kt +++ b/kmath-prob/src/jvmTest/kotlin/scientifik/kmath/prob/StatisticTest.kt @@ -7,12 +7,14 @@ import kotlinx.coroutines.runBlocking import scientifik.kmath.streaming.chunked import kotlin.test.Test -class StatisticTest { +internal class StatisticTest { //create a random number generator. val generator = RandomGenerator.default(1) + //Create a stateless chain from generator. val data = generator.chain { nextDouble() } - //Convert a chaint to Flow and break it into chunks. + + //Convert a chain to Flow and break it into chunks. val chunked = data.chunked(1000) @Test @@ -22,7 +24,8 @@ class StatisticTest { .flow(chunked) //create a flow with results .drop(99) // Skip first 99 values and use one with total data .first() //get 1e5 data samples average + println(average) } } -} \ No newline at end of file +} 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 84e927721..64f2da4c1 100644 --- a/kmath-viktor/src/main/kotlin/scientifik/kmath/viktor/ViktorNDStructure.kt +++ b/kmath-viktor/src/main/kotlin/scientifik/kmath/viktor/ViktorNDStructure.kt @@ -5,82 +5,84 @@ import scientifik.kmath.operations.RealField import scientifik.kmath.structures.DefaultStrides import scientifik.kmath.structures.MutableNDStructure import scientifik.kmath.structures.NDField +import scientifik.kmath.structures.Strides @Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") -inline class ViktorNDStructure(val f64Buffer: F64Array) : MutableNDStructure { +public inline class ViktorNDStructure(public val f64Buffer: F64Array) : MutableNDStructure { + public override val shape: IntArray get() = f64Buffer.shape - override val shape: IntArray get() = f64Buffer.shape + public override inline fun get(index: IntArray): Double = f64Buffer.get(*index) - override inline fun get(index: IntArray): Double = f64Buffer.get(*index) - - override inline fun set(index: IntArray, value: Double) { + public override inline fun set(index: IntArray, value: Double) { f64Buffer.set(*index, value = value) } - override fun elements(): Sequence> { - return DefaultStrides(shape).indices().map { it to get(it) } - } + public override fun elements(): Sequence> = + DefaultStrides(shape).indices().map { it to get(it) } } -fun F64Array.asStructure(): ViktorNDStructure = ViktorNDStructure(this) +public fun F64Array.asStructure(): ViktorNDStructure = ViktorNDStructure(this) @Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") -class ViktorNDField(override val shape: IntArray) : NDField { - override val zero: ViktorNDStructure +public class ViktorNDField(public override val shape: IntArray) : NDField { + public override val zero: ViktorNDStructure get() = F64Array.full(init = 0.0, shape = *shape).asStructure() - override val one: ViktorNDStructure + + public override val one: ViktorNDStructure get() = F64Array.full(init = 1.0, shape = *shape).asStructure() - val strides = DefaultStrides(shape) + public val strides: Strides = DefaultStrides(shape) - override val elementContext: RealField get() = RealField + public override val elementContext: RealField get() = RealField - override fun produce(initializer: RealField.(IntArray) -> Double): ViktorNDStructure = F64Array(*shape).apply { - this@ViktorNDField.strides.indices().forEach { index -> - set(value = RealField.initializer(index), indices = *index) - } - }.asStructure() - - override fun map(arg: ViktorNDStructure, transform: RealField.(Double) -> Double): ViktorNDStructure = + public override fun produce(initializer: RealField.(IntArray) -> Double): ViktorNDStructure = F64Array(*shape).apply { this@ViktorNDField.strides.indices().forEach { index -> - set(value = RealField.transform(arg[index]), indices = *index) + set(value = RealField.initializer(index), indices = index) } }.asStructure() - override fun mapIndexed( + public override fun map(arg: ViktorNDStructure, transform: RealField.(Double) -> Double): ViktorNDStructure = + F64Array(*shape).apply { + this@ViktorNDField.strides.indices().forEach { index -> + set(value = RealField.transform(arg[index]), indices = index) + } + }.asStructure() + + public override fun mapIndexed( arg: ViktorNDStructure, transform: RealField.(index: IntArray, Double) -> Double ): ViktorNDStructure = F64Array(*shape).apply { this@ViktorNDField.strides.indices().forEach { index -> - set(value = RealField.transform(index, arg[index]), indices = *index) + set(value = RealField.transform(index, arg[index]), indices = index) } }.asStructure() - override fun combine( + public override fun combine( a: ViktorNDStructure, b: ViktorNDStructure, transform: RealField.(Double, Double) -> Double ): ViktorNDStructure = F64Array(*shape).apply { this@ViktorNDField.strides.indices().forEach { index -> - set(value = RealField.transform(a[index], b[index]), indices = *index) + set(value = RealField.transform(a[index], b[index]), indices = index) } }.asStructure() - override fun add(a: ViktorNDStructure, b: ViktorNDStructure): ViktorNDStructure { - return (a.f64Buffer + b.f64Buffer).asStructure() - } + public override fun add(a: ViktorNDStructure, b: ViktorNDStructure): ViktorNDStructure = + (a.f64Buffer + b.f64Buffer).asStructure() - override fun multiply(a: ViktorNDStructure, k: Number): ViktorNDStructure = + public override fun multiply(a: ViktorNDStructure, k: Number): ViktorNDStructure = (a.f64Buffer * k.toDouble()).asStructure() - override inline fun ViktorNDStructure.plus(b: ViktorNDStructure): ViktorNDStructure = + public override inline fun ViktorNDStructure.plus(b: ViktorNDStructure): ViktorNDStructure = (f64Buffer + b.f64Buffer).asStructure() - override inline fun ViktorNDStructure.minus(b: ViktorNDStructure): ViktorNDStructure = + public override inline fun ViktorNDStructure.minus(b: ViktorNDStructure): ViktorNDStructure = (f64Buffer - b.f64Buffer).asStructure() - override inline fun ViktorNDStructure.times(k: Number): ViktorNDStructure = (f64Buffer * k.toDouble()).asStructure() + public override inline fun ViktorNDStructure.times(k: Number): ViktorNDStructure = + (f64Buffer * k.toDouble()).asStructure() - override inline fun ViktorNDStructure.plus(arg: Double): ViktorNDStructure = (f64Buffer.plus(arg)).asStructure() + public override inline fun ViktorNDStructure.plus(arg: Double): ViktorNDStructure = + (f64Buffer.plus(arg)).asStructure() } \ No newline at end of file