// // Math.NET Numerics, part of the Math.NET Project // http://mathnet.opensourcedotnet.info // // Copyright (c) 2009 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. // namespace MathNet.Numerics.Interpolation.Algorithms { using System; using System.Collections.Generic; /// /// Lagrange Polynomial Interpolation using Neville's Algorithm. /// /// /// /// This algorithm supports differentiation, but doesn't support integration. /// /// /// When working with equidistant or Chebyshev sample points it is /// recommended to use the barycentric algorithms specialized for /// these cases instead of this arbitrary Neville algorithm. /// /// public class NevillePolynomialInterpolation : IInterpolation { /// /// Sample Points t. /// private IList _points; /// /// Spline Values x(t). /// private IList _values; /// /// Initializes a new instance of the NevillePolynomialInterpolation class. /// public NevillePolynomialInterpolation() { } /// /// Initializes a new instance of the NevillePolynomialInterpolation class. /// /// Sample Points t /// Sample Values x(t) public NevillePolynomialInterpolation( IList samplePoints, IList sampleValues) { Initialize(samplePoints, sampleValues); } /// /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). /// /// /// bool IInterpolation.SupportsDifferentiation { get { return true; } } /// /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). /// /// bool IInterpolation.SupportsIntegration { get { return false; } } /// /// Initialize the interpolation method with the given spline coefficients. /// /// Sample Points t /// Sample Values x(t) public void Initialize( IList samplePoints, IList sampleValues) { if (null == samplePoints) { throw new ArgumentNullException("samplePoints"); } if (null == sampleValues) { throw new ArgumentNullException("sampleValues"); } if (samplePoints.Count != sampleValues.Count) { throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths); } _points = samplePoints; _values = sampleValues; } /// /// Interpolate at point t. /// /// Point t to interpolate at. /// Interpolated value x(t). public double Interpolate(double t) { double[] x = new double[_values.Count]; _values.CopyTo(x, 0); for (int level = 1; level < x.Length; level++) { for (int i = 0; i < x.Length - level; i++) { double hp = t - _points[i + level]; double ho = _points[i] - t; double den = _points[i] - _points[i + level]; x[i] = ((hp * x[i]) + (ho * x[i + 1])) / den; } } return x[0]; } /// /// Differentiate at point t. /// /// Point t to interpolate at. /// Interpolated first derivative at point t. /// /// public double Differentiate(double t) { double[] x = new double[_values.Count]; double[] dx = new double[_values.Count]; _values.CopyTo(x, 0); for (int level = 1; level < x.Length; level++) { for (int i = 0; i < x.Length - level; i++) { double hp = t - _points[i + level]; double ho = _points[i] - t; double den = _points[i] - _points[i + level]; dx[i] = ((hp * dx[i]) + x[i] + (ho * dx[i + 1]) - x[i + 1]) / den; x[i] = ((hp * x[i]) + (ho * x[i + 1])) / den; } } return dx[0]; } /// /// Differentiate at point t. /// /// Point t to interpolate at. /// Interpolated value x(t) /// Interpolated second derivative at point t. /// Interpolated first derivative at point t. /// /// public double Differentiate( double t, out double interpolatedValue, out double secondDerivative) { double[] x = new double[_values.Count]; double[] dx = new double[_values.Count]; double[] ddx = new double[_values.Count]; _values.CopyTo(x, 0); for (int level = 1; level < x.Length; level++) { for (int i = 0; i < x.Length - level; i++) { double hp = t - _points[i + level]; double ho = _points[i] - t; double den = _points[i] - _points[i + level]; ddx[i] = ((hp * ddx[i]) + (ho * ddx[i + 1]) + (2 * dx[i]) - (2 * dx[i + 1])) / den; dx[i] = ((hp * dx[i]) + x[i] + (ho * dx[i + 1]) - x[i + 1]) / den; x[i] = ((hp * x[i]) + (ho * x[i + 1])) / den; } } interpolatedValue = x[0]; secondDerivative = ddx[0]; return dx[0]; } /// /// Integrate up to point t. /// /// Right bound of the integration interval [a,t]. /// Interpolated definite integral over the interval [a,t]. /// double IInterpolation.Integrate(double t) { throw new NotSupportedException(); } } }