# Interpolating into a series of matrices

**URL:** https://discourse.julialang.org/t/interpolating-into-a-series-of-matrices/46635
**Category:** General Usage
**Tags:** interpolations
**Created:** [September 15, 2020, 12:23pm UTC](https://discourse.julialang.org/t/interpolating-into-a-series-of-matrices/46635 "2020-09-15T12:23:38Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [September 15, 2020, 12:23pm UTC](https://discourse.julialang.org/t/interpolating-into-a-series-of-matrices/46635/1 "2020-09-15T12:23:38Z")

</div>

I’m writing a package that needs to generically interpolate a matrix from a time-series of matrices.

For example, maybe it’s a series of spatial matrices of monthly mean values, that I would like to interpolate to a single matrix of values for a particular day somewhere in the series.

Is there an options in an interpolations package that would handle this kind of thing, giving the options of various splines or linear interpolation?

I also want to run it on a GPU (greedy…), so broadcast operations are good here, including ways I can roll my own. But hopefully without having to define all the spline etc. methods myself.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [September 15, 2020, 2:19pm UTC](https://discourse.julialang.org/t/interpolating-into-a-series-of-matrices/46635/2 "2020-09-15T14:19:47Z")

</div>

AFAIK Interpolations.jl should “just work”:

```julia
julia> data = [[1 2; 3 4], [5 6; 7 8]]
2-element Array{Array{Int64,2},1}:
 [1 2; 3 4]
 [5 6; 7 8]

julia> itp = interpolate(data, BSpline(Linear()))
2-element interpolate(::Array{Array{Int64,2},1}, BSpline(Linear())) with element type Array{Float64,2}:
 [1 2; 3 4]
 [5 6; 7 8]

julia> itp[1.0]
2×2 Array{Float64,2}:
 1.0 2.0
 3.0 4.0

julia> itp[1.1]
2×2 Array{Float64,2}:
 1.4 2.4
 3.4 4.4

```

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [September 15, 2020, 2:34pm UTC](https://discourse.julialang.org/t/interpolating-into-a-series-of-matrices/46635/3 "2020-09-15T14:34:22Z")

</div>

Although apparently not for splines:

```julia
julia> itp = interpolate(data, BSpline(Cubic(Line(OnGrid()))))
ERROR: MethodError: no method matching zero(::Type{Array{Int64,2}})
Closest candidates are:
  zero(::Type{Pkg.Resolve.FieldValue}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Pkg/src/Resolve/fieldvalues.jl:38
  zero(::Type{DateTime}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Dates/src/types.jl:404
  zero(::Type{Measures.Length{:mm,Float64}}) at /home/raf/.julia/packages/Plots/M1wcx/src/layouts.jl:12
  ...
Stacktrace:
 [1] copy_with_padding(::Type{Array{Int64,2}}, ::Array{Array{Int64,2},1}, ::BSpline{Cubic{Line{OnGrid}}}) at /home/raf/.julia/packages/Interpolations/TBuvH/src/b-splines/prefiltering.jl:27
 [2] prefilter(::Type{Float64}, ::Type{Array{Int64,2}}, ::Array{Array{Int64,2},1}, ::BSpline{Cubic{Line{OnGrid}}}) at /home/raf/.julia/packages/Interpolations/TBuvH/src/b-splines/prefiltering.jl:39
 [3] interpolate(::Type{Float64}, ::Type{Array{Int64,2}}, ::Array{Array{Int64,
2},1}, ::BSpline{Cubic{Line{OnGrid}}}) at /home/raf/.julia/packages/Interpolations/TBuvH/src/b-splines/b-splines.jl:160
 [4] interpolate(::Array{Array{Int64,2},1}, ::BSpline{Cubic{Line{OnGrid}}}) at /home/raf/.julia/packages/Interpolations/TBuvH/src/b-splines/b-splines.jl:179
 [5] top-level scope at REPL[40]:1

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [September 15, 2020, 2:47pm UTC](https://discourse.julialang.org/t/interpolating-into-a-series-of-matrices/46635/4 "2020-09-15T14:47:25Z")

</div>

Ah, interesting. Depending on the size of your array, you could use `SMatrix` from StaticArrays, which does have a `zero` defined:

```julia
julia> data = [@SMatrix[1.0 2; 3 4], @SMatrix[3.0 4; 5 6]]
2-element Array{SArray{Tuple{2,2},Float64,2,4},1}:
 [1.0 2.0; 3.0 4.0]
 [3.0 4.0; 5.0 6.0]

julia> itp = interpolate(data, BSpline(Cubic(Line(OnGrid()))))
2-element interpolate(OffsetArray(::Array{SArray{Tuple{2,2},Float64,2,4},1}, 0:3), BSpline(Cubic(Line(OnGrid())))) with element type SArray{Tuple{2,2},Float64,2,4}:
 [0.9999999999999999 2.0; 3.0 4.0]
 [3.0 4.0; 5.0 6.0]

julia> itp[1.1]
2×2 SArray{Tuple{2,2},Float64,2,4} with indices SOneTo(2)×SOneTo(2):
 1.2 2.2
 3.2 4.2

```

Alternatively, if you know the particular sizes of your data but don’t want to use StaticArrays, you could create a wrapper `struct` around a `Matrix{T}` and define the relevant methods (probably just `zero` and basic arithmetic). I did that in the past when I needed to interpolate `Vector{Float64}`s and it worked fine.

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [September 15, 2020, 2:51pm UTC](https://discourse.julialang.org/t/interpolating-into-a-series-of-matrices/46635/5 "2020-09-15T14:51:57Z")

</div>

2000 \* 4000 😅

Strange that `Array` doesn’t have `zero` defined.

They will always be wrapped in `GeoArray` from GeoData.jl. I can just define `zero` on that. Thanks

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [September 15, 2020, 2:59pm UTC](https://discourse.julialang.org/t/interpolating-into-a-series-of-matrices/46635/6 "2020-09-15T14:59:51Z")

</div>

`Array` doesn’t have `zero` because there isn’t a way to know how big the zeroed array should be (since `zero` gets passed the type, not the value). I think `SizedArray` from StaticArrays should work though, and without the compile time issues for large matrices.

edit: to be more clear, there’s a method for `zero` that takes a type, and a method that takes a value, so `zero(rand(2,2))` works, but `zero(typeof(rand(2,2)))` does not, and it seems like Interpolations uses the latter one.

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [September 15, 2020, 4:14pm UTC](https://discourse.julialang.org/t/interpolating-into-a-series-of-matrices/46635/7 "2020-09-15T16:14:35Z")

</div>

That makes more sense. So really this is probably a bug/oversight in Interpolations.jl using zero on the type instead of the array.

Namely here:

[https://github.com/JuliaMath/Interpolations.jl/blob/eb5690069a8c5cbe97b6ac2e44f58ea1567bee8d/src/b-splines/prefiltering.jl#L19-L31](https://github.com/JuliaMath/Interpolations.jl/blob/eb5690069a8c5cbe97b6ac2e44f58ea1567bee8d/src/b-splines/prefiltering.jl#L19-L31)

It’s using TC instead of A on line 27, but probably because of things happening below in `prefilter`

Wish it had a maintainer to clarify that.

It’s also inefficient in that you can’t pass in the `padded_similar` array so it reuses the same one instead of allocating a new one every time.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [September 16, 2020, 8:10am UTC](https://discourse.julialang.org/t/interpolating-into-a-series-of-matrices/46635/8 "2020-09-16T08:10:27Z")

</div>

> [@Raf](#):
>
> Wish it had a maintainer to clarify that.

Perhaps open an issue. (How do you expect the maintainers to know about it otherwise?)

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [September 16, 2020, 10:38am UTC](https://discourse.julialang.org/t/interpolating-into-a-series-of-matrices/46635/9 "2020-09-16T10:38:58Z")

</div>

It’s looking for a maintainer currently - but looking more closely they are trying to help with PRs to encourage someone to take it over.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [September 16, 2020, 11:06am UTC](https://discourse.julialang.org/t/interpolating-into-a-series-of-matrices/46635/10 "2020-09-16T11:06:09Z")

</div>

In which case opening an issue is even more important, since scattered questions like this would just get lost in the transition.
