diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferPrimitiveAccess.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferPrimitiveAccess.kt new file mode 100644 index 000000000..0e465c64b --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferPrimitiveAccess.kt @@ -0,0 +1,37 @@ +package space.kscience.kmath.structures + +import space.kscience.kmath.misc.UnstableKMathAPI + +/** + * Non-boxing access to primitive [Double] + */ +@OptIn(UnstableKMathAPI::class) +public fun Buffer.getDouble(index: Int): Double = if (this is BufferView) { + val originIndex = originIndex(index) + if( originIndex>=0) { + origin.getDouble(originIndex) + } else { + get(index) + } +} else if (this is DoubleBuffer) { + array[index] +} else { + get(index) +} + +/** + * Non-boxing access to primitive [Int] + */ +@OptIn(UnstableKMathAPI::class) +public fun Buffer.getInt(index: Int): Int = if (this is BufferView) { + val originIndex = originIndex(index) + if( originIndex>=0) { + origin.getInt(originIndex) + } else { + get(index) + } +} else if (this is IntBuffer) { + array[index] +} else { + get(index) +} \ No newline at end of file diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/MCScope.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/MCScope.kt similarity index 98% rename from kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/MCScope.kt rename to kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/MCScope.kt index 5e1e577ba..4fb7cb2fb 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/MCScope.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/MCScope.kt @@ -3,7 +3,7 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. */ -package space.kscience.kmath.stat +package space.kscience.kmath.random import kotlinx.coroutines.* import kotlin.contracts.InvocationKind diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomChain.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomChain.kt similarity index 97% rename from kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomChain.kt rename to kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomChain.kt index 61e472334..aec85d9d5 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomChain.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomChain.kt @@ -3,7 +3,7 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. */ -package space.kscience.kmath.stat +package space.kscience.kmath.random import space.kscience.kmath.chains.BlockingDoubleChain import space.kscience.kmath.chains.Chain diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomGenerator.kt similarity index 99% rename from kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt rename to kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomGenerator.kt index 98ee6402a..30b27e338 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/RandomGenerator.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomGenerator.kt @@ -3,7 +3,7 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. */ -package space.kscience.kmath.stat +package space.kscience.kmath.random import space.kscience.kmath.structures.DoubleBuffer import kotlin.random.Random diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Rank.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Rank.kt index 92b7e1e7b..5a5b99177 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Rank.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Rank.kt @@ -3,26 +3,33 @@ package space.kscience.kmath.stat import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.asIterable -public class Rank>: BlockingStatistic { - override fun evaluateBlocking(data: Buffer): IntArray { - // https://www.geeksforgeeks.org/rank-elements-array/ - val permutations = ArrayList>(data.size) - data.asIterable().mapIndexedTo(permutations) { i, v -> v to i } - permutations.sortBy { it.first } - var rank = 1 - var i = 0 - val r = IntArray(data.size) { 0 } - while (i < data.size) { - var j = i - while (j < data.size - 1 && permutations[j].first == permutations[j + 1]) ++j - val n = j - i + 1 - (0 until n).map { k -> - val idx = permutations[i + k].second - r[idx] = rank + ((n - 1) * 0.5f).toInt() +/** + * Rank statistics + */ +public class Rank> : BlockingStatistic { + override fun evaluateBlocking(data: Buffer): IntArray = Companion.evaluate(data) + + public companion object { + public fun > evaluate(data: Buffer): IntArray { + // https://www.geeksforgeeks.org/rank-elements-array/ + val permutations = ArrayList>(data.size) + data.asIterable().mapIndexedTo(permutations) { i, v -> v to i } + permutations.sortBy { it.first } + var rank = 1 + var i = 0 + val r = IntArray(data.size) + while (i < data.size) { + var j = i + while (j < data.size - 1 && permutations[j].first == permutations[j + 1]) ++j + val n = j - i + 1 + (0 until n).map { k -> + val idx = permutations[i + k].second + r[idx] = rank + ((n - 1) * 0.5f).toInt() + } + rank += n + i += n } - rank += n - i += n + return r } - return r } } \ No newline at end of file