diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/Expression.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/Expression.kt index c3a6caf58..f7ae09d02 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/Expression.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/expressions/Expression.kt @@ -17,9 +17,8 @@ interface ExpressionContext { } internal class VariableExpression(val name: String, val default: T? = null) : Expression { - override fun invoke(arguments: Map): T { - return arguments[name] ?: default ?: error("The parameter not found: $name") - } + override fun invoke(arguments: Map): T = + arguments[name] ?: default ?: error("Parameter not found: $name") } internal class ConstantExpression(val value: T) : Expression { diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/PhantomHistogram.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/PhantomHistogram.kt index 75d9ebd74..860eb9346 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/PhantomHistogram.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/histogram/PhantomHistogram.kt @@ -52,9 +52,8 @@ class PhantomHistogram>( override val dimension: Int get() = data.dimension - override fun iterator(): Iterator> { - return bins.asSequence().map { entry -> PhantomBin(entry.key, data[entry.value]) }.iterator() - } + override fun iterator(): Iterator> = + bins.asSequence().map { entry -> PhantomBin(entry.key, data[entry.value]) }.iterator() override fun get(point: Point): PhantomBin? { val template = bins.keys.find { it.contains(point) } diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LUDecomposition.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LUDecomposition.kt index 402947288..2ceb5bcd3 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LUDecomposition.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LUDecomposition.kt @@ -90,7 +90,7 @@ abstract class LUDecomposition, F : Field>(val matrix: Matr } /** - * In-place transformation for [MutableNDArray], using given transformation for each element + * In-place transformation for [MutableNDStructure], using given transformation for each element */ operator fun MutableNDStructure.set(i: Int, j: Int, value: T) { this[intArrayOf(i, j)] = value @@ -181,9 +181,7 @@ abstract class LUDecomposition, F : Field>(val matrix: Matr * @return the pivot permutation vector * @see .getP */ - fun getPivot(): IntArray { - return pivot.copyOf() - } + fun getPivot(): IntArray = pivot.copyOf() } diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LinearAlgrebra.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LinearAlgrebra.kt index 72a859111..a3d2feccc 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LinearAlgrebra.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/LinearAlgrebra.kt @@ -29,8 +29,8 @@ fun List.toVector() = Vector.real(this.size) { this[it] } /** * Convert matrix to vector if it is possible */ -fun > Matrix.toVector(): Vector { - return if (this.numCols == 1) { +fun > Matrix.toVector(): Vector = + if (this.numCols == 1) { // if (this is ArrayMatrix) { // //Reuse existing underlying array // ArrayVector(ArrayVectorSpace(rows, context.field, context.ndFactory), array) @@ -38,9 +38,8 @@ fun > Matrix.toVector(): Vector { // //Generic vector // vector(rows, context.field) { get(it, 0) } // } - Vector.generic(numRows, context.ring) { get(it, 0) } - } else error("Can't convert matrix with more than one column to vector") -} + Vector.generic(numRows, context.ring) { get(it, 0) } + } else error("Can't convert matrix with more than one column to vector") fun > Vector.toMatrix(): Matrix { // val context = StructureMatrixContext(size, 1, context.space) @@ -57,9 +56,8 @@ fun > Vector.toMatrix(): Matrix { } object VectorL2Norm : Norm, Double> { - override fun norm(arg: Vector): Double { - return kotlin.math.sqrt(arg.asSequence().sumByDouble { it.toDouble() }) - } + override fun norm(arg: Vector): Double = + kotlin.math.sqrt(arg.asSequence().sumByDouble { it.toDouble() }) } typealias RealVector = Vector diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/misc/Grids.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/misc/Grids.kt index e586bca71..90ce5da68 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/misc/Grids.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/misc/Grids.kt @@ -8,30 +8,29 @@ package scientifik.kmath.misc * * 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 -> sequence { - var current = start - while (current <= endInclusive) { - yield(current) - current += step +fun ClosedFloatingPointRange.toSequence(step: Double): Sequence = + when { + step == 0.0 -> error("Zero step in double progression") + step > 0 -> sequence { + var current = start + while (current <= endInclusive) { + yield(current) + 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 array of evenly spaced doubles, where the size of array equals [numPoints] */ fun ClosedFloatingPointRange.toGrid(numPoints: Int): DoubleArray { - if (numPoints < 2) error("Can't buffered 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 } } \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt index 219af01fd..a6bd8d78a 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Algebra.kt @@ -34,6 +34,7 @@ interface Space { operator fun T.div(k: Number) = multiply(this, 1.0 / k.toDouble()) operator fun Number.times(b: T) = b * this fun Iterable.sum(): T = fold(zero) { left, right -> left + right } + fun Sequence.sum(): T = fold(zero) { left, right -> left + right } }