timholy / Grid.jl

Interpolation and related operations on grids
MIT License
47 stars 26 forks source link

Feature request: BCLinExtrapolate #32

Closed floswald closed 9 years ago

floswald commented 10 years ago

hi!

I thought it would be very useful to have the boundary condition BCLinExtrapolate which would linearly extrapolate the function outside lower and upper bounds, using the first and last two gridpoints respectively to compute this extrapolation.

tomasaschan commented 10 years ago

I think this would be a really cool feature, but unfortunately I also think it would require a quite substantial rewrite of the code that handles evaluation outside the domain. Basically, there are features conceptually similar to this - BCnearest is basically a constant extrapolation, just as BCLinExtrapolate would be a linear one. But the current code doesn't look at it that way, but "cheats" its way around thinking about extrapolation by doing some clever tricks that I've found hard to generalize.

I think I'm going to take a stab at rewriting some of that code (I seem to need that for #31 too). When that's done, this will hopefully be quite simple to do.

timholy commented 10 years ago

Shoot, somehow I didn't notice this earlier.

@tlycken, sounds interesting! Since you're thinking about some redesign, I opened #36 to introduce some other things I've had on my mind.

tomasaschan commented 10 years ago

@timholy Great minds think alike ;) I started typing - and thereby posted - #37 before I saw this, but since they concern different things I think we can probably make good use of both.

@floswald As you can see there might be some waiting before this happens, but I too would find this really useful (linear extrapolation is useful every day to a physicist...) so it will be done eventually. Just don't hold your breath =)

floswald commented 10 years ago

Alright good to hear you are brushing it up a bit. I wrote the extrapolation myself, in one dim it's no big issue.

On Saturday, 19 July 2014, Tomas Lycken notifications@github.com wrote:

@timholy https://github.com/timholy Great minds think alike ;) I started typing - and thereby posted - #37 https://github.com/timholy/Grid.jl/issues/37 before I saw this, but since they concern different things I think we can probably make good use of both.

@floswald https://github.com/floswald As you can see there might be some waiting before this happens, but I too would find this really useful (linear extrapolation is useful every day to a physicist...) so it will be done eventually. Just don't hold your breath =)

— Reply to this email directly or view it on GitHub https://github.com/timholy/Grid.jl/issues/32#issuecomment-49528009.

tomasaschan commented 10 years ago

In higher dimensions (take 2D as an example), we need to make some smart decision on what to do in the "outside corners". This picture might illustrate the problem:

      |      extrap in 1D    | what to do
      |                      | in this part?
-----------------------------------------------------
      |    inner domain      | 
      |  all is fine here    |  extrap in 1D
      |                      |
-----------------------------------------------------
      |                      |

Since we today interpolate in one dimension at a time, it's very possible that that works also for extrapolation - but it might be faster and/or more correct to do something else.

For lack of somewhere better to write it down, I just want to log this thought here so I don't forget to think about it when we start implementing this.

ghost commented 9 years ago

This would be amazing! I would love to be able to use linear extrapolation!

timholy commented 9 years ago

See Interpolate.jl, where most of this already works.

ghost commented 9 years ago

@timholy Interpolate.jl has no documentation and no equivalent to CoordInterpGrid. Is this package no longer being developed? Thanks!

floswald commented 9 years ago

I think Tim meant tlycken/Interpolations.jl?

On Wednesday, 11 March 2015, Gabriel Mihalache notifications@github.com wrote:

@timholy https://github.com/timholy Interpolate.jl has no documentation and no equivalent to CoordInterpGrid. Is this package no longer being developed? Thanks!

— Reply to this email directly or view it on GitHub https://github.com/timholy/Grid.jl/issues/32#issuecomment-78344261.

tomasaschan commented 9 years ago

@gaabriel, @floswald Yes, I think Interpolations.jl is the interesting package here. It's still not on feature-parity with Grid.jl, but if something corresponding to CoordInterpGrid is the deal breaker for you, it should be relatively easy to port the one in Grid.jl to work with Interpolations.jl instead. In fact, I have been meaning to do that myself, but I just haven't gotten to it (and of course, a pull request would be very welcome!).

Regarding documentation, I know it is lacking, but that is partly because the API isn't 100% final yet. There are some examples in this IJulia notebook, and there's plenty of documentation of the implementation details in the doc/latex folder.

ghost commented 9 years ago

Thank you for replying, @tlycken ! Without CoordInterpGrid I would need to always translate between my grid and the integer indices, and I doubt I could do that efficiently. It's an interpolation in itself, albeit trivial conceptually. So it's matter of both convenience and potentially performance, I think.

When I said documentation I meant more like a self-contained example of e.g. multilinear interpolation and extrapolation. For example, in my application I have a function with 2 parameters and another with 3. And I think bilinear and trilinear interpolation and extrapolation is all I need. (This would be interp2 and interpn in matlab.)

Thank you for your time and effort on this!

Edit: Somewhat related, is Interpolation.jl not in the Pkg.add("") mechanism?

tomasaschan commented 9 years ago

To answer your last question first: no, you can't Pkg.add("Interpolations") just yet. (The details are that I haven't tagged a version of Interpolations.jl yet, since I wanted to get a few more features in place first. However, it seems the library is already getting some traction, so I'm seriously considering changing my stance on this, so it might become possible in the near future.) In the meantime, use Pkg.clone() (which will do the same thing, but you'll have to be more specific with the url).

As for examples, did you look at the IJulia notebook I linked to? It seems to me that all the information should already be there - it has all the components you need, albeit perhaps not utilized in exactly the combination you looked for. Something like Interpolation(data, Linear(OnGrid()), ExtrapLinear()), given a multidimensional array data, should give you a multilinear interpolation with linear extrapolation in all directions.

Edit: of course, if you want something like CoordInterpGrid, you would have to port the code from Grid.jl to work with this interpolation object, but I suspect the changes should be trivial.