Skip to content

Evaluation on grid points does not match the input exactly #207

Closed

Description

@linwaytin

Hi there,

First, thanks for this fabulous work, which is indeed fast and contains very useful functionalities, like PCHIP and periodic BCs.

The issue I ran into is like this:

x = -1:0.1:1;
y = rand(length(x));
itp = pchip_interp(x, y);
itp.(x) - y

The difference contains several small (~1e-16) but nonzero numbers.
I also check cubic_interp and the result is worse. itp.(x) and y are different in general.

This should not be an issue in practice, but I would expect a polynomial interpolator to generate exactly the same result as the input data. Am I wrong or there is some subtle issue in the code?

Activity

  1. mgyoo86 commented on Sep 30, 2026

    @mgyoo86
    Member

    Hi @linwaytin,
    Thanks for the kind words and the report!

    In short, this is expected and intended behavior.
    The node values may differ only in the last few digits, which is a fair (and largely unavoidable) trade-off for performance.
    Other packages like Interpolations.jl and DataInterpolations.jl have the same issues.

    In particular, Range grids (e.g., -1:0.1:1) are fundamentally harder.
    Node positions aren't stored; they're computed at everytime we request value, and it may differ depending on the internal definition/implementaion of Range.
    That computation can introduce small round-off, so a node position value may differ slightly.
    (FastInterpolations internally redefines the Range in a simpler way for the performance)
    Nevertheless, if your grid is uniform, I recommend keeping it as a range, since it's much faster.

    If you really need exact node values, consider a Vector grid (e.g., x = collect(-1:0.1:1)).
    Keep in mind that Vector grids are generally slower, and some methods may still not match exactly even there, due to floating-point rounding in the evaluation.

    One general tip:
    if you have many query points (xq), try the batch query form of itp(xq) instead of broadcasting form of itp.(xq).
    FastInterpolations has optimizations specific to batch queries, so itp(xq) is usually faster.

    I just noticed that, however, in the case of (pchip + range grid), the batch query form is slightly slower than broadcasting form.
    This is probably because pchip is not fully optimized yet.
    I will look into it and make it faster.
    Thanks again for the report!

  2. linwaytin commented on Sep 30, 2026

    @linwaytin
    Author

    Thank you so much for the detailed explanation!

    As you said, this is not a real issue, but just for my curiosity, I did some experiments. Indeed using Vector largely improves the consistency (and again pchip_interp is much better than cubic_interp), but it can still happen, especially at the end points.

    Thanks for your tip on the performance!

  3. mgyoo86 commented on Sep 30, 2026

    @mgyoo86
    Member

    Yes, thanks to this isssue, I noticed some edge cases and room for improvements.
    I'll try to fix issues and improve performance in the near future!

    Please let me know if you find any other issues or have new ideas/suggestions.
    I'll be happy to take a look :)

  4. mgyoo86 commented on Oct 7, 2026

    @mgyoo86
    Member

    Hi @linwaytin,

    Just a quick update: PR #211 has resolved the inconsistency in cubic interpolation.
    Now, Vector grid gives the exact nodal values (except for the last node; last nodal value may still have round-off errors)
    The cubic kernel improvements address the accuracy issue and, as a bonus, also improve performance :)

  5. linwaytin commented on Oct 8, 2026

    @linwaytin
    Author

    That is awesome! Thanks for the update.

  6. mgyoo86 commented on Oct 14, 2026

    @mgyoo86
    Member

    @linwaytin
    FYI, the new version (v0.4.20) has just been released.
    Please give it a try and let me know if you find any further issues :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions