# Dierckx to perform a derivative over a 2d spline

**URL:** <https://discourse.julialang.org/t/dierckx-to-perform-a-derivative-over-a-2d-spline/96101>\
**Category:** Numerics\
**Tags:** question, interpolations, splines\
**Created:** [March 15, 2023, 3:07am UTC](https://discourse.julialang.org/t/dierckx-to-perform-a-derivative-over-a-2d-spline/96101 "2023-03-15T03:07:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [March 15, 2023, 3:07am UTC](https://discourse.julialang.org/t/dierckx-to-perform-a-derivative-over-a-2d-spline/96101/1 "2023-03-15T03:07:45Z")

</div>

I cannot use `Dierckx.jl` to perform a derivative over a 2d spline even though from the [documentation](https://github.com/kbarbary/Dierckx.jl#2-d-splines) it seems like it should work.

The help of the function `derivative` gives:

```julia
Dierckx.derivative is a Function.

# 10 methods for generic function "derivative":
[1] derivative(spline::Spline1D, x::AbstractVector) in Dierckx at /Users/roiholtzman/.julia/packages/Dierckx/xFcLv/src/Dierckx.jl:339
[2] derivative(spline::Spline1D, x::AbstractVector, nu::Int64) in Dierckx at /Users/roiholtzman/.julia/packages/Dierckx/xFcLv/src/Dierckx.jl:339
[3] derivative(spline::Spline1D, x::Real) in Dierckx at /Users/roiholtzman/.julia/packages/Dierckx/xFcLv/src/Dierckx.jl:344
[4] derivative(spline::Spline1D, x::Real, nu::Int64) in Dierckx at /Users/roiholtzman/.julia/packages/Dierckx/xFcLv/src/Dierckx.jl:344
[5] derivative(spline::ParametricSpline, x::AbstractVector) in Dierckx at /Users/roiholtzman/.julia/packages/Dierckx/xFcLv/src/Dierckx.jl:601
[6] derivative(spline::ParametricSpline, x::AbstractVector, nu::Int64) in Dierckx at /Users/roiholtzman/.julia/packages/Dierckx/xFcLv/src/Dierckx.jl:601
[7] derivative(spline::ParametricSpline, x::Real) in Dierckx at /Users/roiholtzman/.julia/packages/Dierckx/xFcLv/src/Dierckx.jl:608
[8] derivative(spline::ParametricSpline, x::Real, nu::Int64) in Dierckx at /Users/roiholtzman/.julia/packages/Dierckx/xFcLv/src/Dierckx.jl:608
[9] derivative(spline::Spline1D, x; nu) in Dierckx at /Users/roiholtzman/.julia/packages/Dierckx/xFcLv/src/Dierckx.jl:349
[10] derivative(spline::ParametricSpline, x; nu) in Dierckx at /Users/roiholtzman/.julia/packages/Dierckx/xFcLv/src/Dierckx.jl:606

```

So indeed, it cannot get a `Spline2D` object.  
Is there a mistake in the documentation?

Does anyone know if I can get the derivative for a `Spline2D`?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [March 15, 2023, 5:01am UTC](https://discourse.julialang.org/t/dierckx-to-perform-a-derivative-over-a-2d-spline/96101/2 "2023-03-15T05:01:10Z")

</div>

This is something that has been recently merged, and you can use the master branch to perform this. A release hasn’t been tagged yet, as the package doesn’t seem actively maintained.

You may add

```julia
pkg> add https://github.com/kbarbary/Dierckx.jl

```

and use

```julia
julia> using Dierckx

julia> x = Vector{Float64}(1:4); y = Vector{Float64}(1:5); z = (x.^2) * (y.^3)'
4×5 Matrix{Float64}:
  1.0 8.0 27.0 64.0 125.0
  4.0 32.0 108.0 256.0 500.0
  9.0 72.0 243.0 576.0 1125.0
 16.0 128.0 432.0 1024.0 2000.0

julia> s = Spline2D(x, y, z);

julia> derivative(s, 1, 1, nuy=1, nux=0) # 3x^2y^2
3.000000000000028

julia> derivative(s, 1, 1, nuy=0, nux=1) # 2xy^3
1.9999999999999887

julia> derivative(s, 1, 1, nuy=1, nux=1) # 6xy^2
6.000000000000547

```

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [March 15, 2023, 1:33pm UTC](https://discourse.julialang.org/t/dierckx-to-perform-a-derivative-over-a-2d-spline/96101/3 "2023-03-15T13:33:43Z")

</div>

> [@jishnub](#):
>
> A release hasn’t been tagged yet, as the package doesn’t seem actively maintained.

Too bad. Is there a package that has the same functionality that replaces this package?

---

<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:** [March 15, 2023, 1:45pm UTC](https://discourse.julialang.org/t/dierckx-to-perform-a-derivative-over-a-2d-spline/96101/4 "2023-03-15T13:45:46Z")

</div>

> [@roi.holtzman](#):
>
> a derivative over a 2d spline

If you can choose the evaluation points yourself, you might consider something like [ApproxFun.jl](https://github.com/JuliaApproximation/ApproxFun.jl) or [FastChebInterp.jl](https://github.com/stevengj/FastChebInterp.jl)

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [March 17, 2023, 12:19am UTC](https://discourse.julialang.org/t/dierckx-to-perform-a-derivative-over-a-2d-spline/96101/5 "2023-03-17T00:19:10Z")

</div>

Thanks!

You mean that if I have a given grid of points in x,y, I cannot use these packages?

---

<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:** [March 17, 2023, 12:27am UTC](https://discourse.julialang.org/t/dierckx-to-perform-a-derivative-over-a-2d-spline/96101/6 "2023-03-17T00:27:31Z")

</div>

You can, but for an arbitrarily given grid you need to use Chebyshev regression rather than interpolation, which requires far more points to achieve the same accuracy.

The advantage of evaluating your function at special points — a Chebyshev grid — is that then you can get exponentially good accuracy (“spectral” convergence) for smooth functions.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [March 17, 2023, 11:55am UTC](https://discourse.julialang.org/t/dierckx-to-perform-a-derivative-over-a-2d-spline/96101/7 "2023-03-17T11:55:48Z")

</div>

> [@roi.holtzman](#):
>
> You mean that if I have a given grid of points in x,y I cannot use these packages?

You may fit a spline and pass that to `ApproxFun`. ApproxFun will then generate a Chebyshev approximation to the spline. This should usually be fine if your function is smooth. However, note that in this, you’re interpolating your data on a Chebyshev grid and fitting the interpolated data, so this may introduce extra errors that you need to consider. Whether that matters or not depends on your application.

---

<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:** [March 17, 2023, 12:39pm UTC](https://discourse.julialang.org/t/dierckx-to-perform-a-derivative-over-a-2d-spline/96101/8 "2023-03-17T12:39:26Z")

</div>

> [@jishnub](#):
>
> You may fit a spline and pass that to `ApproxFun`. ApproxFun will then generate a Chebyshev approximation to the spline.

This is a bad idea. A spline is non-smooth so Chebyshev interpolation will be very inefficient.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [March 20, 2023, 11:24am UTC](https://discourse.julialang.org/t/dierckx-to-perform-a-derivative-over-a-2d-spline/96101/9 "2023-03-20T11:24:08Z")

</div>

Update: A release has been tagged now, so the issue in the original post is fixed.
