From 068b90e7ab9e116bf6668dd960111f71da00b35e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 12 Feb 2020 22:26:25 +0300 Subject: [PATCH] Add different ways to provide data for XY interpolator --- .../kmath/interpolation/Interpolator.kt | 24 +++++++++++++++++++ .../kmath/interpolation/LoessInterpolator.kt | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/kmath-functions/src/commonMain/kotlin/scientifik/kmath/interpolation/Interpolator.kt b/kmath-functions/src/commonMain/kotlin/scientifik/kmath/interpolation/Interpolator.kt index 9b13c92e3..8d83e4198 100644 --- a/kmath-functions/src/commonMain/kotlin/scientifik/kmath/interpolation/Interpolator.kt +++ b/kmath-functions/src/commonMain/kotlin/scientifik/kmath/interpolation/Interpolator.kt @@ -3,6 +3,8 @@ package scientifik.kmath.interpolation import scientifik.kmath.functions.PiecewisePolynomial import scientifik.kmath.functions.value import scientifik.kmath.operations.Ring +import scientifik.kmath.structures.Buffer +import scientifik.kmath.structures.asBuffer interface Interpolator { fun interpolate(points: XYPointSet): (X) -> Y @@ -18,4 +20,26 @@ interface PolynomialInterpolator> : Interpolator { override fun interpolate(points: XYPointSet): (T) -> T = { x -> interpolatePolynomials(points).value(algebra, x) ?: getDefaultValue() } +} + +fun > PolynomialInterpolator.interpolatePolynomials( + x: Buffer, + y: Buffer +): PiecewisePolynomial { + val pointSet = BufferXYPointSet(x, y) + return interpolatePolynomials(pointSet) +} + +fun > PolynomialInterpolator.interpolatePolynomials( + data: Map +): PiecewisePolynomial { + val pointSet = BufferXYPointSet(data.keys.toList().asBuffer(), data.values.toList().asBuffer()) + return interpolatePolynomials(pointSet) +} + +fun > PolynomialInterpolator.interpolatePolynomials( + data: List> +): PiecewisePolynomial { + val pointSet = BufferXYPointSet(data.map { it.first }.asBuffer(), data.map { it.second }.asBuffer()) + return interpolatePolynomials(pointSet) } \ No newline at end of file diff --git a/kmath-functions/src/commonMain/kotlin/scientifik/kmath/interpolation/LoessInterpolator.kt b/kmath-functions/src/commonMain/kotlin/scientifik/kmath/interpolation/LoessInterpolator.kt index c29549ed0..6707bd8bc 100644 --- a/kmath-functions/src/commonMain/kotlin/scientifik/kmath/interpolation/LoessInterpolator.kt +++ b/kmath-functions/src/commonMain/kotlin/scientifik/kmath/interpolation/LoessInterpolator.kt @@ -1,4 +1,4 @@ -//package scientifik.kmath.interpolation +package scientifik.kmath.interpolation // //import scientifik.kmath.functions.PiecewisePolynomial //import scientifik.kmath.operations.Ring