# Interpolation with matrices

**URL:** <https://discourse.julialang.org/t/interpolation-with-matrices/49058>\
**Category:** General Usage\
**Tags:** package, interpolations\
**Created:** [October 26, 2020, 4:51pm UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058 "2020-10-26T16:51:36Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregory](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregory/32/18969_2.png) [@gregory](https://discourse.julialang.org/u/gregory)\
**Post date:** [October 26, 2020, 4:51pm UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/1 "2020-10-26T16:51:36Z")

</div>

```julia
Using Interpolations

x = rand(50,5)*2 .- 1 # Fake input data
y = rand(50) # Fake output data

itp = interpolate((x,),y, BSpline(Linear()))

ERROR: MethodError: no method matching interpolate(::Tuple{Array{Float64,1}}, ::Array{Float64,1}, ::BSpline{Linear})
Closest candidates are:
  interpolate(::Tuple{Vararg{Union{AbstractArray{T,1}, Tuple} where T,N}}, ::AbstractArray{Tel,N}, ::IT) where
{Tel, N, IT<:Union{NoInterp, Tuple{Vararg{Union{NoInterp, Gridded},N} where N}, Gridded}} 
at .../.julia/packages/Interpolations/XUr6v/src/gridded/gridded.jl:83

```

I don’t understand how to fix this. Alternatively, is there a package that does something similar to the R-package seen here [https://cran.r-project.org/web/packages/chebpol/chebpol.pdf](https://cran.r-project.org/web/packages/chebpol/chebpol.pdf)

Thank you for your time.

---

<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:** [October 26, 2020, 7:57pm UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/2 "2020-10-26T19:57:33Z")

</div>

> [@gregory](#):
>
> Alternatively, is there a package that does something similar to the R-package seen here [https://cran.r-project.org/web/packages/chebpol/chebpol.pdf](https://cran.r-project.org/web/packages/chebpol/chebpol.pdf)

Yes, I have a package for fast (FFT-based) multivariate vector-valued Chebyshev interpolation on hypercubes: [GitHub - JuliaMath/FastChebInterp.jl: fast multidimensional Chebyshev interpolation and regression in Julia](https://github.com/stevengj/FastChebInterp.jl)

(It is currently unregistered — it wasn’t clear to me if this should ultimately be a new package or should be merged with the ChebyshevApprox.jl package [Generated functions are slow · Issue #3 · RJDennis/ChebyshevApprox.jl · GitHub](https://github.com/RJDennis/ChebyshevApprox.jl/issues/3))

Note that this is quite different from BSpline interpolation — fast Chebyshev interpolation requires data at a very specific set of interpolation points (and achieves exponential accuracy for smooth functions), whereas splines work with a more arbitrary set of points.

---

<div class="post-metadata">

**Author:** ![gregory](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregory/32/18969_2.png) [@gregory](https://discourse.julialang.org/u/gregory)\
**Post date:** [October 26, 2020, 10:22pm UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/3 "2020-10-26T22:22:02Z")

</div>

Thank you for the reply. If it requires specific data points for interpolation, I won’t be able to use it as this comes from biological data that I cannot control.

---

<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:** [October 26, 2020, 10:52pm UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/4 "2020-10-26T22:52:41Z")

</div>

I could be wrong here (I mainly use gridded interpolation), but I think it is expecting one vector for each dimension of your knots (`x` here). Is each column of `x` a different dimension?

---

<div class="post-metadata">

**Author:** ![gregory](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregory/32/18969_2.png) [@gregory](https://discourse.julialang.org/u/gregory)\
**Post date:** [October 27, 2020, 12:46am UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/5 "2020-10-27T00:46:21Z")

</div>

Nope. They are all the same dimension. It was a minimal working example but essentially I have 5 columns that describe the copy number (CN) variability of a particular chromosome `x_i` and I have a corresponding fitness `y`. The goal was to use interpolation and have a function `f` that takes in the CN values and outputs a value e.g. `f(X) = Y`. I can’t seem to find documented examples in any of the standard packages (e.g. Interpolations, ApproxFun) where they have an input matrix `X` and output vector `Y` for the interpolation step.

---

<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:** [October 27, 2020, 2:05am UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/6 "2020-10-27T02:05:29Z")

</div>

Not sure I understand then. Here’s how I think about it: how would you go about plotting this data?

The pdf that you linked to also uses data with multiple dimensions (aka a multivariate grid).

---

<div class="post-metadata">

**Author:** ![gregory](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregory/32/18969_2.png) [@gregory](https://discourse.julialang.org/u/gregory)\
**Post date:** [October 27, 2020, 2:36pm UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/7 "2020-10-27T14:36:42Z")

</div>

It does have multivariate grids, but it also describes using the package for scattered data (see page 3). The output does produce a function that allows us to give a vector of 5 values and get out an expected `y`. I have not dug too deeply into how it works, so I cannot answer to that point. In regards to plotting data in 5D, I’d argue that is a rather difficult concept in general 😉

---

<div class="post-metadata">

**Author:** ![gregory](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregory/32/18969_2.png) [@gregory](https://discourse.julialang.org/u/gregory)\
**Post date:** [October 27, 2020, 2:50pm UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/8 "2020-10-27T14:50:49Z")

</div>

Perhaps this will also be useful in regards to the R package in terms of the functionality I’m hoping to use in Julia: [https://cran.r-project.org/web/packages/chebpol/vignettes/chebpol.pdf](https://cran.r-project.org/web/packages/chebpol/vignettes/chebpol.pdf). In particular the last few pages (beginning at the end of page 7) discusses the two solvers available for scattered data.

---

<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:** [October 27, 2020, 2:59pm UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/9 "2020-10-27T14:59:38Z")

</div>

> [@gregory](#):
>
> In particular the last few pages (beginning at the end of page 7) discusses the two solvers available for scattered data.

[GitHub - RJDennis/ChebyshevApprox.jl: A Julia package to approximate multivariate continuous functions using Chebyshev polynomials.](https://github.com/RJDennis/ChebyshevApprox.jl) handles multivariate chebyshev approximation with scattered data, similar the last section of that R package, IIRC.

---

<div class="post-metadata">

**Author:** ![gregory](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregory/32/18969_2.png) [@gregory](https://discourse.julialang.org/u/gregory)\
**Post date:** [October 27, 2020, 3:39pm UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/10 "2020-10-27T15:39:02Z")

</div>

Ok. I am trying to use ChebysgevApprox, but am getting some errors. Here is some sample data:

```julia
 nodes = [0.8 1.3 0.5 0.6 0.5
 1.2 1.0 -0.4 0.4 0.4
 0.6 0.7 -0.5 0.1 -0.1
 0.2 0.9 1.1 0.1 0.1
 1.5 2.1 1.3 3.1 0.2
 1.9 -0.1 0.3 1.1 1.8
 0.9 0.0 0.0 0.0 0.8
 1.1 1.3 -0.8 0.2 0.3
 0.9 1.6 -1.1 1.7 -0.8
 0.4 0.4 -0.3 0.4 0.4]

y = [0.346573590279973
 0.277258872223978
 0.154032706791099
 0.198042051588556
 0.346573590279973
 0.693147180559945
 0.462098120373297
 0.346573590279973
 0.462098120373297
 0.462098120373297]

domain = [-1.0 -0.5 -1.1 -1.0 -0.9
  3.8 2.5 1.8 3.1 2.3];

w = chebyshev_weights(B,(nodes,),[2,2,2,2,2],domain)

```

Leads to the error:

```julia
ERROR: MethodError: no method matching chebyshev_weights(::Array{Float64,1}, ::NTuple{5,Array{Float64,1}}, ::Array{Int64,1}, ::Array{Float64,2})

```

---

<div class="post-metadata">

**Author:** ![gregory](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregory/32/18969_2.png) [@gregory](https://discourse.julialang.org/u/gregory)\
**Post date:** [October 27, 2020, 3:40pm UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/11 "2020-10-27T15:40:37Z")

</div>

I have also tried converting `nodes` to a tuple of 1D arrays a few ways but also as verbose as possible with:

```julia
tmp=(nodes[:,1],nodes[:,2],nodes[:,3],nodes[:,4],nodes[:,5])

```

which still does not help. I am sure I’m doing something silly. Any ideas how to fix it?

---

<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:** [October 27, 2020, 4:26pm UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/12 "2020-10-27T16:26:13Z")

</div>

> [@gregory](#):
>
> I am sure I’m doing something silly. Any ideas how to fix it?

My bad, I just looked back at the ChebyshevApprox.jl package and it still requires that the points lie on a tensor-product grid; it doesn’t support a completely unstructured set of points, which seems to be what you want.

(Of course, you could just do a multivariate polynomial least-squares fit with `\` or with `LsqFit.jl`.)

---

<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:** [October 28, 2020, 8:53am UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/13 "2020-10-28T08:53:55Z")

</div>

> [@stevengj](#):
>
> it doesn’t support a completely unstructured set of points, which seems to be what you want.

Just to add that to really get the benefits of Chebyshev interpolation, one should use the relevant Chebyshev nodes; otherwise accuracy degrades significantly.

---

<div class="post-metadata">

**Author:** ![gregory](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregory/32/18969_2.png) [@gregory](https://discourse.julialang.org/u/gregory)\
**Post date:** [October 28, 2020, 3:15pm UTC](https://discourse.julialang.org/t/interpolation-with-matrices/49058/14 "2020-10-28T15:15:06Z")

</div>

After staring at it longer, I realize what I wanted is something similar to [Polyharmonic spline - Wikipedia](https://en.wikipedia.org/wiki/Polyharmonic_spline). I did not find this in any of the interpolation packages to my knowledge, so I just went ahead and coded up one myself. Thanks for all the advice.
