Additional piecewise extension

This commit is contained in:
Alexander Nozik 2021-05-14 12:11:37 +03:00
parent dc9ec54644
commit 6eb95f26eb
2 changed files with 14 additions and 3 deletions

View File

@ -79,4 +79,13 @@ public class OrderedPiecewisePolynomial<T : Comparable<T>>(delimiter: T) :
public fun <T : Comparable<T>, C : Ring<T>> PiecewisePolynomial<T>.value(ring: C, arg: T): T? = public fun <T : Comparable<T>, C : Ring<T>> PiecewisePolynomial<T>.value(ring: C, arg: T): T? =
findPiece(arg)?.value(ring, arg) findPiece(arg)?.value(ring, arg)
/**
* Convert this polynomial to a function returning nullable value (null if argument is outside piecewise range).
*/
public fun <T : Comparable<T>, C : Ring<T>> PiecewisePolynomial<T>.asFunction(ring: C): (T) -> T? = { value(ring, it) } public fun <T : Comparable<T>, C : Ring<T>> PiecewisePolynomial<T>.asFunction(ring: C): (T) -> T? = { value(ring, it) }
/**
* Convert this polynomial to a function using [defaultValue] for arguments outside the piecewise range.
*/
public fun <T : Comparable<T>, C : Ring<T>> PiecewisePolynomial<T>.asFunction(ring: C, defaultValue: T): (T) -> T =
{ value(ring, it) ?: defaultValue }

View File

@ -42,9 +42,11 @@ public class SplineInterpolator<T : Comparable<T>>(
val g = 2.0 * (points.x[i + 1] - points.x[i - 1]) - h[i - 1] * mu[i - 1] val g = 2.0 * (points.x[i + 1] - points.x[i - 1]) - h[i - 1] * mu[i - 1]
mu[i] = h[i] / g mu[i] = h[i] / g
z[i] = z[i] = (3.0 * (points.y[i + 1] * h[i - 1]
(3.0 * (points.y[i + 1] * h[i - 1] - points.x[i] * (points.x[i + 1] - points.x[i - 1]) + points.y[i - 1] * h[i]) / (h[i - 1] * h[i]) - points.x[i] * (points.x[i + 1] - points.x[i - 1])
- h[i - 1] * z[i - 1]) / g + points.y[i - 1] * h[i]) / (h[i - 1] * h[i])
- h[i - 1] * z[i - 1]
) / g
} }
// cubic spline coefficients -- b is linear, c quadratic, d is cubic (original y's are constants) // cubic spline coefficients -- b is linear, c quadratic, d is cubic (original y's are constants)