繁体   English   中英

有没有办法在 sympy 中剥离变量的所有函数的表达式?

[英]Is there a way to strip an expression of all functions of a variable in sympy?

假设我有一个等式:

 eq = sym.Eq(5*x**2 + 2*x + 5*y + f_1(y) + f_2(x), 0)

Sympy 中有没有办法从上述表达式中去除 x 部分的 function 和 y 部分的 function ?

我的目标是这样的:

sym.strip(eq, x)
>>>5*x**2 + 2*x + f_2(x)
sym.strip(eq, y)
>>>5*y + f_1(y)

我不是 100% 确定这是否适用于您定义的方程,但这似乎对我来说适用于多项式。 试试这个或改编它。

def strip_sym(expr, sym):
    newExpr = 0
    for term in expr.args:
        newTerm = term if sym in term.free_symbols else 0
        newExpr = newExpr + newTerm
    return newExpr

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM