使用相同名称的符号替换sympy表达式中的函数

时间:2022-05-02 19:34:19

It seems that sympy's solve function cannot solve some equations when Functions are present in the equations or are the targets to solve for.

当函数存在于方程中或者是要求解的目标时,似乎sympy的求解函数不能解决某些方程。

To get around this, I'd like to create a generic function that will automatically replace Functions in an expression with Symbols of the same name.

为了解决这个问题,我想创建一个通用函数,它将使用相同名称的符号自动替换表达式中的函数。

For example, if Function('myfunc')(Symbol('t')) appears in an expression, I'd like to substitute Symbol('myfunc') in for that.

例如,如果函数('myfunc')(符号('t'))出现在表达式中,我想替换符号('myfunc')。

The subs function cannot do this because the names must be explicitly known beforehand.

subs函数不能这样做,因为必须事先明确知道名称。

The replace function looks promising, particularly the func -> func flavor of it, but I cannot figure out how to get the name of a function object from the object.

替换函数看起来很有前景,特别是它的func - > func风格,但我无法弄清楚如何从对象中获取函数对象的名称。

1 个解决方案

#1


0  

Here is my suggestion:

这是我的建议:

import sympy

t = sympy.Symbol("t")
x = sympy.Symbol("x")
F = sympy.Function("F")(t)
G = sympy.Function("G")

def remove_undefineds(expression):
    for item in sympy.preorder_traversal(expression):
        if isinstance(item, sympy.function.AppliedUndef):
            name = str(item).split('(')[0]
            expression = expression.subs(item, sympy.Symbol(name))
    return expression


expression = F + G(x) + t**x

cleaned = remove_undefineds(expression)

yields:

收益率:

In [1]: expression
Out[1]: t**x + F(t) + G(x)

In [2]: cleaned
Out[2]: F + G + t**x

The idea is to loop over preorder_traversal of the expression, check for each item whether it's an instance of AppliedUndefined (i.e. F(_) with F being an undefined function), and if yes, replace it with a symbol of according name.

我们的想法是遍历表达式的preorder_traversal,检查每个项目是否是AppliedUndefined的实例(即F(_),其中F是未定义的函数),如果是,则将其替换为相应名称的符号。

I used string split to get the "myfunc" part alone, assuming the arguments shall be dropped at this step; else one could replace just by str(item).

我使用字符串拆分来单独获取“myfunc”部分,假设在此步骤中应删除参数;否则只能用str(item)替换。

I also assume only undefind functions are to be replaced, else one would have to augment accordingly...

我还假设只有未取代的功能被替换,否则就必须相应增加...

#1


0  

Here is my suggestion:

这是我的建议:

import sympy

t = sympy.Symbol("t")
x = sympy.Symbol("x")
F = sympy.Function("F")(t)
G = sympy.Function("G")

def remove_undefineds(expression):
    for item in sympy.preorder_traversal(expression):
        if isinstance(item, sympy.function.AppliedUndef):
            name = str(item).split('(')[0]
            expression = expression.subs(item, sympy.Symbol(name))
    return expression


expression = F + G(x) + t**x

cleaned = remove_undefineds(expression)

yields:

收益率:

In [1]: expression
Out[1]: t**x + F(t) + G(x)

In [2]: cleaned
Out[2]: F + G + t**x

The idea is to loop over preorder_traversal of the expression, check for each item whether it's an instance of AppliedUndefined (i.e. F(_) with F being an undefined function), and if yes, replace it with a symbol of according name.

我们的想法是遍历表达式的preorder_traversal,检查每个项目是否是AppliedUndefined的实例(即F(_),其中F是未定义的函数),如果是,则将其替换为相应名称的符号。

I used string split to get the "myfunc" part alone, assuming the arguments shall be dropped at this step; else one could replace just by str(item).

我使用字符串拆分来单独获取“myfunc”部分,假设在此步骤中应删除参数;否则只能用str(item)替换。

I also assume only undefind functions are to be replaced, else one would have to augment accordingly...

我还假设只有未取代的功能被替换,否则就必须相应增加...