Matching for interpolation

Is there a matching package that can do something like

V = [1.1,  2,  4.8,  6,  9,  11]
x = 5.1

@match begin

     V[i]  ≤  x  <  V[i+1]     =>     #some interp function of V[i] and V[i+i]


Not that I know of, but you can simply use i = findfirst(>=(x), V) to figure out the indices, given that V is sorted. I guess the rest depends on what kind of interpolation you are looking for. For example a piecewise linear interpolation over the full range can be constructed efficiently with DataInterpolations.jl. Guess I am misunderstanding your aim.

Thanks. yes I could use an interpolation package.
This is for the purpose of sharing the code with others.
I’m wondering how close an interpolation algorithm can look to the math document describing it.

Ah, so it’s mainly for educational purposes.

Isn’t i = findfirst(>=(x), V) (plus a few lines to handle corner cases) as close as it gets to the algorithm step

Find i such that V[i]\leq x\lt V[i+1]

…?

If the vector is known to be in sorted order, you can also use searchsortedlast(V, x).

searchsortedlast(a, x; by=, lt=, rev=false)

Return the index of the last value in a less than or equal to x, according
to the specified order. Return firstindex(a) - 1 if x is less than all
values in a. a is assumed to be sorted.


Something closer to the notation you’re looking for maybe:

julia> findfirst(i -> V[i] ≤ x < V[i+1], eachindex(V)[1:end-1])
3
3 Likes