Implementation of a 1D Interpolation Module
To implement a high-performance 1D interpolation module in fortran_stdlib, I propose a design centered on a unified stdlib_interpolation interface that leverages the fypp preprocessor to ensure type-safety across all supported real kinds (sp, dp, xdp, qp). The core architecture will utilize an $O(\log n)$ bisection-based interval hunting strategy to efficiently locate target coordinates within discretized datasets, preventing the performance degradation typical of linear scans in large-scale scientific simulations. This module will integrate two primary mathematical backends: a piecewise linear routine for rapid $f(x)$ lookups and a more sophisticated cubic spline implementation that ensures $C^2$ continuity across all knots. The cubic spline implementation will require the internal integration of a robust tridiagonal matrix solver, specifically the Thomas Algorithm, to compute the second derivatives at each knot point under specified boundary conditions (e.g., natural or clamped). By embedding this numerical logic directly into stdlib, we provide a "Fortran-native" solution that maintains a clear separation of responsibilities while eliminating the overhead of linking against external libraries like LAPACK for common fitting tasks. This feature directly supports the stdlib mission by providing a scalable, preprocessed framework that can eventually be extended to bilinear and bicubic interpolation for multi-dimensional data arrays.
@jalvesz what's your opinion on this module? Looking for your feedback.
Hi @srinjoy933, the idea is appealing but the text looks like the summary of a first prompt to an LLM. Nothing inherently bad about it but it is rough and needs digging:
What's your current understanding of the state of the art for this in other ecosystems? Numpy, SciPy, scikit-learn, Julia, R, C++? This is important to check common patterns in the consumer facing API, as well as efficient internal implementations. Have you searched for packages in the Fortran ecosystem which propose this to see how they do it?
The text talks about not needing lapack, but stdlib already embeddes a modernized version of blas\lapack as well as allowing linking against optimized libraries such as MKL, OpenBLAS, etc. You might want to ponder this in the proposal. @loiseaujc also established the base to enable tridiagonal matrices and solvers around them. So you might want to try and build on top of that.
Tridiagonal matrices and associated solvers are indeed already provided by the stdlib_specialmatrices module so you could build on top of that. Moreover, as I mentioned here, @jacobwilliams has already modernized a whole bunch of interpolation softwares in Fortran. These include: bspline-fortran, splpak, pchip, or finterp.
Along with doing a survey of what's available in other standard languages/libraries (e.g. numpy, scipy, Julia, etc) to get a feel for what a good API may look like, you could also look at how @jacobwilliams implemented things in Fortran to get some ideas. I don't know exactly which licences @jacobwilliams is using. Maybe you could also discuss with him to see if some parts of his modernized implementations could be re-used directly into stdlib.
@jalvesz Actually I had the idea and some knowledge about how to implement this , but I was not able frame the issue in a more short and to the point manner ,that's why told one LLM model about my idea and what my thinking is , and told to make a issue with the summary of information what i provided to it.
Hi @jalvesz and @loiseaujc, thank you both for the detailed feedback and for pointing me in the right direction! You are completely right, and I appreciate you clarifying the current architecture of stdlib. Good catch on the LAPACK stuff! I completely missed that stdlib already handles the linking. Also, thanks @loiseaujc for pointing out stdlib_specialmatrices.
Looking at how SciPy (scipy.interpolate.CubicSpline or interp1d) and Julia (Interpolations.jl) handle this, the standard consumer-facing API is heavily object-oriented. Usually, a user passes the x and y arrays into a function that returns an "interpolator object". This object calculates and stores the heavy math (like the spline coefficients/derivatives) once. Then, the user can call an evaluation method on that object multiple times for new data points.For Fortran, this means the most efficient API would likely revolve around a derived type (e.g., type(interpolator_1d)) that stores the knots and coefficients, preventing the need to recompute the tridiagonal matrix for every single lookup.
I will definetly check out @jacobwilliams's work (finterp, pchip, etc). Seeing how he structured his modern Fortran implementations is a way better starting point than starting from zero, assuming the licenses match up.
after going through all of this , I will try to implement this module. Again Thank you both for the guidance and also eagerly waiting for any more guidance from your side.
Hello, I’m interested in working on the 1D interpolation module proposal. Before implementing, I would review the existing numerical modules and related prior art, then prepare a small API and testing proposal for maintainer feedback. Is this project still wanted, and would you be open to discussing the expected scope before assignment?
Hi @jalvesz @loiseaujc ,
I've done some initial research on the existing Fortran interpolation libraries and the current stdlib infrastructure.I looked into finterp, bspline-fortran, and PCHIP. One interesting approach I found in finterp is the use of derived types with separate initialize, evaluate, and destroy methods. I think a similar design could work well for a 1D interpolation module, allowing users to initialize an interpolator once and reuse the precomputed coefficients for multiple evaluations.
I also found that stdlib_specialmatrices already provides tridiagonal matrix support, which could be useful when implementing cubic splines instead of introducing a separate solver.Based on this initial research, I think a good starting point would be to implement linear interpolation with a reusable interpolator type, and then extend it to cubic splines with appropriate boundary conditions.For interval searching, I suggest using binary search instead of a linear search. Since the input x-values are sorted, binary search can locate the required interval in O(log n) time rather than O(n), which should make repeated evaluations more efficient for larger datasets.
I'd like to get some feedback on this proposed direction and API before moving forward with the implementation.
Hi @jalvesz and @loiseaujc,
I'd like to take this forward and start working on an initial linear interpolation implementation. I have completed some preliminary research and would now like to develop a small prototype following the existing stdlib architecture.
I also noticed another contributor has expressed interest, so I'd be happy to coordinate to avoid duplicating work. Would you recommend starting with the linear interpolation implementation as the first contribution?
Hi @hassan689, thanks for your interest in looking into this.
Your proposed api based on finterp looks in the right direction. If you have a clear idea on how to start working on it please go ahead an open a draft PR by following the style guide. Do not shy away from looking at what is done on other ecosystems such as numpy, scipy, Julia, etc. to collect ideas on both public API style and internal implementations.
If the other contributors would like to jump in and help pushing it further that would indeed be welcomed, you could coordinate the work in case that happens by building on top of the initial design you would propose for the linear interpolation.
Thanks @jalvesz ! I have opened a draft PR in #1252 with an initial prototype for stdlib_interpolation (1D linear interpolation, unit tests, example, and draft spec).
As suggested, I compared existing designs across Fortran (finterp, PCHIP, bspline-fortran), NumPy (np.interp), SciPy (CubicSpline / PPoly), and Julia (Interpolations.jl):
- Stateful object APIs (
finterp, SciPyPPoly) work best for repeated evaluations because they validatexand precompute piecewise coefficients once, thoughfinterp's subroutine-onlycall s%evaluate(xq, yq)is less convenient in expressions and array calls than function-style evaluation. - Stateless functions (
numpy.interp) are cleaner for one-off lookups without managing an object, while Julia (Interpolations.jl) separates the interpolation scheme from boundary extrapolation behavior.
Based on this, I went with a two-layer hybrid approach in #1252, which seems like the best fit for stdlib:
- An abstract
interp1d_<kind>_typeextended bylinear_interp1d_<kind>_type(withinitialize/destroy, precomputed segment slopes,O(log n)binary search, and function-styleyq = interp%evaluate(xq)for scalars and 1D arrays) so cubic splines (built onstdlib_specialmatrices) and PCHIP can extend the same base interface later. - A one-shot
yq = interp1d(xq, x, y [, extrap])convenience function for single-use lookups, with explicit extrapolation modes (interp_extrap_linear,interp_extrap_clamp,interp_extrap_nan) and optionalstate_typeerror handling.
Looking forward to feedback on the API in #1252, and happy to coordinate with anyone else who wants to build on top of it!