forked from kscience/kmath
Grid generators for 1D double array
This commit is contained in:
parent
ba63b2e373
commit
9c064b00b1
40
kmath-common/src/main/kotlin/scientifik/kmath/misc/Grids.kt
Normal file
40
kmath-common/src/main/kotlin/scientifik/kmath/misc/Grids.kt
Normal file
@ -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<Double>.toSequence(step: Double): Sequence<Double> {
|
||||||
|
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<Double>.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 }
|
||||||
|
}
|
@ -40,6 +40,7 @@ abstract class BufferNDField<T>(shape: List<Int>, field: Field<T>) : NDField<T>(
|
|||||||
}.sum()
|
}.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO introduce a fast way to calculate index of the next element?
|
||||||
protected fun index(offset: Int): List<Int> {
|
protected fun index(offset: Int): List<Int> {
|
||||||
return buildSequence {
|
return buildSequence {
|
||||||
var current = offset
|
var current = offset
|
||||||
|
@ -22,3 +22,9 @@ compileTestKotlin {
|
|||||||
kotlinOptions.jvmTarget = "1.8"
|
kotlinOptions.jvmTarget = "1.8"
|
||||||
}
|
}
|
||||||
sourceCompatibility = "1.8"
|
sourceCompatibility = "1.8"
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
experimental {
|
||||||
|
coroutines "enable"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user