【问题标题】:SymPy - symbolic 2D interpolation, how to make scipy.interpolate.interp2d symbolicSymPy - 符号 2D 插值,如何使 scipy.interpolate.interp2d 符号化
【发布时间】:2021-11-06 22:49:55
【问题描述】:

我正在尝试对散点进行符号 2D 插值。我利用了

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: 无法根据规则“安全”将数组数据从 dtype('O') 转换为 dtype('float64')

您能否指导或帮助我实现符号 2D 插值或如何使 scipy.interpolate.interp2d 对象具有符号?

注意:插值函数如下所示。它由 4 个角点给出。

【问题讨论】:

  • numpy/scipy 不能进行符号计算。那是严格的数字。
  • sympy.lambdify 将 sympy 表达式转换为 numpy 函数(在限制范围内)
  • 这是否意味着无法获得符号功能?我想到了另一种方法。如果我没记错的话,我正在寻找一个函数 z(x,y) = ax + bxy + cy + d。所以,也许我可以从方程组中找到系数 a、b、c、d - 因为我知道角落的 4 个点的 (x, y, z)。

标签: python scipy interpolation sympy


【解决方案1】:

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

# (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])

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

【讨论】:

    猜你喜欢
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 2017-07-24
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多