Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Numerics.Tests/InterpolationTests/LinearSplineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,31 @@ public void FewSamples()
Assert.That(() => LinearSpline.Interpolate(new double[1], new double[1]), Throws.ArgumentException);
Assert.That(LinearSpline.Interpolate(new[] { 1.0, 2.0 }, new[] { 2.0, 2.0 }).Interpolate(1.0), Is.EqualTo(2.0));
}

[TestCase(-2.4, .6, 1.5, 1e-15)]
[TestCase(-0.9, 1.7, 1.0, 1e-15)]
[TestCase(-0.5, .5, 1.0, 1e-15)]
[TestCase(-0.1, -.7, 1.0, 1e-15)]
[TestCase(0.1, -.9, 1.0, 1e-15)]
[TestCase(0.4, -.6, 1.0, 1e-15)]
[TestCase(1.2, .2, 1.0, 1e-15)]
[TestCase(10.0, 9.0, double.MaxValue, 1e-15)]
[TestCase(-10.0, -7.0, double.MaxValue, 1e-15)]
public void DeltaTCommonPoints(double t, double x, double maxDeltaT, double maxAbsoluteError)
{
LinearSpline ip = LinearSpline.Interpolate(_t, _y);
Assert.AreEqual(x, ip.Interpolate(t, maxDeltaT), maxAbsoluteError, "Interpolation at {0}", t);
}

[TestCase(0.1, -.9, 0.2)]
[TestCase(0.4, -.6, .2)]
[TestCase(1.2, .2, .2)]
[TestCase(10.0, 9.0, .2)]
[TestCase(-10.0, -7.0, .2)]
public void DeltaTPointTooNarrow(double t, double x, double maxDeltaT)
{
LinearSpline ip = LinearSpline.Interpolate(_t, _y);
Assert.Throws<InterpolatingDistanceException>(() => ip.Interpolate(t, maxDeltaT));
}
}
}
28 changes: 28 additions & 0 deletions src/Numerics/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,32 @@ protected SingularUMatrixException(System.Runtime.Serialization.SerializationInf
{
}
}

/// <summary>
/// Distance between point 't' and reference points is too large (can lead to inaccurate interpolation).
/// </summary>
[Serializable]
public class InterpolatingDistanceException : Exception

{
public InterpolatingDistanceException()
: base("Distace from 't' to the two closest points exceeds MaxDeltaT.")
{
}

public InterpolatingDistanceException(string message)
: base(message)
{
}

public InterpolatingDistanceException(string message, Exception innerException)
: base(message, innerException)
{
}

protected InterpolatingDistanceException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
: base(info, context)
{
}
}
}
23 changes: 22 additions & 1 deletion src/Numerics/Interpolation/LinearSpline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,30 @@ public static LinearSpline Interpolate(IEnumerable<double> x, IEnumerable<double
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated value x(t).</returns>
public double Interpolate(double t)
{
return Interpolate(t, double.MaxValue);
}

/// <summary>
/// Interpolate at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <param name="maxDeltaT">Maximum allowed delta between point 't' and reference points.</param>
/// <returns>Interpolated value x(t).</returns>
/// <exception cref="InterpolatingDistanceException">Thrown when distance from xn or xn+1 to 't' is exceeding <paramref name="maxDeltaT"/>.</exception>
public double Interpolate(double t, double maxDeltaT)
{
int k = LeftSegmentIndex(t);
return _c0[k] + (t - _x[k])*_c1[k];

if (Math.Abs(t - _x[k]) > maxDeltaT)
{
throw new InterpolatingDistanceException("Lower bound point exceeds maxDetlaT.");
}
else if (Math.Abs(_x[k + 1] - t) > maxDeltaT)
{
throw new InterpolatingDistanceException("Upper bound point exceeds maxDetlaT.");
}
return _c0[k] + (t - _x[k]) * _c1[k];
}

/// <summary>
Expand Down