Evaluate the output from scipy 2D interpolation along a curve

前端 未结 3 1396
遥遥无期
遥遥无期 2021-01-14 02:40

I have data z sampled from a 2D function f at grid points x, y, as in z = f(x, y).

It is easy to interpolate

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-14 02:58

    Based on user6655984's suggestion, I've posted the following wrapper function in another thread:

    import scipy.interpolate as si
    def interp2d_pairs(*args,**kwargs):
        """ Same interface as interp2d but the returned interpolant will evaluate its inputs as pairs of values.
        """
        # Internal function, that evaluates pairs of values, output has the same shape as input
        def interpolant(x,y,f):
            x,y = np.asarray(x), np.asarray(y)
            return (si.dfitpack.bispeu(f.tck[0], f.tck[1], f.tck[2], f.tck[3], f.tck[4], x.ravel(), y.ravel())[0]).reshape(x.shape)
        # Wrapping the scipy interp2 function to call out interpolant instead
        return lambda x,y: interpolant(x,y,si.interp2d(*args,**kwargs))
    
    # Create the interpolant (same interface as interp2d)
    f = interp2d_pairs(X,Y,Z,kind='cubic')
    # Evaluate the interpolant on each pairs of x and y values
    z=f(x,y)
    

提交回复
热议问题