# Cubic interpolation

**URL:** https://discourse.julialang.org/t/cubic-interpolation/34552
**Category:** General Usage
**Created:** [February 13, 2020, 3:28am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552 "2020-02-13T03:28:32Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [February 13, 2020, 3:28am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/1 "2020-02-13T03:28:32Z")

</div>

Interpolations.jl: The API may be clever, but it isn’t easy to use. How do I interpolate a set of points with a cubic curve?

I tried this:

```julia
 npts = size(xyz,1);
    s = fill(0.0, npts);
    for j in 2:npts
        s[j] = s[j-1]+norm(xyz[j,:]-xyz[j-1,:]);
    end 
    CubicSplineInterpolation((s,), xyz[:, 1])

```

so that the curve would be parameterized by the distance between the control points. No go:

```julia
julia> CubicSplineInterpolation((s,), xyz[:, 1])                                                                                                                                                      
ERROR: MethodError: no method matching CubicSplineInterpolation(::Tuple{Array{Float64,1}}, ::Array{Float64,1})                                                                                            
Closest candidates are:                                                                                                                                                                                   
  CubicSplineInterpolation(::AbstractRange, ::AbstractArray{T,1} where T; bc, extrapolation_bc) at C:\Users\PKrysl\.julia\packages\Interpolations\9sq9m\src\convenience-constructors.jl:8                 
  CubicSplineInterpolation(::Tuple{Vararg{AbstractRange,N}}, ::AbstractArray{T,N}; bc, extrapolation_bc) where {N, T} at C:\Users\PKrysl\.julia\packages\Interpolations\9sq9m\src\convenience-constructors.jl:19                                                                                                                                                                                                    
Stacktrace:                                                                                                                                                                                               
 [1] top-level scope at REPL[25]:1  

```

If I replace cubic interp with `LinearInterpolation`, it works.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [February 13, 2020, 3:44am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/2 "2020-02-13T03:44:15Z")

</div>

There is also [GitHub - slabanja/SimplePCHIP: Simple monotone piecewise cubic interpolation](https://github.com/slabanja/SimplePCHIP).

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 13, 2020, 3:48am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/3 "2020-02-13T03:48:05Z")

</div>

I think Interpolations.jl only supports unequally spaced points for linear interpolation?

You might try [https://github.com/kbarbary/Dierckx.jl](https://github.com/kbarbary/Dierckx.jl)

SimplePCHIP might be a good reference for a Julia Hermite interpolation package, but it is a bit out of date and the implementation looks rather Pythonic (not very type generic, and some potential performance problems).

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [February 13, 2020, 3:50am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/4 "2020-02-13T03:50:39Z")

</div>

Thanks for the hint, I tried to browse through the tests and it looks like you are right.  
It doesn’t work for cubic curves.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [February 13, 2020, 4:13am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/5 "2020-02-13T04:13:58Z")

</div>

> [@stevengj](#):
>
> You might try [https://github.com/kbarbary/Dierckx.jl](https://github.com/kbarbary/Dierckx.jl)

Beautiful! Why can’t Interpolations.jl be so simple to use?

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [February 13, 2020, 4:29am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/6 "2020-02-13T04:29:53Z")

</div>

Thanks. This is good to know, but for my present purpose the PCHIP is too flat inbetween the control points. I really do prefer the regular spline in this case.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [February 13, 2020, 6:00am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/7 "2020-02-13T06:00:13Z")

</div>

> **[GitHub - SciML/DataInterpolations.jl: A library of data interpolation and...](https://github.com/SciML/DataInterpolations.jl)**
>
> A library of data interpolation and smoothing functions - GitHub - SciML/DataInterpolations.jl: A library of data interpolation and smoothing functions

[https://htmlpreview.github.io/?https://github.com/UMCTM/DataInterpolations.jl/blob/master/Example/DataInterpolations.html](https://htmlpreview.github.io/?https://github.com/UMCTM/DataInterpolations.jl/blob/master/Example/DataInterpolations.html)

---

<div class="post-metadata">

### Author: ![braamvandyk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/braamvandyk/32/5086_2.png) [@braamvandyk](https://discourse.julialang.org/u/braamvandyk)
#### Post date: [February 13, 2020, 6:25am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/8 "2020-02-13T06:25:10Z")

</div>

Dierckx.jl? It is an easy to use package, but I don’t know about unequally spaced data.

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [February 13, 2020, 11:40am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/9 "2020-02-13T11:40:08Z")

</div>

I agree that Interpolations.jl should be reworked to be easier to use. It could really use a dedicated maintainer. It’s a nicely-isolated package (it doesn’t require you to maintain an entire ecosystem), mathematically interesting, and has huge implications for the community. Volunteers wanted!

---

<div class="post-metadata">

### Author: ![kevinczimmerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevinczimmerman/32/13040_2.png) [@kevinczimmerman](https://discourse.julialang.org/u/kevinczimmerman)
#### Post date: [February 13, 2020, 12:58pm UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/10 "2020-02-13T12:58:00Z")

</div>

I would like to contribute more to the Julia community but I’m not sure I would be qualified to be a maintainer. I wouldn’t mind focusing my extra time on contributing to the package rework though.

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [February 13, 2020, 1:08pm UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/11 "2020-02-13T13:08:25Z")

</div>

That would be awesome. I’ve been trying to review PRs to Interpolations when I can find the time; if you submit a few, not only will you be helping make it better but you’ll be in training for having more authority over the direction of the package.

---

<div class="post-metadata">

### Author: ![kevinczimmerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevinczimmerman/32/13040_2.png) [@kevinczimmerman](https://discourse.julialang.org/u/kevinczimmerman)
#### Post date: [February 13, 2020, 2:24pm UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/12 "2020-02-13T14:24:35Z")

</div>

Should I start by tackling issues or do you have a vision/roadmap for rework?

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [February 13, 2020, 3:46pm UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/13 "2020-02-13T15:46:20Z")

</div>

If there are some small issues easy to fix, that might be a great place to start.

My own vision looks something like this:

- better documentation. Right now I think the documentation has been written by the same people who wrote the code, and that usually gives mixed results.  
The conundrum is that one could document the API and then it might change, so it’s a bit of a question of how much effort to throw in here.  
One fairly safe step might be to improve documenting the internals in a manner that teaches how the package works. (Obviously start by checking the current writeups and seeing whether you think they’re adequate or not; maybe it’s better than I think.) There are certain parts of the package that I think are solid, notably the [WeightedIndex](https://github.com/JuliaMath/Interpolations.jl/blob/bd399254dbb0e77d4a779b7cd9d46b78aedff08b/src/Interpolations.jl#L176-L231) infrastructure and probably the prefiltering. The closer one gets to the user level, though, the more likely I think some of it will change in the long run.
- check to see if the benchmarks work and whether we need more. See if work needs to be done to make running them easy, possibly as part of CI. This would allow us to detect whether future changes cause performance regressions.
- start thinking about reworking the API. Can we take useful inspiration from other interpolation packages? Can we leverage inferrable keyword arguments (which Julia didn’t have when Interpolations was designed) to deliver a more convenient interface? The whole `OnGrid`/`OnCell` issue has added a lot of complexity, can that be simplified? [https://github.com/JuliaMath/Interpolations.jl/issues/227](https://github.com/JuliaMath/Interpolations.jl/issues/227) and [https://github.com/JuliaMath/Interpolations.jl/issues/242](https://github.com/JuliaMath/Interpolations.jl/issues/242) represent probably the biggest architectural changes but both serve as roadmaps for important changes.

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [April 12, 2020, 6:32pm UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/14 "2020-04-12T18:32:55Z")

</div>

It’s official: [Interpolations.jl needs a new maintainer](https://discourse.julialang.org/t/interpolations-jl-needs-a-new-maintainer/37454)

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [September 13, 2020, 2:47am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/15 "2020-09-13T02:47:02Z")

</div>

[quote

> [@ChrisRackauckas](#):
>
> PumasAI/DataInterpolations.jl

I found this package is really neat. Is there any plan to add derivative and integration functions just like provided in Dierckx.jl? @ChrisRackauckas

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [September 13, 2020, 5:32am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/16 "2020-09-13T05:32:48Z")

</div>

> [@liuyxpp](#):
>
> I found this package is really neat. Is there any plan to add derivative and integration functions just like provided in Dierckx.jl? @ChrisRackauckas

> <https://github.com/SciML/DataInterpolations.jl/pull/72>
>
> \# TODOs:
> \- \[x\] Implement derivatives for all the interpolation methods
> \- \[x\] I…ntegrate with ChainRulesCore
> \- \[x\] Verify correctness using FiniteDifferences
> 
> Follow up from https://github.com/SciML/DiffEqFlux.jl/pull/380

---

<div class="post-metadata">

### Author: ![jovansam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jovansam/32/15023_2.png) [@jovansam](https://discourse.julialang.org/u/jovansam)
#### Post date: [October 2, 2020, 12:54pm UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/17 "2020-10-02T12:54:20Z")

</div>

@ChrisRackauckas. I have some questions. Liking DataInterpolations.jl a lot.

1. Does it support extrapolation?
2. Are there plans for multidimensional extrapolation/interpolation?
3. Relative to other interpolation packages, why switch the positions of x,y (see below)?

```julia
using DataInterpolations
x = collect(1:0.1:5)
y = rand(length(x))
itp1 = LinearInterpolation(y, x)
@show itp1(4)
@show itp1(6)

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [October 24, 2020, 6:31am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/18 "2020-10-24T06:31:26Z")

</div>

> [@jovansam](#):
>
> Does it support extrapolation?

I believe it does now.

> [@jovansam](#):
>
> Are there plans for multidimensional extrapolation/interpolation?

Not really: we didn’t need it for our application.

> [@jovansam](#):
>
> Relative to other interpolation packages, why switch the positions of x,y (see below)?

It matches the differential equation solver, so the convention is “correct” 😛. Yeah… that’s how conventions go.

---

<div class="post-metadata">

### Author: ![jovansam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jovansam/32/15023_2.png) [@jovansam](https://discourse.julialang.org/u/jovansam)
#### Post date: [October 24, 2020, 6:39am UTC](https://discourse.julialang.org/t/cubic-interpolation/34552/19 "2020-10-24T06:39:42Z")

</div>

Thanks @ChrisRackauckas. Great package.
