Dierckx to perform a derivative over a 2d spline

I cannot use Dierckx.jl to perform a derivative over a 2d spline even though from the documentation it seems like it should work.

The help of the function derivative gives:

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?

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

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

and use

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
4 Likes

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

If you can choose the evaluation points yourself, you might consider something like ApproxFun.jl or FastChebInterp.jl

1 Like

Thanks!

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

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.

1 Like

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.

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

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

1 Like