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

itp2 = swapValues(itp,v2): exchange the values to be interpolated to v2, but keeping coefs from itp #130

Closed
floswald opened this issue Oct 27, 2016 · 3 comments

Comments

@floswald
Copy link

I am quite often in the situation that i have several functions f1,f2,... defined on exactly the same domain X. I then have to interpolate each of them at the same point x \in X. Currently, I have to reconstruct an Interpolation object for each interpolation I want to do. This is not efficient, because the coefficients are going to be the same on each of those objects. In other words, this:

julia> f(x) = 3x + x^2
f (generic function with 5 methods)

julia> f2(x) = 3x + x^3
f2 (generic function with 1 method)

julia> domain = 0:0.1:1

julia> v1 = Float64[f(i) for i in domain]
11-element Array{Float64,1}:
 0.0 
 0.31
 0.64
 0.99
 1.36
 1.75
 2.16
 2.59
 3.04
 3.51
 4.0 

julia> v2 = Float64[f2(i) for i in domain];

julia> itp = interpolate(v1, BSpline(Quadratic(Line())), OnCell())
11-element Interpolations.BSplineInterpolation{Float64,1,Array{Float64,1},Interpolations.BSpline{Interpolations.Quadratic{Interpolations.Line}},Interpolations.OnCell,1}:
 0.0 
 0.31
 0.64
 0.99
 1.36
 1.75
 2.16
 2.59
 3.04
 3.51
 4.0 

julia> itp[1.1]
0.03070710674992569

julia> itp2 = interpolate(v2, BSpline(Quadratic(Line())), OnCell())
11-element Interpolations.BSplineInterpolation{Float64,1,Array{Float64,1},Interpolations.BSpline{Interpolations.Quadratic{Interpolations.Line}},Interpolations.OnCell,1}:
 0.0  
 0.301
 0.608
 0.927
 1.264
 1.625
 2.016
 2.443
 2.912
 3.429
 4.0  

julia> itp2[1.1]
0.03002499990621737

# would instead like to do
itp2 = swapValues(itp,v2)

any way this could be added? I see there is already itp.coefs. Can I construct itp2 with those coefficients? thanks.

@sglyon
Copy link
Member

sglyon commented Oct 27, 2016

Hi @floswald I'm not 100% sure if this suits your needs, but to do multi-function interpolation on the same domain you can try to used FixedSizeArrays as hinted at in this comment

@floswald
Copy link
Author

Bingo, that's exactly what I'm looking for. So the answer is that the package already supports interpolating matrices, not only vectors. Documentation is clearly lagging behind with all that's going on here!
I'm not totally sure I get the FixedSizeArray thing though. So I should cast my interpolation data as this Vec type? anyway, just if you have some experience with that, let me know. thanks!

@floswald
Copy link
Author

wait a sec, that's not actually what I need. It would be my use case if the example contained 2 matrices a and aa (for example), both interpolated with the same set of coefficients. But I'm just realizing that what I'm saying makes only sense for linear interpolation, I think. the idea is that the main costly operation when interpolating x in xgrid is to searchsortedlast(xgrid,x) (or similar) to find the bracket in xgrid that contains x. Now the idea was that given I interpolate all functions on the same xgrid at the same point x, I have to do that operation only once. One could just use the weights implied by the position of x within the bracket and multiply with different function values defined at the bracket edges. Possible that this is completely pointless for the approach taken here.

@floswald floswald reopened this Oct 27, 2016
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

No branches or pull requests

2 participants