# Multidimensional linear interpolation

**URL:** <https://discourse.julialang.org/t/multidimensional-linear-interpolation/35299>\
**Category:** New to Julia\
**Tags:** question, interpolations\
**Created:** [February 28, 2020, 8:24pm UTC](https://discourse.julialang.org/t/multidimensional-linear-interpolation/35299 "2020-02-28T20:24:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [February 28, 2020, 8:24pm UTC](https://discourse.julialang.org/t/multidimensional-linear-interpolation/35299/1 "2020-02-28T20:24:06Z")

</div>

I have some 2D `Array` data `x` that I want to (linearly) interpolate along a single vector `a` uniformly in each dimension (with the `Interpolations` package). An example is below:

```julia
using Interpolations
a = collect(range(0, stop=5, length=10))
x = vcat([collect(range(0, stop=5i, length=10)) for i in 1:3]'...)

```

but neither of the two below attempts works

```julia
itp = interpolate((a,a,a), x, Gridded(Linear()))
itp = interpolate((a,), x, Gridded(Linear()))

```

I could create an array of interpolants for each dimension manually

```julia
itp = [interpolate((a,), x[i,:], Gridded(Linear())) for i in 1:3]

```

but was wondering if I could efficiently do this in “one go.”

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [February 28, 2020, 8:57pm UTC](https://discourse.julialang.org/t/multidimensional-linear-interpolation/35299/2 "2020-02-28T20:57:50Z")

</div>

Please include what packages you have `using`ed in your MWE.

We don’t know what ` Gridded(Linear())` you mean, its not in the standard library.  
So it must be from some package, but there are over 2000 julia packages.

🙂

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 28, 2020, 9:13pm UTC](https://discourse.julialang.org/t/multidimensional-linear-interpolation/35299/3 "2020-02-28T21:13:52Z")

</div>

It seems to me like you don’t _actually_ want multivariate linear interpolation. Bivariate gridded interpolation is for when you have two vectors `x` and `y` and a matrix`Z=F(x,y)` for each point the combined `x`,`y` space (these are the “knots”).

```julia
itp =interpolate((x,y),Z,Gridded(Linear()))

```

It sounds to me like you want to take some irregularly spaced data and put it on a grid `a`. In this case you do need to interpolate each time independently (using the actual knots) and then evaluate the interpolant at `a`. Interpolations.jl is pretty quick though, especially for univariate linear interpolation, so you shouldn’t worry about it being too slow.

Did I misunderstand?

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [February 28, 2020, 9:30pm UTC](https://discourse.julialang.org/t/multidimensional-linear-interpolation/35299/4 "2020-02-28T21:30:28Z")

</div>

Thanks @tbeason and @oxinabox, I added more detail (using `Interpolations`). Basically I have a function f: \mathbb{R} \to \mathbb{R}^3 evaluated at [f(a\_1), f(a\_2), \ldots, f(a\_N)] and I want to interpolate linearly f(a) between the a\_i.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 28, 2020, 9:40pm UTC](https://discourse.julialang.org/t/multidimensional-linear-interpolation/35299/5 "2020-02-28T21:40:04Z")

</div>

f : \mathbb{R} \rightarrow \mathbb{R}^3 … can’t say I’ve used interpolations in a context like that before. It sounds to me like you’d need to do it separately.

---

<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 29, 2020, 10:51am UTC](https://discourse.julialang.org/t/multidimensional-linear-interpolation/35299/6 "2020-02-29T10:51:50Z")

</div>

I’m a little confused about what you were trying above, but if your goal is

> [@jacob-roth](#):
>
> I have a function f: \mathbb{R} \to \mathbb{R}^3 evaluated at [f(a\_1), f(a\_2), \ldots, f(a\_N)] and I want to interpolate linearly f(a) between the a\_i.

then it should work out of the box if you do exactly what the math tells you to do:

```julia
using Interpolations, StaticArrays
f(a) = SVector{3}(2a, 3a, 4a) # replace with your real function
agrid = 0:0.5:5
vals = [f(a) for a in agrid]
itp = interpolate((agrid,), vals, Gridded(Linear()))

julia> itp(0.25)
3-element SArray{Tuple{3},Float64,1,3} with indices SOneTo(3):
 0.5 
 0.75
 1.0

julia> f(0.25) # since the function happens to be linear this should match to within numeric precision
3-element SArray{Tuple{3},Float64,1,3} with indices SOneTo(3):
 0.5 
 0.75
 1.0 

```

The confusion might stem from representing `vals` as a 2d array rather than a 1d array.

If your grid is evenly spaced, performance-wise you’ll be much better off using a scaled BSpline than Gridded.

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [March 18, 2020, 10:01pm UTC](https://discourse.julialang.org/t/multidimensional-linear-interpolation/35299/7 "2020-03-18T22:01:10Z")

</div>

Ah I see, I wasn’t clear, sorry. Instead of a function `f`, I have evaluations of `f` on a grid, and I’d like to interpolate linearly on the grid without knowing / having access to `f` itself.

---

<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:** [March 18, 2020, 10:45pm UTC](https://discourse.julialang.org/t/multidimensional-linear-interpolation/35299/8 "2020-03-18T22:45:29Z")

</div>

`f` was just for demonstration purposes. If you have `agrid` and `vals` from another source the `itp = interpolate(...)` call is the same regardless.

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [March 18, 2020, 11:59pm UTC](https://discourse.julialang.org/t/multidimensional-linear-interpolation/35299/9 "2020-03-18T23:59:25Z")

</div>

> [@tim.holy](#):
>
> itp = interpolate((agrid,), vals, Gridded(Linear()))

Ah, the difference is that I have `vals` as a 2D array.

---

<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:** [March 19, 2020, 6:55am UTC](https://discourse.julialang.org/t/multidimensional-linear-interpolation/35299/10 "2020-03-19T06:55:45Z")

</div>

Right. Given the math you posted above, you will likely be better off representing it as an array-of-arrays. (In particular the fact that it’s 3-dimensional is encoded statically, which often allows for some efficiencies.) If you must store it as a 2d array, try `itp = interpolate((1:3, agrid,), data, (NoInterp(), Gridded(Linear())))`.

---

<div class="post-metadata">

**Author:** ![swish47](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swish47/32/43921_2.png) [@swish47](https://discourse.julialang.org/u/swish47)\
**Post date:** [February 27, 2023, 11:29pm UTC](https://discourse.julialang.org/t/multidimensional-linear-interpolation/35299/11 "2023-02-27T23:29:01Z")

</div>

Cool answer. I also have a similar question with the question owner then I fortunately find your solution.  
I have a question that why `StaticArrays` can help `Interpolations` on interpolating vector value function? How does it work?
