使用SymPy(Python)将多个分数合并为一个

时间:2022-08-02 21:15:35

I have a symbolic expression (say, var_1) with is a sum of around ten fractions, each being a complex combination of many parameters. var_1 arises after performing several operations into other expressions. In other words, var_1 is an output and not an input. This is, I only have var_1 and not its individual components.

我有一个符号表达式(例如,var_1),它是大约十个分数的总和,每个分数是许多参数的复杂组合。 var_1在对其他表达式执行多个操作后出现。换句话说,var_1是输出而不是输入。这是,我只有var_1而不是它的各个组件。

By construction, I know that when var_1 is written as one fraction (using the least common multiple as denominator), then the nominator is zero. I am confirming this with the SymPy library of Python (I am actually using SymPy in Julia but operations are the same).

通过构造,我知道当var_1被写为一个分数(使用最小公倍数作为分母)时,分子为零。我用Python的SymPy库证实了这一点(我实际上在Julia中使用SymPy,但操作是相同的)。

I am looking for a function that combines multiple fractions into one, but applied to a single variable like var_1. factor(var_1) doesn't work. It seems SymPy stops factoring due to the complexity of the expression var_1. Any idea?

我正在寻找一个功能,将多个分数合并为一个,但应用于单个变量,如var_1。 factor(var_1)不起作用。由于表达式var_1的复杂性,似乎SymPy停止了因子分解。任何想法?

1 个解决方案

#1


0  

In fact, there are many standard functions that add up fractions in SymPy. For example, simplify, fraction, ratsimp, etc. See the developers section for a complete list of functions (link here).

实际上,有很多标准函数可以在SymPy中添加分数。例如,simplify,fraction,ratsimp等。有关完整的函数列表,请参阅开发人员部分(此处链接)。

The problem in my case is that the expression I have is too complex to be handled by those functions. Therefore the above functions return as output the same input. Yet, my problem was solved by the function powsimp with the option force=true. This is, I called powsimp(var_1,force=true) in Julia (I think it is powsimp(var_1,force=True) in Python). I am not sure myself why that option works but it does!

我的问题是我所拥有的表达式太复杂而无法由这些函数处理。因此,上述函数作为输出返回相同的输入。然而,我的问题是由函数powsimp和选项force = true解决的。这就是我在Julia中调用powsimp(var_1,force = true)(我认为它是Python中的powsimp(var_1,force = True))。我不确定自己为什么这个选项有效,但确实如此!

#1


0  

In fact, there are many standard functions that add up fractions in SymPy. For example, simplify, fraction, ratsimp, etc. See the developers section for a complete list of functions (link here).

实际上,有很多标准函数可以在SymPy中添加分数。例如,simplify,fraction,ratsimp等。有关完整的函数列表,请参阅开发人员部分(此处链接)。

The problem in my case is that the expression I have is too complex to be handled by those functions. Therefore the above functions return as output the same input. Yet, my problem was solved by the function powsimp with the option force=true. This is, I called powsimp(var_1,force=true) in Julia (I think it is powsimp(var_1,force=True) in Python). I am not sure myself why that option works but it does!

我的问题是我所拥有的表达式太复杂而无法由这些函数处理。因此,上述函数作为输出返回相同的输入。然而,我的问题是由函数powsimp和选项force = true解决的。这就是我在Julia中调用powsimp(var_1,force = true)(我认为它是Python中的powsimp(var_1,force = True))。我不确定自己为什么这个选项有效,但确实如此!