SymPy - 符号二维插值,如何使 scipy.interpolate.interp2d 符号化

SymPy - symbolic 2D interpolation, how to make scipy.interpolate.interp2d symbolic

我正在尝试对散点进行符号二维插值。我使用了

f = scipy.interpolate.interp2d(X, Y, Z)

但我无法使用 SymPy 使插值函数符号化。我需要它作为符号区分和其他东西的象征。

我试过了

x = sympy.Symbol("r")
y = sympy.Symbol("s")
f_symbolic = sympy.lambdify((x, y), f(x, y))

这给出了

TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'

你能指导我或帮助我实现符号二维插值或如何使 scipy.interpolate.interp2d 对象符号化吗?

注意:插值函数看起来像这样。它由4个角点给出。

正如我在评论中的意思,对于这个特定的函数,可以获得符号函数作为方程组的解。

# (x, y, z) of the 4 corner points which are given
x = np.array([[0, 1],
              [0, 1]])
y = np.array([[0, 0],
              [-1, -1]])
z = np.array([[1, 0],
              [0, 0]])

# looking for a function z(x, y) = ax + bxy + cy + d
a, b, c, d = sympy.var("a b c d")   # unknown coeffs
rows, cols = x.shape
eqs = []
# creating an equation for each of the point
for i_row in range(rows):
    for i_col in range(cols):
        eqs.append(a*x[i_row, i_col] + b*x[i_row, i_col]*y[i_row, i_col] + c*y[i_row, i_col] + d - z[i_row, i_col])
sols = sympy.solve(eqs, [a, b, c, d])
# print(sols)
# {a: -1.00000000000000, b: -1.00000000000000, c: 1.00000000000000, d: 1.00000000000000}
x_ = sympy.Symbol("x")
y_ = sympy.Symbol("y")
# resulting symbolic function
f_symbolic = sympy.Lambda((x_, y_), sols[a]*x_ + sols[b]*x_*y_ + sols[c]*y_ + sols[d])

也许这个解决方法对某人有帮助。