Minor change in grid builders

This commit is contained in:
Alexander Nozik 2020-06-08 12:07:27 +03:00
parent 1a869ace0e
commit 774b1123f7
2 changed files with 30 additions and 20 deletions

View File

@ -128,14 +128,14 @@ fun <T : Comparable<T>, F : Field<T>> GenericMatrixContext<T, F>.lup(
luRow[col] = sum luRow[col] = sum
// maintain best permutation choice // maintain best permutation choice
if (abs(sum) > largest) { if (this@lup.abs(sum) > largest) {
largest = abs(sum) largest = this@lup.abs(sum)
max = row max = row
} }
} }
// Singularity check // Singularity check
if (checkSingular(abs(lu[max, col]))) { if (checkSingular(this@lup.abs(lu[max, col]))) {
error("The matrix is singular") error("The matrix is singular")
} }

View File

@ -1,5 +1,7 @@
package scientifik.kmath.misc package scientifik.kmath.misc
import kotlin.math.abs
/** /**
* Convert double range to sequence. * Convert double range to sequence.
* *
@ -8,28 +10,36 @@ package scientifik.kmath.misc
* *
* If step is negative, the same goes from upper boundary downwards * If step is negative, the same goes from upper boundary downwards
*/ */
fun ClosedFloatingPointRange<Double>.toSequence(step: Double): Sequence<Double> = fun ClosedFloatingPointRange<Double>.toSequenceWithStep(step: Double): Sequence<Double> = when {
when { step == 0.0 -> error("Zero step in double progression")
step == 0.0 -> error("Zero step in double progression") step > 0 -> sequence {
step > 0 -> sequence { var current = start
var current = start while (current <= endInclusive) {
while (current <= endInclusive) { yield(current)
yield(current) current += step
current += step
}
}
else -> sequence {
var current = endInclusive
while (current >= start) {
yield(current)
current += step
}
}
} }
}
else -> sequence {
var current = endInclusive
while (current >= start) {
yield(current)
current += step
}
}
}
/**
* Convert double range to sequence with the fixed number of points
*/
fun ClosedFloatingPointRange<Double>.toSequenceWithPoints(numPoints: Int): Sequence<Double> {
require(numPoints > 1) { "The number of points should be more than 2" }
return toSequenceWithStep(abs(endInclusive - start) / (numPoints - 1))
}
/** /**
* Convert double range to array of evenly spaced doubles, where the size of array equals [numPoints] * Convert double range to array of evenly spaced doubles, where the size of array equals [numPoints]
*/ */
@Deprecated("Replace by 'toSequenceWithPoints'")
fun ClosedFloatingPointRange<Double>.toGrid(numPoints: Int): DoubleArray { fun ClosedFloatingPointRange<Double>.toGrid(numPoints: Int): DoubleArray {
if (numPoints < 2) error("Can't create generic grid with less than two points") if (numPoints < 2) error("Can't create generic grid with less than two points")
return DoubleArray(numPoints) { i -> start + (endInclusive - start) / (numPoints - 1) * i } return DoubleArray(numPoints) { i -> start + (endInclusive - start) / (numPoints - 1) * i }