-
Notifications
You must be signed in to change notification settings - Fork 109
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
why is BSpline() only working for regular grids? #131
Comments
Hey @floswald I would also really like this, but it would require fairly substantial changes. The theoretical derivation of the equations for the (if anyone knows that I've just given bad information please correct me) |
Yes, that's right: someone who wants this will have to sit down and work out the mathematics. |
Here's a systematic solution for this, with symbolic lib derived math and (bad) Julia implementation: Irregular grid + Any order BSplines. I put the code for one dimension here, but multi dimension is just a tensor product for BSplines right? module poly_BSplines using SymEngine export GenBSplines, EvalBSplines function GenBSplines(order, grid)
end function EvalBSplines(x, order, grid, BSplines)
end end |
@wupeifan If you'd like to share the underlying mathematics too, that would be really helpful for putting a solid implementation together :) We have some math docs here but (as so often in open source...) they're definitely not complete. Most useful to you might be the Mathematica notebook Spencer put together. Maybe you could put together something similar for your derivations? Multidimensional spline interpolation should just be a tensor product of 1-dimensional ones, but it would be really helpful (to me, at least) to have some explicit formulation of how this works for irregular grids, as our current implementation pretty heavily assumes a regular grid. And of course, if you'd like to tackle this yourself, a PR is most welcome! You definitely don't have to write a full-fledged implementation with all kinds of boundary conditions etc, but do take care with the interface (function and method names, etc) so it matches well with what already exists in the package, so that it is easy to keep building upon. |
@tlycken Thanks! I think I already tackled it myself - the thing is my implementation in Julia is not that efficient. I'm willing to share the math behind. A PDF file is enough. Currently it is with natural BSplines - I guess you also know that boundary conditions can be altered through putting different construction points of BSpliens. |
Sure, you can either attach it in the issue, (if Github allows it) or put it on e.g. dropbox or google drive and link to it here. The main idea behind I guess there are two parts of the mathematics that I would need to see to be able to help creating an efficient implementation:
|
I'll show you that in the PDF file. What does BC mean? (bad English sorry)
I did it by a dense matrix multiplication in the code above, which means that this implementation is about 100 times slower than its real complexity. Will show you when I have my file ready too. |
Boundary Condition. (Don't worry about it - English is not my first language either 😃 )
What I'm really interested in is how to formulate the mathematics of multidimensional, irregular splines in a way that lets us do it efficiently. As an example, consider linear splines in 2D. It's quite easy to see that the interpolated value at a point
I'm sure this can be generalized first to arbitrarily-dimensional splines, and then to arbitrarily-dimensional grids, to obtain an algorithm similar to what we do for regular grids with the |
@tlycken Here it is, Please tell me whether it is clear or not... I have a bad habit of writing things in an abstract way. For multi dimensional, I think it is a direct tensor product of all the interpolants right? |
@tlycken Let me try to explain better for multidimensional irregular spline case. No matter the grid is evenly spaced or not, spline interpolation is always a weighted average of function values nearby, say 3 or 4 of them. Therefore, we still only need to find where does the variable lies in the box, then tensor product the weights across different dimensions. I don't know whether I clearly put it or not. |
@tlycken If I am not explaining myself well I was wondering whether I can make a pull request? Just want to contribute to this. |
@wupeifan Pull requests are always welcome :) If you do submit one, do try to make your implementation match the style of the APIs of the other interpolation methods (regular-grid BSplines being the most feature-complete example), to make the public API of the library as consistent and usable as possible. If you want help with this, we can probably continue the discussion around specific details in the PR itself. |
@tlycken Cool thanks! I'll do that. I also noticed your latest open issue here, so just let me know who I can talk to when I implement this. @fabgrei I guess you are at the same place as I am, but actually I don't know who you are. If you are interested in this, we can talk to see whether there's some synergy. |
Thank you @fabgrei -- that link is helpful. I think it should give us the info we need to implement the cubic splines here. If anyone wants to tackle this (cc @wupeifan/@cc7768/anyone else who is interested) leave a message here and I'll give you some tips/pointers on how to get started. |
Hi @fabgrei and @sglyon, I would like to contribute some cubic spline code. I have written cubic splines in other languages using the same numerical recipe that @fabgrei has linked, and I also have some Julia code for cubic Akima splines (http://www.alglib.net/interpolation/spline3.php#header5) whose behaviour I generally prefer. I don't have experience contributing to open source projects yet, but I would like to try making a pull request to create |
Thank you, @sp94, for your interest in contributing here! We'd love to have your help. One tricky aspect of this library is that it heavily utilizes meta programming. If you are not familiar with Julia's meta programming facilities (e.g.
On the other hand, if you are already familiar with meta programming then I'd recommend skipping step (2) (or only lightly brushing up on the docs) and starting step (3) a little sooner. I still think it is good idea to implement the routine outside of Interpolations.jl before hand to work through any mathematical uncertainties. |
Hi all, I been examining some of the generated code using macroexpand and either I'm failing to understand the code or there may be inconsistencies between the docstring and generated code variable names.
It seems to me that the generated getindex_impl code uses a different naming convention (see the bottom of the macroexpand() results at the end of this comment):
Have I got my wires totally crossed? If not, this makes it very difficult to interpret the code. I’d like to clear up my understanding before I start trying to understand higher dimensional cases. P.S. I’m new to contributing to OS projects so any advice would be welcome.
Gives the following generated code:
|
@BenWhetton, many thanks for your interest!
Correct. Keep in mind the mathematics focus on just the interval of support (between two adjacent knots of the spline), but the underlying object has to support the entire domain of the array. So
Wow. Looks like you are right. I can't explain how that happened. Yikes.
Really good idea. Best is to test all your ideas in 1d without the fancy apparatus of Interpolations and then generalize it to arbitrary dimensions and orders. |
To all those who expressed interest, this should be much simpler now with the rewrite in #226. No need to figure out all the metaprogramming stuff. |
^bump As a code-illiterate astronomer, I just wanted to thank everyone who has been working on this =D |
Hi, I tried to address this several years ago but I wasn't able to get my head around the metaprogramming at the time. I ended up writing my own snippet which may be helpful to some people landing on this thread |
Hot dang this community is awesome. Thank you, can't wait to try it out! |
Works like a charm, thanks @sp94! For the particular use case I have (essentially trying to replicate this), it would be amazing if we could also do cubic B-splines on irregular grids in Julia. I just noticed that you already discussed this earlier in the thread, so here's hoping! |
Bumping this to signal there is still interest. I have been using cubic B-splines for a while and recently needed to use an irregular grid but was unable to do so. |
The main reason is that no one has implemented it yet. Essentially the scaling would need to be adjusted so that the derivatives match at the knots for BSplines of higher order than linear. A question is how to do this in a performant manner since the basis for this package is to optimize the performance when applied to unit grids or scalings thereof. Another reason is that that there are many methods for implementing irregular grids in multiple dimensions. There is also https://github.com/eljungsk/ScatteredInterpolation.jl |
I was wondering how hard it would be to support irregular grids for
BSpline
interpolation. I am aware that you haveGridded
for this case, however, I often end up missing features ofGridded
whichBSpline
supports. for example, I can't linearly extrapolate a 2D gridded interpolation, whereas I can do that with aBSpline(Quadratic(Free()))
. For many of my applications, irregular grids are necessary.If performance is worse on irregular grids, so be it.
The text was updated successfully, but these errors were encountered: