Matrix interpolations

I’m trying out `Interpolations.jl` for matrix-valued data, for this example `Linear` runs but `Cubic` fails.

``````using LinearAlgebra
using Interpolations

f(t) =  [cos(t) -sin(t); sin(t)*im cos(t)]
ts = 0:.01:2*pi
fs = [f(ti) for ti in ts]

itp = interpolate(fs, BSpline(Cubic(Line(OnGrid())))) # fails

itp = interpolate(fs, BSpline(Linear())) #works
ifx = scale(itp, ts)
``````

The error message is `MethodError: no method matching zero(::Type{Array{Complex{Float64},2}})` The documentation of `Interpolations.jl` seems a bit sparse. How can one solve this problem? Thanks a lot!

That error tells you that at some point, Interpolations tried to call `zero` on the type of one of your matrices returned by `f(t)`, which is not defined (and it can’t be, since the size of the matrix is not encoded in the type). Its really just an artifact of how Interpolations.jl is written though, since it might otherwise try to call `zero` on the matrix itself, which would have worked.

In any case, barring changing Interpolations.jl, you can have your function return a StaticMatrix, which does have the size encoded in the type. If you do the following, the rest of your code works:

``````using StaticArrays
f(t) = @SMatrix[cos(t) -sin(t); sin(t)*im cos(t)]
``````

You might also get some speedups since StaticArrays are faster for operations on small matrices, exactly like the ones you have there.

Note though that it looks like evaluating the interpolation is slower than just evaluating `f(t)`, so depending on what your actual problem is you may not want to do the interpolation at all.

5 Likes