From cf5bd78dca5637ae37a61f3e54433fca2816e788 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 21 Dec 2013 12:29:37 +0100 Subject: [PATCH] Interpolation: drop old redundant barycentric interpolation classes --- .../Interpolation/BarycentricInterpolation.cs | 235 --------------- .../EquidistantPolynomialInterpolation.cs | 215 ------------- .../FloaterHormannRationalInterpolation.cs | 284 ------------------ src/Numerics/Numerics.csproj | 3 - 4 files changed, 737 deletions(-) delete mode 100644 src/Numerics/Interpolation/BarycentricInterpolation.cs delete mode 100644 src/Numerics/Interpolation/EquidistantPolynomialInterpolation.cs delete mode 100644 src/Numerics/Interpolation/FloaterHormannRationalInterpolation.cs diff --git a/src/Numerics/Interpolation/BarycentricInterpolation.cs b/src/Numerics/Interpolation/BarycentricInterpolation.cs deleted file mode 100644 index 88e01e9e..00000000 --- a/src/Numerics/Interpolation/BarycentricInterpolation.cs +++ /dev/null @@ -1,235 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -using System; -using System.Collections.Generic; - -namespace MathNet.Numerics.Interpolation -{ - /// - /// Barycentric Interpolation Algorithm. - /// - /// - /// This algorithm neither supports differentiation nor integration. - /// - public class BarycentricInterpolation : IInterpolation - { - /// - /// Sample Points t. - /// - IList _points; - - /// - /// Sample Values x(t). - /// - IList _values; - - /// - /// Barycentric Weights w(t). - /// - IList _weights; - - /// - /// Initializes a new instance of the BarycentricInterpolation class. - /// - public BarycentricInterpolation() - { - } - - /// - /// Initializes a new instance of the BarycentricInterpolation class. - /// - /// Sample Points t (no sorting assumed) - /// Sample Values x(t) - /// Barycentric weights w(t) - public BarycentricInterpolation(IList samplePoints, IList sampleValues, IList barycentricWeights) - { - Initialize(samplePoints, sampleValues, barycentricWeights); - } - - /// - /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). - /// - bool IInterpolation.SupportsDifferentiation - { - get { return false; } - } - - /// - /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). - /// - bool IInterpolation.SupportsIntegration - { - get { return false; } - } - - /// - /// Initialize the interpolation method with the given sample set (no sorting assumed). - /// - /// Sample Points t - /// Sample Values x(t) - /// Barycentric weights w(t) - public void Initialize(IList samplePoints, IList sampleValues, IList barycentricWeights) - { - if (null == samplePoints) - { - throw new ArgumentNullException("samplePoints"); - } - - if (null == sampleValues) - { - throw new ArgumentNullException("sampleValues"); - } - - if (null == barycentricWeights) - { - throw new ArgumentNullException("barycentricWeights"); - } - - if (samplePoints.Count < 1) - { - throw new ArgumentOutOfRangeException("samplePoints"); - } - - if (samplePoints.Count != sampleValues.Count) - { - throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLength); - } - - if (samplePoints.Count != barycentricWeights.Count) - { - throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLength); - } - - _points = samplePoints; - _values = sampleValues; - _weights = barycentricWeights; - } - - /// - /// Interpolate at point t. - /// - /// Point t to interpolate at. - /// Interpolated value x(t). - public double Interpolate(double t) - { - // trivial case: only one sample? - if (_points.Count == 1) - { - return _values[0]; - } - - // evaluate closest point and offset from that point (no sorting assumed) - int closestPoint = 0; - double offset = t - _points[0]; - for (int i = 1; i < _points.Count; i++) - { - if (Math.Abs(t - _points[i]) < Math.Abs(offset)) - { - offset = t - _points[i]; - closestPoint = i; - } - } - - // trivial case: on a known sample point? - if (offset == 0.0) - { - // NOTE (cdrnet, 200908) not offset.AlmostZero() by design - return _values[closestPoint]; - } - - if (Math.Abs(offset) > 1e-150) - { - // no need to guard against overflow, so use fast formula - closestPoint = -1; - offset = 1.0; - } - - double s1 = 0.0; - double s2 = 0.0; - for (int i = 0; i < _points.Count; i++) - { - if (i != closestPoint) - { - double v = offset*_weights[i]/(t - _points[i]); - s1 = s1 + (v*_values[i]); - s2 = s2 + v; - } - else - { - double v = _weights[i]; - s1 = s1 + (v*_values[i]); - s2 = s2 + v; - } - } - - return s1/s2; - } - - /// - /// Differentiate at point t. NOT SUPPORTED. - /// - /// Point t to interpolate at. - /// Interpolated first derivative at point t. - double IInterpolation.Differentiate(double t) - { - throw new NotSupportedException(); - } - - /// - /// Differentiate twice at point t. NOT SUPPORTED. - /// - /// Point t to interpolate at. - /// Interpolated second derivative at point t. - double IInterpolation.Differentiate2(double t) - { - throw new NotSupportedException(); - } - - /// - /// Indefinite integral at point t. NOT SUPPORTED. - /// - /// Point t to integrate at. - double IInterpolation.Integrate(double t) - { - throw new NotSupportedException(); - } - - /// - /// Definite integral between points a and b. NOT SUPPORTED. - /// - /// Left bound of the integration interval [a,b]. - /// Right bound of the integration interval [a,b]. - double IInterpolation.Integrate(double a, double b) - { - throw new NotSupportedException(); - } - } -} diff --git a/src/Numerics/Interpolation/EquidistantPolynomialInterpolation.cs b/src/Numerics/Interpolation/EquidistantPolynomialInterpolation.cs deleted file mode 100644 index 72ddb638..00000000 --- a/src/Numerics/Interpolation/EquidistantPolynomialInterpolation.cs +++ /dev/null @@ -1,215 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -using System; -using System.Collections.Generic; - -namespace MathNet.Numerics.Interpolation -{ - /// - /// Barycentric Polynomial Interpolation where the given sample points are equidistant. - /// - /// - /// This algorithm neither supports differentiation nor integration. - /// - public class EquidistantPolynomialInterpolation : IInterpolation - { - /// - /// Internal Barycentric Interpolation - /// - readonly BarycentricInterpolation _barycentric; - - /// - /// Initializes a new instance of the EquidistantPolynomialInterpolation class. - /// - public EquidistantPolynomialInterpolation() - { - _barycentric = new BarycentricInterpolation(); - } - - /// - /// Initializes a new instance of the EquidistantPolynomialInterpolation class. - /// - /// Left bound of the sample point interval. - /// Right bound of the sample point interval. - /// Sample Values x(t) where t is equidistant over [a,b], i.e. x[i] = x(a+(b-a)*i/(n-1)) - public EquidistantPolynomialInterpolation(double leftBound, double rightBound, IList sampleValues) - { - _barycentric = new BarycentricInterpolation(); - Initialize(leftBound, rightBound, sampleValues); - } - - /// - /// Initializes a new instance of the EquidistantPolynomialInterpolation class. - /// - /// Equidistant Sample Points t = a+(b-a)*i/(n-1) - /// Sample Values x(t) where t are equidistant over [a,b], i.e. x[i] = x(a+(b-a)*i/(n-1)) - public EquidistantPolynomialInterpolation(IList samplePoints, IList sampleValues) - { - _barycentric = new BarycentricInterpolation(); - Initialize(samplePoints, sampleValues); - } - - /// - /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). - /// - bool IInterpolation.SupportsDifferentiation - { - get { return false; } - } - - /// - /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). - /// - bool IInterpolation.SupportsIntegration - { - get { return false; } - } - - /// - /// Initialize the interpolation method with the given sampls in the interval [leftBound,rightBound]. - /// - /// Left bound of the sample point interval. - /// Right bound of the sample point interval. - /// Sample Values x(t) where t are equidistant over [a,b], i.e. x[i] = x(a+(b-a)*i/(n-1)) - public void Initialize(double leftBound, double rightBound, IList sampleValues) - { - if (null == sampleValues) - { - throw new ArgumentNullException("sampleValues"); - } - - if (sampleValues.Count < 1) - { - throw new ArgumentOutOfRangeException("sampleValues"); - } - - var samplePoints = new double[sampleValues.Count]; - samplePoints[0] = leftBound; - double step = (rightBound - leftBound)/(samplePoints.Length - 1); - for (int i = 1; i < samplePoints.Length; i++) - { - samplePoints[i] = samplePoints[i - 1] + step; - } - - var weights = EvaluateBarycentricWeights(sampleValues.Count); - - _barycentric.Initialize(samplePoints, sampleValues, weights); - } - - /// - /// Initialize the interpolation method with the given sample set (no sorting assumed). - /// - /// Equidistant Sample Points t = a+(b-a)*i/(n-1) - /// Sample Values x(t) where t are equidistant over [a,b], i.e. x[i] = x(a+(b-a)*i/(n-1)) - public void Initialize(IList samplePoints, IList sampleValues) - { - if (null == sampleValues) - { - throw new ArgumentNullException("sampleValues"); - } - - var weights = EvaluateBarycentricWeights(sampleValues.Count); - _barycentric.Initialize(samplePoints, sampleValues, weights); - } - - /// - /// Evaluate the barycentric weights as used - /// internally by this interpolation algorithm. - /// - /// Count of Sample Values x(t). - /// Barycentric Weight Vector - public static double[] EvaluateBarycentricWeights(int sampleCount) - { - if (sampleCount < 1) - { - throw new ArgumentOutOfRangeException("sampleCount"); - } - - var weights = new double[sampleCount]; - weights[0] = 1.0; - for (int i = 1; i < weights.Length; i++) - { - weights[i] = -(weights[i - 1]*(weights.Length - i))/i; - } - - return weights; - } - - /// - /// Interpolate at point t. - /// - /// Point t to interpolate at. - /// Interpolated value x(t). - public double Interpolate(double t) - { - return _barycentric.Interpolate(t); - } - - /// - /// Differentiate at point t. NOT SUPPORTED. - /// - /// Point t to interpolate at. - /// Interpolated first derivative at point t. - double IInterpolation.Differentiate(double t) - { - throw new NotSupportedException(); - } - - /// - /// Differentiate twice at point t. NOT SUPPORTED. - /// - /// Point t to interpolate at. - /// Interpolated second derivative at point t. - double IInterpolation.Differentiate2(double t) - { - throw new NotSupportedException(); - } - - /// - /// Indefinite integral at point t. NOT SUPPORTED. - /// - /// Point t to integrate at. - double IInterpolation.Integrate(double t) - { - throw new NotSupportedException(); - } - - /// - /// Definite integral between points a and b. NOT SUPPORTED. - /// - /// Left bound of the integration interval [a,b]. - /// Right bound of the integration interval [a,b]. - double IInterpolation.Integrate(double a, double b) - { - throw new NotSupportedException(); - } - } -} diff --git a/src/Numerics/Interpolation/FloaterHormannRationalInterpolation.cs b/src/Numerics/Interpolation/FloaterHormannRationalInterpolation.cs deleted file mode 100644 index 586e5852..00000000 --- a/src/Numerics/Interpolation/FloaterHormannRationalInterpolation.cs +++ /dev/null @@ -1,284 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -using System; -using System.Collections.Generic; - -namespace MathNet.Numerics.Interpolation -{ - /// - /// Barycentric Rational Interpolation without poles, using Mike Floater and Kai Hormann's Algorithm. - /// - /// - /// This algorithm neither supports differentiation nor integration. - /// - public class FloaterHormannRationalInterpolation : IInterpolation - { - /// - /// Internal Barycentric Interpolation - /// - readonly BarycentricInterpolation _barycentric; - - /// - /// Initializes a new instance of the FloaterHormannRationalInterpolation class. - /// - public FloaterHormannRationalInterpolation() - { - _barycentric = new BarycentricInterpolation(); - } - - /// - /// Initializes a new instance of the FloaterHormannRationalInterpolation class. - /// - /// Sample Points t - /// Sample Values x(t) - public FloaterHormannRationalInterpolation(IList samplePoints, IList sampleValues) - { - _barycentric = new BarycentricInterpolation(); - Initialize(samplePoints, sampleValues); - } - - /// - /// Initializes a new instance of the FloaterHormannRationalInterpolation class. - /// - /// 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. - /// - public FloaterHormannRationalInterpolation(IList samplePoints, IList sampleValues, int order) - { - _barycentric = new BarycentricInterpolation(); - Initialize(samplePoints, sampleValues, order); - } - - /// - /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). - /// - bool IInterpolation.SupportsDifferentiation - { - get { return false; } - } - - /// - /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). - /// - bool IInterpolation.SupportsIntegration - { - get { return false; } - } - - /// - /// Initialize the interpolation method with the given sample set. - /// - /// - /// The interpolation scheme order will be set to 3. - /// - /// Sample Points t (no sorting assumed) - /// Sample Values x(t) - public void Initialize(IList samplePoints, IList sampleValues) - { - if (null == samplePoints) - { - throw new ArgumentNullException("samplePoints"); - } - - double[] weights = EvaluateBarycentricWeights(samplePoints, sampleValues, - Math.Min(3, samplePoints.Count - 1)); - - _barycentric.Initialize(samplePoints, sampleValues, weights); - } - - /// - /// Initialize the interpolation method with the given sample set (no sorting assumed). - /// - /// 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. - /// - public void Initialize(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) - { - throw new ArgumentNullException("samplePoints"); - } - - if (null == sampleValues) - { - throw new ArgumentNullException("sampleValues"); - } - - if (samplePoints.Count < 1) - { - throw new ArgumentOutOfRangeException("samplePoints"); - } - - if (samplePoints.Count != sampleValues.Count) - { - throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLength); - } - - if (0 > order || samplePoints.Count <= order) - { - throw new ArgumentOutOfRangeException("order"); - } - - var sortedWeights = new double[sampleValues.Count]; - var sortedPoints = new double[samplePoints.Count]; - samplePoints.CopyTo(sortedPoints, 0); - - // order: odd -> negative, even -> positive - double sign = ((order & 0x1) == 0x1) ? -1.0 : 1.0; - - // init permutation vector - var perm = new int[sortedWeights.Length]; - for (int i = 0; i < perm.Length; i++) - { - perm[i] = i; - } - - // sort and update permutation vector - for (int i = 0; i < perm.Length - 1; i++) - { - for (int j = i + 1; j < perm.Length; j++) - { - if (sortedPoints[j] < sortedPoints[i]) - { - double s = sortedPoints[i]; - sortedPoints[i] = sortedPoints[j]; - sortedPoints[j] = s; - int k = perm[i]; - perm[i] = perm[j]; - perm[j] = k; - } - } - } - - // compute barycentric weights - for (int k = 0; k < sortedWeights.Length; k++) - { - double s = 0; - for (int i = Math.Max(k - order, 0); i <= Math.Min(k, sortedWeights.Length - 1 - order); i++) - { - double v = 1; - for (int j = i; j <= i + order; j++) - { - if (j != k) - { - v = v/Math.Abs(sortedPoints[k] - sortedPoints[j]); - } - } - - s = s + v; - } - - sortedWeights[k] = sign*s; - sign = -sign; - } - - // reorder back to original order, based on the permutation vector. - var weights = new double[sortedWeights.Length]; - for (int i = 0; i < weights.Length; i++) - { - weights[perm[i]] = sortedWeights[i]; - } - return weights; - } - - /// - /// Interpolate at point t. - /// - /// Point t to interpolate at. - /// Interpolated value x(t). - public double Interpolate(double t) - { - return _barycentric.Interpolate(t); - } - - /// - /// Differentiate at point t. NOT SUPPORTED. - /// - /// Point t to interpolate at. - /// Interpolated first derivative at point t. - double IInterpolation.Differentiate(double t) - { - throw new NotSupportedException(); - } - - /// - /// Differentiate twice at point t. NOT SUPPORTED. - /// - /// Point t to interpolate at. - /// Interpolated second derivative at point t. - double IInterpolation.Differentiate2(double t) - { - throw new NotSupportedException(); - } - - /// - /// Indefinite integral at point t. NOT SUPPORTED. - /// - /// Point t to integrate at. - double IInterpolation.Integrate(double t) - { - throw new NotSupportedException(); - } - - /// - /// Definite integral between points a and b. NOT SUPPORTED. - /// - /// Left bound of the integration interval [a,b]. - /// Right bound of the integration interval [a,b]. - double IInterpolation.Integrate(double a, double b) - { - throw new NotSupportedException(); - } - } -} diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 89d46a5a..f253ffc0 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -369,7 +369,6 @@ - @@ -380,9 +379,7 @@ - -