From f8bfc46151254e3fdb6d93b0d8c94f645012b9c2 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 26 Jul 2009 19:28:21 +0800 Subject: [PATCH] interpolation: refactoring for access to coefficient/weight computation Signed-off-by: Christoph Ruegg --- .../CubicHermiteSplineInterpolation.cs | 23 ++++++++++++- .../FloaterHormannRationalInterpolation.cs | 33 +++++++++++++++++-- .../Algorithms/LinearSplineInterpolation.cs | 20 ++++++++++- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/Managed/Interpolation/Algorithms/CubicHermiteSplineInterpolation.cs b/src/Managed/Interpolation/Algorithms/CubicHermiteSplineInterpolation.cs index 5d5c203c..2918ad06 100644 --- a/src/Managed/Interpolation/Algorithms/CubicHermiteSplineInterpolation.cs +++ b/src/Managed/Interpolation/Algorithms/CubicHermiteSplineInterpolation.cs @@ -96,6 +96,27 @@ namespace MathNet.Numerics.Interpolation.Algorithms IList samplePoints, IList sampleValues, IList sampleDerivatives) + { + double[] coefficients = EvaluateSplineCoefficients( + samplePoints, + sampleValues, + sampleDerivatives); + + _spline.Initialize(samplePoints, coefficients); + } + + /// + /// Evaluate the spline coefficients as used + /// internally by this interpolation algorithm. + /// + /// Sample Points t, sorted ascending. + /// Sample Values x(t) + /// Sample Derivatives x'(t) + /// Spline Coefficient Vector + public static double[] EvaluateSplineCoefficients( + IList samplePoints, + IList sampleValues, + IList sampleDerivatives) { if (null == samplePoints) { @@ -136,7 +157,7 @@ namespace MathNet.Numerics.Interpolation.Algorithms coefficients[j + 3] = ((2 * (sampleValues[i] - sampleValues[i + 1])) + (sampleDerivatives[i] * delta) + (sampleDerivatives[i + 1] * delta)) / delta3; } - _spline.Initialize(samplePoints, coefficients); + return coefficients; } /// diff --git a/src/Managed/Interpolation/Algorithms/FloaterHormannRationalInterpolation.cs b/src/Managed/Interpolation/Algorithms/FloaterHormannRationalInterpolation.cs index 0a7d652a..d7e1cab8 100644 --- a/src/Managed/Interpolation/Algorithms/FloaterHormannRationalInterpolation.cs +++ b/src/Managed/Interpolation/Algorithms/FloaterHormannRationalInterpolation.cs @@ -101,7 +101,12 @@ namespace MathNet.Numerics.Interpolation.Algorithms throw new ArgumentNullException("samplePoints"); } - Initialize(samplePoints, sampleValues, Math.Min(3, samplePoints.Count - 1)); + double[] weights = EvaluateBarycentricWeights( + samplePoints, + sampleValues, + Math.Min(3, samplePoints.Count - 1)); + + _barycentric.Initialize(samplePoints, sampleValues, weights); } /// @@ -117,6 +122,30 @@ namespace MathNet.Numerics.Interpolation.Algorithms IList samplePoints, IList sampleValues, int order) + { + double[] weights = EvaluateBarycentricWeights( + samplePoints, + sampleValues, + order); + + _barycentric.Initialize(samplePoints, sampleValues, weights); + } + + /// + /// Evaluate the barycentric weights as used + /// internally by this interpolation algorithm. + /// + /// Sample Points t + /// Sample Values x(t) + /// + /// Order of the interpolation scheme, 0 <= order <= N. + /// In most cases a value between 3 and 8 gives good results. + /// + /// Barycentric Weight Vector + public static double[] EvaluateBarycentricWeights( + IList samplePoints, + IList sampleValues, + int order) { if (null == samplePoints) { @@ -203,7 +232,7 @@ namespace MathNet.Numerics.Interpolation.Algorithms weights[perm[i]] = sortedWeights[i]; } - _barycentric.Initialize(samplePoints, sampleValues, weights); + return weights; } /// diff --git a/src/Managed/Interpolation/Algorithms/LinearSplineInterpolation.cs b/src/Managed/Interpolation/Algorithms/LinearSplineInterpolation.cs index 82d2bf6c..d0a78577 100644 --- a/src/Managed/Interpolation/Algorithms/LinearSplineInterpolation.cs +++ b/src/Managed/Interpolation/Algorithms/LinearSplineInterpolation.cs @@ -92,6 +92,24 @@ namespace MathNet.Numerics.Interpolation.Algorithms public void Initialize( IList samplePoints, IList sampleValues) + { + double[] coefficients = EvaluateSplineCoefficients( + samplePoints, + sampleValues); + + _spline.Initialize(samplePoints, coefficients); + } + + /// + /// Evaluate the spline coefficients as used + /// internally by this interpolation algorithm. + /// + /// Sample Points t, sorted ascending. + /// Sample Values x(t) + /// Spline Coefficient Vector + public static double[] EvaluateSplineCoefficients( + IList samplePoints, + IList sampleValues) { if (null == samplePoints) { @@ -123,7 +141,7 @@ namespace MathNet.Numerics.Interpolation.Algorithms coefficients[j + 3] = 0; } - _spline.Initialize(samplePoints, coefficients); + return coefficients; } ///