How to interpolate in n dimensions with different length axes

Viewed 229

So I was looking at how to use scipy's interpn function, and the example they have on the documentation isn't quite working with what I need it to do.

My implementation is a bit different. I have a precomputed value array with shape [200,40,40,40] that I get from a different script.

So when I do something like:

t = np.linspace(0,1, 200)
x = np.linspace(0,1, 40)
y = np.linspace(0,1, 40)
z = np.linspace(0,1, 40)
points = (t,x,y,z)
interpn(points,values,point)

I get an error: "ValueError: There are 40 points and 200 values in dimension 0"

It seems as though the dimensions of my points tuple and value array are not lining up, but I thought since my "t" axis is first in the tuple, it should be match. Any advice?

1 Answers

So this works for me:

import numpy as np
from scipy.interpolate import interpn

def f(x,y,z,t):
    '''Simple 3D + time dimensional function.'''
    return (np.sin(x)+y+np.sqrt(z))*t

t = np.linspace(0,1,200)
x = np.linspace(0,1,40)
y = np.linspace(0,1,40)
z = np.linspace(0,1,40)
points = (x,y,z,t)
values = f(*np.meshgrid(*points))

# example point in domain
point = [0,0.5,0.75,1/3.]

print(interpn(points, values, point))
array([0.44846267])

You defined x,y,z as np.linspace(0,40,1), this means you have a single point on the interval [0,40]. The same for t. That's probably your error. Example taken from the official scipy documentation.

Related