# Gradient of 2D splines on irregular grid

**URL:** <https://discourse.julialang.org/t/gradient-of-2d-splines-on-irregular-grid/87575>\
**Category:** General Usage\
**Tags:** spline, interpolations, forwarddiff, gradient\
**Created:** [September 21, 2022, 2:12pm UTC](https://discourse.julialang.org/t/gradient-of-2d-splines-on-irregular-grid/87575 "2022-09-21T14:12:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Mikaelsen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thomas_mikaelsen/32/42736_2.png) [@Thomas\_Mikaelsen](https://discourse.julialang.org/u/Thomas_Mikaelsen)\
**Post date:** [September 21, 2022, 2:12pm UTC](https://discourse.julialang.org/t/gradient-of-2d-splines-on-irregular-grid/87575/1 "2022-09-21T14:12:24Z")

</div>

Hello.

I’m looking to (1) interpolate data where the x-axis is a log-spaced grid, the y-axis is a regular spaced grid and there is an array A(x,y) recording the function values in each point; and (2) take the gradient of the resulting interpolated function.

I’ve been looking at different packages, e.g. Dierckx and Interpolations. But Dierckx doesn’t support gradients above 1 dimension and Interpolations only support linear interpolations for irregular grids, and hence the derivatives will not be well-defined.

Is there a different package that will let me do the above?

Thanks alot.

---

<div class="post-metadata">

**Author:** ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)\
**Post date:** [September 21, 2022, 2:17pm UTC](https://discourse.julialang.org/t/gradient-of-2d-splines-on-irregular-grid/87575/2 "2022-09-21T14:17:30Z")

</div>

You could use a (smoothing) interpolation on a sufficiently fine numerical grid (e.g. [GitHub - gher-uliege/DIVAnd.jl: DIVAnd performs an n-dimensional variational analysis of arbitrarily located observations](https://github.com/gher-uliege/DIVAnd.jl)) and then just perform a simple finite difference on this high-resolution grid.

---

<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:** [September 21, 2022, 2:29pm UTC](https://discourse.julialang.org/t/gradient-of-2d-splines-on-irregular-grid/87575/3 "2022-09-21T14:29:33Z")

</div>

> [@Thomas\_Mikaelsen](#):
>
> I’m looking to (1) interpolate data where the x-axis is a log-spaced grid, the y-axis is a regular spaced grid and there is an array A(x,y) recording the function values in each point; and (2) take the gradient of the resulting interpolated function.

In other words, x = \exp(z) where z = \log(x) is on a regularly spaced grid? So then you can use a method for a regular grid in z,y to interpolate the function B(z,y), from which you can compute A(x,y) = B(e^z, y) and hence \nabla A = ((1/x) \partial B/\partial z, \partial B/\partial y).

Alternatively, _whenever_ your data is on a product of two grids, even if the grids are irregular, you can do a sequence of two 1d interpolations

1. for each x value construct an interpolant in the y coordinate.
2. you now have a sequence of interpolation coefficients c (at each x) — interpolate these in the x grid to get an interpolated coefficient c(x).
3. for any arbitrary (x,y), first interpolate the coefficients c(x) at this x, then use those interpolated coefficients to evaluate at y.
