diff --git a/kmath-common/src/main/kotlin/scientifik/kmath/misc/Grids.kt b/kmath-common/src/main/kotlin/scientifik/kmath/misc/Grids.kt new file mode 100644 index 000000000..5ea4bd949 --- /dev/null +++ b/kmath-common/src/main/kotlin/scientifik/kmath/misc/Grids.kt @@ -0,0 +1,40 @@ +package scientifik.kmath.misc + +import kotlin.coroutines.experimental.buildSequence + + +/** + * Convert double range to sequence. + * + * If the step is positive, than the sequence starts with the lower boundary and increments by [step] until current value is lower than upper boundary. + * The boundary itself is not necessary included. + * + * If step is negative, the same goes from upper boundary downwards + */ +fun ClosedFloatingPointRange.toSequence(step: Double): Sequence { + return when { + step == 0.0 -> error("Zero step in double progression") + step > 0 -> buildSequence { + var current = start + while (current <= endInclusive) { + yield(current) + current += step + } + } + else -> buildSequence { + var current = endInclusive + while (current >= start) { + yield(current) + current += step + } + } + } +} + +/** + * Convert double range to array of evenly spaced doubles, where the size of array equals [numPoints] + */ +fun ClosedFloatingPointRange.toGrid(numPoints: Int): DoubleArray { + if (numPoints < 2) error("Can't create grid with less than two points") + return DoubleArray(numPoints) { i -> start + (endInclusive - start) / (numPoints - 1) * i } +} \ No newline at end of file diff --git a/kmath-common/src/main/kotlin/scientifik/kmath/structures/BufferNDField.kt b/kmath-common/src/main/kotlin/scientifik/kmath/structures/BufferNDField.kt index 6a2df9acc..5b3ef4c6a 100644 --- a/kmath-common/src/main/kotlin/scientifik/kmath/structures/BufferNDField.kt +++ b/kmath-common/src/main/kotlin/scientifik/kmath/structures/BufferNDField.kt @@ -40,11 +40,12 @@ abstract class BufferNDField(shape: List, field: Field) : NDField( }.sum() } - protected fun index(offset: Int): List{ + //TODO introduce a fast way to calculate index of the next element? + protected fun index(offset: Int): List { return buildSequence { var current = offset - var strideIndex = strides.size-2 - while (strideIndex>=0){ + var strideIndex = strides.size - 2 + while (strideIndex >= 0) { yield(current / strides[strideIndex]) current %= strides[strideIndex] strideIndex-- @@ -59,7 +60,7 @@ abstract class BufferNDField(shape: List, field: Field) : NDField( protected abstract fun createBuffer(capacity: Int, initializer: (Int) -> T): Buffer override fun produce(initializer: (List) -> T): NDArray { - val buffer = createBuffer(capacity){initializer(index(it))} + val buffer = createBuffer(capacity) { initializer(index(it)) } return BufferNDArray(this, buffer) } diff --git a/kmath-jvm/build.gradle b/kmath-jvm/build.gradle index 0f1240a3a..110dc0287 100644 --- a/kmath-jvm/build.gradle +++ b/kmath-jvm/build.gradle @@ -21,4 +21,10 @@ compileKotlin { compileTestKotlin { kotlinOptions.jvmTarget = "1.8" } -sourceCompatibility = "1.8" \ No newline at end of file +sourceCompatibility = "1.8" + +kotlin { + experimental { + coroutines "enable" + } +} \ No newline at end of file