Browse Source

Add CubicSplineMonotone to Interpolate.

v4
Febin 6 years ago
parent
commit
e3cf8d12f5
  1. 23
      src/Numerics/Interpolate.cs
  2. 6
      src/Numerics/Interpolation/CubicSpline.cs

23
src/Numerics/Interpolate.cs

@ -201,7 +201,7 @@ namespace MathNet.Numerics
} }
/// <summary> /// <summary>
/// Create an piecewise cubic Akima spline interpolation based on arbitrary points. /// Create a piecewise cubic Akima spline interpolation based on arbitrary points.
/// Akima splines are robust to outliers. /// Akima splines are robust to outliers.
/// </summary> /// </summary>
/// <param name="points">The sample points t.</param> /// <param name="points">The sample points t.</param>
@ -221,6 +221,27 @@ namespace MathNet.Numerics
return Interpolation.CubicSpline.InterpolateAkima(points, values); return Interpolation.CubicSpline.InterpolateAkima(points, values);
} }
/// <summary>
/// Create a piecewise cubic monotone spline interpolation based on arbitrary points.
/// This is a shape-preserving spline with continuous first derivative.
/// </summary>
/// <param name="points">The sample points t.</param>
/// <param name="values">The sample point values x(t).</param>
/// <returns>
/// An interpolation scheme optimized for the given sample points and values,
/// which can then be used to compute interpolations and extrapolations
/// on arbitrary points.
/// </returns>
/// <remarks>
/// if your data is already sorted in arrays, consider to use
/// MathNet.Numerics.Interpolation.CubicSpline.InterpolatePchipSorted
/// instead, which is more efficient.
/// </remarks>
public static IInterpolation CubicSplineMonotone(IEnumerable<double> points, IEnumerable<double> values)
{
return Interpolation.CubicSpline.InterpolatePchip(points, values);
}
/// <summary> /// <summary>
/// Create a piecewise cubic Hermite spline interpolation based on arbitrary points /// Create a piecewise cubic Hermite spline interpolation based on arbitrary points
/// and their slopes/first derivative. /// and their slopes/first derivative.

6
src/Numerics/Interpolation/CubicSpline.cs

@ -244,7 +244,9 @@ namespace MathNet.Numerics.Interpolation
var mIs0 = m[i].AlmostEqual(0.0); var mIs0 = m[i].AlmostEqual(0.0);
if (mIs0 || mPrevIs0 || Math.Sign(m[i]) != Math.Sign(m[i - 1])) if (mIs0 || mPrevIs0 || Math.Sign(m[i]) != Math.Sign(m[i - 1]))
{
dd[i] = 0; dd[i] = 0;
}
else else
{ {
// Weighted harmonic mean of each slope. // Weighted harmonic mean of each slope.
@ -272,10 +274,14 @@ namespace MathNet.Numerics.Interpolation
var d = ((2 * h0 + h1) * m0 - h0 * m1) / (h0 + h1); var d = ((2 * h0 + h1) * m0 - h0 * m1) / (h0 + h1);
if (Math.Sign(d) != Math.Sign(m0)) if (Math.Sign(d) != Math.Sign(m0))
{
return 0.0; return 0.0;
}
if (Math.Sign(m0) != Math.Sign(m1) && (Math.Abs(d) > 3 * Math.Abs(m0))) if (Math.Sign(m0) != Math.Sign(m1) && (Math.Abs(d) > 3 * Math.Abs(m0)))
{
return 3 * m0; return 3 * m0;
}
return d; return d;
} }

Loading…
Cancel
Save