Sympy不会简化符号除以其平方的平方根的表达式

时间:2022-08-11 16:50:42

I have a problem with sympy simplification. See the code below. When I substitute expression under sqrt with something, it becomes "unbreakable". For example, in the last line of code I multiplied expression by f0 and simplified the result. f0 in numerator and sqrt(f0**2) in denominator did not get simplified out even though f0 is declared as nonnegative. What did I do wrong and how do I substitute expressions without having this effect?

我有简化问卷的问题。请参阅下面的代码。当我用sqrt替换sqrt下的表达式时,它变得“牢不可破”。例如,在代码的最后一行中,我将表达式乘以f0并简化了结果。即使f0被声明为非负,分母中的f0和分母中的sqrt(f0 ** 2)也没有被简化。我做错了什么,如何在没有这种效果的情况下替换表达式?

Code (pretty version with rendered equations):

代码(带渲染方程的漂亮版本):

>>> from __future__ import division
>>> from sympy import *
>>> from sympy.abc import pi

>>> init_printing(use_unicode=True)
>>> L0, Csum0, f0 = symbols("L0 C_{{\\Sigma}0} f0", nonnegative=True)
>>> equation = sqrt(L0)
>>> equation
  ____
╲╱ L₀

>>> substitute = solve(Eq(1/(2*pi*sqrt(L0)*sqrt(Csum0)), f0), L0, dict=1)[0]
>>> substitute
⎧              1           ⎫
⎪L₀: ──────────────────────⎪
⎨                      2  2⎬
⎪    4⋅C_{{\Sigma}0}⋅f₀ ⋅π ⎪
⎩                          ⎭

>>> equation = equation.subs(substitute)
>>> equation
     ______________________
    ╱          1
   ╱  ────────────────────
  ╱                   2  2
╲╱    C_{{\Sigma}0}⋅f₀ ⋅π
───────────────────────────
             2

>>> simplify(equation*f0)
        ______________________
       ╱          1
f₀⋅   ╱  ────────────────────
     ╱                   2  2
   ╲╱    C_{{\Sigma}0}⋅f₀ ⋅π
──────────────────────────────
               2

1 个解决方案

#1


1  

Cancelling f0/sqrt(f0**2) is not legitimate when f0 is 0. To ensure this cancellation is allowed, declare f0 to be positive, not just nonnegative.

当f0为0时,取消f0 / sqrt(f0 ** 2)是不合法的。为了确保允许这种取消,将f0声明为正,而不仅仅是非负。

Also, importing pi from sympy.abc makes pi a generic symbol with no specific meaning; in particular, it's not known to be positive. SymPy already has pi (mathematical constant) built-in, and knows it's a positive number. So removing the import from sympy.abc improves simplification.

此外,从sympy.abc导入pi使pi成为一个没有特定含义的通用符号;特别是,不知道是积极的。 SymPy已经内置了pi(数学常数),并且知道它是一个正数。因此,从sympy.abc中删除导入可以简化操作。

from sympy import *
L0, Csum0, f0 = symbols("L0 C_{{\\Sigma}0} f0", positive=True)
equation = sqrt(L0)
substitute = solve(Eq(1/(2*pi*sqrt(L0)*sqrt(Csum0)), f0), L0, dict=1)[0]
equation = equation.subs(substitute)
simplify(equation*f0)

returns

回报

1/(2*pi*sqrt(C_{{\Sigma}0}))

#1


1  

Cancelling f0/sqrt(f0**2) is not legitimate when f0 is 0. To ensure this cancellation is allowed, declare f0 to be positive, not just nonnegative.

当f0为0时,取消f0 / sqrt(f0 ** 2)是不合法的。为了确保允许这种取消,将f0声明为正,而不仅仅是非负。

Also, importing pi from sympy.abc makes pi a generic symbol with no specific meaning; in particular, it's not known to be positive. SymPy already has pi (mathematical constant) built-in, and knows it's a positive number. So removing the import from sympy.abc improves simplification.

此外,从sympy.abc导入pi使pi成为一个没有特定含义的通用符号;特别是,不知道是积极的。 SymPy已经内置了pi(数学常数),并且知道它是一个正数。因此,从sympy.abc中删除导入可以简化操作。

from sympy import *
L0, Csum0, f0 = symbols("L0 C_{{\\Sigma}0} f0", positive=True)
equation = sqrt(L0)
substitute = solve(Eq(1/(2*pi*sqrt(L0)*sqrt(Csum0)), f0), L0, dict=1)[0]
equation = equation.subs(substitute)
simplify(equation*f0)

returns

回报

1/(2*pi*sqrt(C_{{\Sigma}0}))