How do I get sympy to simplify an expression containing sqrt(2)/2?

空扰寡人 提交于 2019-12-24 00:35:02

问题


This code:

from sympy import *
x = Symbol('x', positive=True)
vp = Symbol('vp', positive=True)
num = integrate( (vp*sin(x))**2, (x, 0, 2*pi))
den = integrate(      1        , (x, 0, 2*pi))
print " num =",num
print " den =",den
vrms = sqrt(num/den)
print "vrms =",vrms
print "simplified vrms = ",simplify(vrms)

Returns this:

 num = pi*vp**2
 den = 2*pi
vrms = sqrt(2)*vp/2
simplified vrms =  sqrt(2)*vp/2

How can I get it to take the last step? I'd like it return this:

vrms = vp/sqrt(2)

回答1:


SymPy automatically canonicalizes rational power of rational numbers to a form with positive exponents and reduced powers. Because this happens automatically, it will happen with every such number that appears in any expression, meaning there is no way to make sqrt(2)/2 result in 1/sqrt(2).




回答2:


So it looks like sqrt(2)/2 is simpler than 1/sqrt(2).

Thank you, sandwich.

Indeed, much of the code in the example is superfluous. I was worried that how the symbols were defined and calculated could be relevant.



来源:https://stackoverflow.com/questions/37378902/how-do-i-get-sympy-to-simplify-an-expression-containing-sqrt2-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!