diff --git a/src/Managed/Interpolation/Algorithms/CubicHermiteSplineInterpolation.cs b/src/Managed/Interpolation/Algorithms/CubicHermiteSplineInterpolation.cs new file mode 100644 index 00000000..5d5c203c --- /dev/null +++ b/src/Managed/Interpolation/Algorithms/CubicHermiteSplineInterpolation.cs @@ -0,0 +1,192 @@ +// +// 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; + + /// + /// Cubic Hermite Spline Interpolation Algorithm. + /// + /// + /// This algorithm supports both differentiation and integration. + /// + public class CubicHermiteSplineInterpolation : IInterpolation + { + /// + /// Internal Spline Interpolation + /// + private readonly SplineInterpolation _spline; + + /// + /// Initializes a new instance of the CubicHermiteSplineInterpolation class. + /// + public CubicHermiteSplineInterpolation() + { + _spline = new SplineInterpolation(); + } + + /// + /// Initializes a new instance of the CubicHermiteSplineInterpolation class. + /// + /// Sample Points t, sorted ascending. + /// Sample Values x(t) + /// Sample Derivatives x'(t) + public CubicHermiteSplineInterpolation( + IList samplePoints, + IList sampleValues, + IList sampleDerivatives) + { + _spline = new SplineInterpolation(); + Initialize(samplePoints, sampleValues, sampleDerivatives); + } + + /// + /// 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 true; } + } + + /// + /// Initialize the interpolation method with the given spline coefficients (sorted by the sample points t). + /// + /// Sample Points t, sorted ascending. + /// Sample Values x(t) + /// Sample Derivatives x'(t) + public void Initialize( + IList samplePoints, + IList sampleValues, + IList sampleDerivatives) + { + if (null == samplePoints) + { + throw new ArgumentNullException("samplePoints"); + } + + if (null == sampleValues) + { + throw new ArgumentNullException("sampleValues"); + } + + if (null == sampleDerivatives) + { + throw new ArgumentNullException("sampleDerivatives"); + } + + if (samplePoints.Count < 2) + { + throw new ArgumentOutOfRangeException("samplePoints"); + } + + if (samplePoints.Count != sampleValues.Count + || samplePoints.Count != sampleDerivatives.Count) + { + throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths); + } + + double[] coefficients = new double[4 * (samplePoints.Count - 1)]; + + for (int i = 0, j = 0; i < samplePoints.Count - 1; i++, j += 4) + { + double delta = samplePoints[i + 1] - samplePoints[i]; + double delta2 = delta * delta; + double delta3 = delta * delta2; + coefficients[j] = sampleValues[i]; + coefficients[j + 1] = sampleDerivatives[i]; + coefficients[j + 2] = ((3 * (sampleValues[i + 1] - sampleValues[i])) - (2 * sampleDerivatives[i] * delta) - (sampleDerivatives[i + 1] * delta)) / delta2; + coefficients[j + 3] = ((2 * (sampleValues[i] - sampleValues[i + 1])) + (sampleDerivatives[i] * delta) + (sampleDerivatives[i + 1] * delta)) / delta3; + } + + _spline.Initialize(samplePoints, coefficients); + } + + /// + /// Interpolate at point t. + /// + /// Point t to interpolate at. + /// Interpolated value x(t). + public double Interpolate(double t) + { + return _spline.Interpolate(t); + } + + /// + /// Differentiate at point t. + /// + /// Point t to interpolate at. + /// Interpolated first derivative at point t. + /// + /// + public double Differentiate(double t) + { + return _spline.Differentiate(t); + } + + /// + /// 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) + { + return _spline.Differentiate(t, out interpolatedValue, out secondDerivative); + } + + /// + /// Integrate up to point t. + /// + /// Right bound of the integration interval [a,t]. + /// Interpolated definite integral over the interval [a,t]. + /// + public double Integrate(double t) + { + return _spline.Integrate(t); + } + } +} \ No newline at end of file diff --git a/src/Managed/Managed.csproj b/src/Managed/Managed.csproj index 66ca14aa..fc479e53 100644 --- a/src/Managed/Managed.csproj +++ b/src/Managed/Managed.csproj @@ -56,6 +56,7 @@ + diff --git a/src/Native/Native.csproj b/src/Native/Native.csproj index af0990e7..b3f9dba6 100644 --- a/src/Native/Native.csproj +++ b/src/Native/Native.csproj @@ -77,6 +77,9 @@ Interpolation\Algorithms\BulirschStoerRationalInterpolation.cs + + Interpolation\Algorithms\CubicHermiteSplineInterpolation.cs + Interpolation\Algorithms\FloaterHormannRationalInterpolation.cs