Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method with option to not allow extrapolation. #995

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Hylke-Atmos
Copy link

Hi @cdrnet

You library is great, however by default the interpolation functions also extrapolate, which is not always desired. Although I know a user is (in most cases) likely able to test possible extrapolation from outside the library, I thought it made a nice addition to the library.

I added a function (to LinearSpline) to which you can provide a flag to indicate whether extrapolation is allowed.

Please let me know if you'd like this addition for the library. It is currently implemented directly in LinearSpline, but I can imagine it might be worth for all interpolation functions. So if you like the idea I can extend it towards the IInterpolation Interface and implement it into all implementations.

Kind Regards,
Hylke

/// <param name="allowExtrapolate">set to fals if extrapolate should not be allowed</param>
/// <returns>Interpolated value x(t).</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when t is outside range and would result in Extrapolation!</exception>
public double Interpolate(double t, bool allowExtrapolate)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a big fan of bool variables in public APIs which act like toggle switches. I would rather create another public function, something like InterpolateWithPossibleExtrapolation, which probably needs to defined on the interface, so every instance can implement it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, it was intended to keep the interface clean and avoid polution of weird functions. Adding an additional function like InterpolateWithoutExtrapolation felt a bit like polluting the clean interface.
Also I didn't want to break any compatibility since the default behavior is currently with possible extrapolation.
To be honest (now I read it again) I can see it isn't making the interface less confusing since the flag name can give the impression that extrapolation is by default not allowed.

As stated in the pull request as well. If you do see potential in the addition of such functionality we can discuss what the most suitable implementation is and how it fits the library best.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi again. I am in favor of having a clean API, and I feel that your proposal is in the right direction.

You are right, the name I suggested should have been InterpolateWithoutExtrapolation

/// <param name="allowExtrapolate">set to fals if extrapolate should not be allowed</param>
/// <returns>Interpolated value x(t).</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when t is outside range and would result in Extrapolation!</exception>
public double Interpolate(double t, bool allowExtrapolate)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That all the interpolators extrapolate by default is possibly surprising behaviour, especially given that is not documented (as far as I can tell).
But I wonder if InterpolateWithoutExtrapolation or the proposed implementation (with allowExtrapolate) will resolve this in general.
Presumably users of the other splines would also want this method which would mean adding this method to all classes and to the interface.

Perhaps a cleaner implementation would be to create a decorator class called NonExtrapolatingSpline (i.e., implements IInterpolation and wraps IInterpolation), that handles this in general for all the interpolators.
Also, maybe doc comments could be added to each of the interpolators indicating that they extrapolate by default, but that this new class exists if anyone wants to opt out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants