问题
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