//
// 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;
///
/// Third-Degree Spline Interpolation Algorithm.
///
///
/// This algorithm supports both differentiation and integration.
///
public class SplineInterpolation : IInterpolation
{
///
/// Sample Points t.
///
private IList _points;
///
/// Spline Coefficients c(t).
///
private IList _coefficients;
///
/// Number of samples.
///
private int _sampleCount;
///
/// Initializes a new instance of the SplineInterpolation class.
///
public SplineInterpolation()
{
}
///
/// Initializes a new instance of the SplineInterpolation class.
///
/// Sample Points t (length: N), sorted ascending.
/// Spline Coefficients (length: 4*(N-1)).
public SplineInterpolation(
IList samplePoints,
IList splineCoefficients)
{
Initialize(samplePoints, splineCoefficients);
}
///
/// 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 (length: N), sorted ascending.
/// Spline Coefficients (length: 4*(N-1)).
public void Initialize(
IList samplePoints,
IList splineCoefficients)
{
if (null == samplePoints)
{
throw new ArgumentNullException("samplePoints");
}
if (null == splineCoefficients)
{
throw new ArgumentNullException("splineCoefficients");
}
if (samplePoints.Count < 1)
{
throw new ArgumentOutOfRangeException("samplePoints");
}
if (splineCoefficients.Count != 4 * (samplePoints.Count - 1))
{
throw new ArgumentOutOfRangeException("splineCoefficients");
}
_points = samplePoints;
_coefficients = splineCoefficients;
_sampleCount = samplePoints.Count;
}
///
/// Interpolate at point t.
///
/// Point t to interpolate at.
/// Interpolated value x(t).
public double Interpolate(double t)
{
int closestLeftIndex = IndexOfClosestPointLeftOf(t);
// Interpolation
double offset = t - _points[closestLeftIndex];
int k = closestLeftIndex << 2;
return _coefficients[k]
+ (offset * (_coefficients[k + 1]
+ (offset * (_coefficients[k + 2]
+ (offset * _coefficients[k + 3])))));
}
///
/// Differentiate at point t.
///
/// Point t to interpolate at.
/// Interpolated first derivative at point t.
///
///
public double Differentiate(double t)
{
int closestLeftIndex = IndexOfClosestPointLeftOf(t);
// Differentiation
double offset = t - _points[closestLeftIndex];
int k = closestLeftIndex << 2;
return _coefficients[k + 1]
+ (2 * offset * _coefficients[k + 2])
+ (3 * offset * offset * _coefficients[k + 3]);
}
///
/// 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)
{
int closestLeftIndex = IndexOfClosestPointLeftOf(t);
// Differentiation
double offset = t - _points[closestLeftIndex];
int k = closestLeftIndex << 2;
interpolatedValue = _coefficients[k]
+ (offset * (_coefficients[k + 1]
+ (offset * (_coefficients[k + 2]
+ (offset * _coefficients[k + 3])))));
secondDerivative = (2 * _coefficients[k + 2])
+ (6 * offset * _coefficients[k + 3]);
return _coefficients[k + 1]
+ (2 * offset * _coefficients[k + 2])
+ (3 * offset * offset * _coefficients[k + 3]);
}
///
/// 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)
{
int closestLeftIndex = IndexOfClosestPointLeftOf(t);
// Integration
double result = 0;
for (int i = 0, j = 0; i < closestLeftIndex; i++, j += 4)
{
double w = _points[i + 1] - _points[i];
result += w * (_coefficients[j]
+ ((w * _coefficients[j + 1] * 0.5)
+ (w * ((_coefficients[j + 2] / 3)
+ (w * _coefficients[j + 3] * 0.25)))));
}
double offset = t - _points[closestLeftIndex];
int k = closestLeftIndex << 2;
return result + (offset * (_coefficients[k]
+ (offset * _coefficients[k + 1] * 0.5)
+ (offset * _coefficients[k + 2] / 3)
+ (offset * _coefficients[k + 3] * 0.25)));
}
///
/// Find the index of the greatest sample point smaller than t.
///
/// The value to look for.
/// The sample point index.
private int IndexOfClosestPointLeftOf(double t)
{
// Binary search in the [ t[0], ..., t[n-2] ] (t[n-1] is not included)
int low = 0;
int high = _sampleCount - 1;
while (low != high - 1)
{
int middle = (low + high) / 2;
if (_points[middle] > t)
{
high = middle;
}
else
{
low = middle;
}
}
return low;
}
}
}