Evaluate the output from scipy 2D interpolation along a curve

前端 未结 3 1393
遥遥无期
遥遥无期 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 03:01

    The method interp2d returns an object whose call method expects the x, y vectors to be coordinates of a rectangular grid. And the reason you don't get the desired values from the diagonal of the returned array is that it sorts x, y first.

    But there is a workaround, which I also used in Querying multiple points on a bivariate spline in the B-spline basis. After executing

    import scipy.interpolate as si
    f = si.interp2d(x, y, z)
    

    evaluate f not by calling it, but by passing its tck properties, followed by your x, y coordinates, to internal bispeu method. Like this:

    print(si.dfitpack.bispeu(f.tck[0], f.tck[1], f.tck[2], f.tck[3], f.tck[4], x, y)[0])
    

    The above returns the same as the slow loop

    print(np.array([f(xi, yi)[0] for xi, yi in zip(x, y)]))
    

    Explanation

    The object f is secretly a B-spline of order 1. The spline parameters (knots, coefficients, order) are contained in its tck property and can be used directly by lower-order routines to the desired effect.

    (Ideally, the call method of f would have a Boolean parameter grid which we'd set to False to let it know we don't want grid evaluation. Alas, it's not implemented.)

提交回复
热议问题