Skip to content

Commit

Permalink
Added Differentiate3 to Interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
osswaldo committed Sep 10, 2024
1 parent f196418 commit 3cc5b65
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Numerics/Interpolation/Barycentric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@ public double Interpolate(double t)
/// <returns>Interpolated second derivative at point t.</returns>
double IInterpolation.Differentiate2(double t) => throw new NotSupportedException();

/// <summary>
/// Differentiate three times at point t. NOT SUPPORTED.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated third derivative at point t.</returns>
double IInterpolation.Differentiate3(double t) => throw new NotSupportedException();

/// <summary>
/// Indefinite integral at point t. NOT SUPPORTED.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ public double Interpolate(double t)
/// <returns>Interpolated second derivative at point t.</returns>
double IInterpolation.Differentiate2(double t) => throw new NotSupportedException();

/// <summary>
/// Differentiate three times at point t. NOT SUPPORTED.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated third derivative at point t.</returns>
double IInterpolation.Differentiate3(double t) => throw new NotSupportedException();

/// <summary>
/// Indefinite integral at point t. NOT SUPPORTED.
/// </summary>
Expand Down
13 changes: 12 additions & 1 deletion src/Numerics/Interpolation/CubicSpline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,18 @@ public double Differentiate2(double t)
{
int k = LeftSegmentIndex(t);
var x = t - _x[k];
return 2*_c2[k] + x*6*_c3[k];
return 2 * _c2[k] + x * 6 * _c3[k];
}

/// <summary>
/// Differentiate three times at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated third derivative at point t.</returns>
public double Differentiate3(double t)
{
int k = LeftSegmentIndex(t);
return 6 * _c3[k];
}

/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Numerics/Interpolation/IInterpolation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public interface IInterpolation
/// <returns>Interpolated second derivative at point t.</returns>
double Differentiate2(double t);

/// <summary>
/// Differentiate three times at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated third derivative at point t.</returns>
double Differentiate3(double t);

/// <summary>
/// Indefinite integral at point t.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Numerics/Interpolation/LinearSpline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ public double Differentiate(double t)
/// <returns>Interpolated second derivative at point t.</returns>
public double Differentiate2(double t) => 0d;

/// <summary>
/// Differentiate three times at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated third derivative at point t.</returns>
public double Differentiate3(double t) => 0d;

/// <summary>
/// Indefinite integral at point t.
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions src/Numerics/Interpolation/LogLinear.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,24 @@ public double Differentiate2(double t)
return secondDerivative;
}

/// <summary>
/// Differentiate three times at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated third derivative at point t.</returns>
public double Differentiate3(double t)
{
var linearFirstDerivative = _spline.Differentiate(t);
var linearSecondDerivative = _spline.Differentiate2(t);
var linearThirdDerivative = _spline.Differentiate3(t);

var thirdDerivative = Differentiate2(t) * linearFirstDerivative +
2 * Differentiate(t) * linearSecondDerivative +
Interpolate(t) * linearThirdDerivative;

return thirdDerivative;
}

/// <summary>
/// Indefinite integral at point t.
/// </summary>
Expand Down
30 changes: 30 additions & 0 deletions src/Numerics/Interpolation/NevillePolynomialInterpolation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,36 @@ public double Differentiate2(double t)
return ddx[0];
}

/// <summary>
/// Differentiate three times at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated third derivative at point t.</returns>
public double Differentiate3(double t)
{
var x = new double[_y.Length];
var dx = new double[_y.Length];
var ddx = new double[_y.Length];
var dddx = new double[_y.Length];
_y.CopyTo(x, 0);

for (int level = 1; level < x.Length; level++)
{
for (int i = 0; i < x.Length - level; i++)
{
double hp = t - _x[i + level];
double ho = _x[i] - t;
double den = _x[i] - _x[i + level];
dddx[i] = ((hp * dddx[i]) + (3 * ddx[i]) + (ho * dddx[i + 1]) - (3 * ddx[i + 1])) / den;
ddx[i] = ((hp * ddx[i]) + (2 * dx[i]) + (ho * ddx[i + 1]) - (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;
}
}

return dddx[0];
}

/// <summary>
/// Indefinite integral at point t. NOT SUPPORTED.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Numerics/Interpolation/QuadraticSpline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ public double Differentiate2(double t)
return 2*_c2[k];
}

/// <summary>
/// Differentiate three times at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated third derivative at point t.</returns>
public double Differentiate3(double t) => 0d;

/// <summary>
/// Indefinite integral at point t.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Numerics/Interpolation/StepInterpolation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ public double Differentiate(double t)
/// <returns>Interpolated second derivative at point t.</returns>
public double Differentiate2(double t) => Differentiate(t);

/// <summary>
/// Differentiate three times at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated third derivative at point t.</returns>
public double Differentiate3(double t) => Differentiate2(t);

/// <summary>
/// Indefinite integral at point t.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Numerics/Interpolation/TransformedInterpolation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ public static TransformedInterpolation Interpolate(
/// <returns>Interpolated second derivative at point t.</returns>
double IInterpolation.Differentiate2(double t) => throw new NotSupportedException();

/// <summary>
/// Differentiate three times at point t. NOT SUPPORTED.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated third derivative at point t.</returns>
double IInterpolation.Differentiate3(double t) => throw new NotSupportedException();

/// <summary>
/// Indefinite integral at point t. NOT SUPPORTED.
/// </summary>
Expand Down

0 comments on commit 3cc5b65

Please sign in to comment.