Iaroslav Postovalov e9c31b7827
Merge branch 'master' into dev
# Conflicts:
#	README.md
#	build.gradle.kts
#	examples/src/main/kotlin/space/kscience/kmath/stat/DistributionBenchmark.kt
#	examples/src/main/kotlin/space/kscience/kmath/structures/ParallelRealNDField.kt
#	kmath-ast/README.md
#	kmath-ast/build.gradle.kts
#	kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstExpression.kt
#	kmath-complex/README.md
#	kmath-complex/build.gradle.kts
#	kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexNDField.kt
#	kmath-core/README.md
#	kmath-core/build.gradle.kts
#	kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MST.kt
#	kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt
#	kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/RealNDField.kt
#	kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/RealBufferField.kt
#	kmath-coroutines/build.gradle.kts
#	kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingRealChain.kt
#	kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/structures/LazyNDStructure.kt
#	kmath-dimensions/src/commonTest/kotlin/kscience/dimensions/DMatrixContextTest.kt
#	kmath-for-real/README.md
#	kmath-functions/README.md
#	kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/Interpolator.kt
#	kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/LoessInterpolator.kt
#	kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/XYPointSet.kt
#	kmath-geometry/build.gradle.kts
#	kmath-nd4j/README.md
#	kmath-nd4j/build.gradle.kts
#	kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/FactorizedDistribution.kt
#	kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Distribution.kt
#	kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Fitting.kt
#	kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/OptimizationProblem.kt
#	kmath-stat/src/jvmMain/kotlin/space/kscience/kmath/stat/distributions.kt
#	kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorNDStructure.kt
2021-04-16 18:02:44 +07:00

83 lines
2.8 KiB
Kotlin

/*
* Copyright 2018-2021 KMath contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package space.kscience.kmath.functions
import space.kscience.kmath.operations.Ring
/**
* Represents piecewise-defined function.
*
* @param T the piece key type.
* @param R the sub-function type.
*/
public fun interface Piecewise<T, R> {
/**
* Returns the appropriate sub-function for given piece key.
*/
public fun findPiece(arg: T): R?
}
/**
* Represents piecewise-defined function where all the sub-functions are polynomials.
*/
public fun interface PiecewisePolynomial<T : Any> : Piecewise<T, Polynomial<T>>
/**
* Basic [Piecewise] implementation where all the pieces are ordered by the [Comparable] type instances.
*
* @param T the comparable piece key type.
*/
public class OrderedPiecewisePolynomial<T : Comparable<T>>(delimiter: T) :
PiecewisePolynomial<T> {
private val delimiters: MutableList<T> = arrayListOf(delimiter)
private val pieces: MutableList<Polynomial<T>> = arrayListOf()
/**
* Dynamically adds a piece to the right side (beyond maximum argument value of previous piece)
*
* @param right new rightmost position. If is less then current rightmost position, an error is thrown.
* @param piece the sub-function.
*/
public fun putRight(right: T, piece: Polynomial<T>) {
require(right > delimiters.last()) { "New delimiter should be to the right of old one" }
delimiters += right
pieces += piece
}
/**
* Dynamically adds a piece to the left side (beyond maximum argument value of previous piece)
*
* @param left the new leftmost position. If is less then current rightmost position, an error is thrown.
* @param piece the sub-function.
*/
public fun putLeft(left: T, piece: Polynomial<T>) {
require(left < delimiters.first()) { "New delimiter should be to the left of old one" }
delimiters.add(0, left)
pieces.add(0, piece)
}
public override fun findPiece(arg: T): Polynomial<T>? {
if (arg < delimiters.first() || arg >= delimiters.last())
return null
else {
for (index in 1 until delimiters.size)
if (arg < delimiters[index])
return pieces[index - 1]
error("Piece not found")
}
}
}
/**
* Return a value of polynomial function with given [ring] an given [arg] or null if argument is outside of piecewise
* definition.
*/
public fun <T : Comparable<T>, C : Ring<T>> PiecewisePolynomial<T>.value(ring: C, arg: T): T? =
findPiece(arg)?.value(ring, arg)
public fun <T : Comparable<T>, C : Ring<T>> PiecewisePolynomial<T>.asFunction(ring: C): (T) -> T? = { value(ring, it) }