2

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)
Chris
  • 33
  • 1
  • 3
  • Much of the code isn't relevant: you could just type `x=sqrt(2)/2` and then try to simplify x. Problem is, sqrt(2)/2 is considered a simpler form than 1/sqrt(2). Entering `x=1/sqrt(2)` will automatically turn it to `sqrt(2)/2`. –  May 23 '16 at 02:44

2 Answers2

2

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).

asmeurer
  • 86,894
  • 26
  • 169
  • 240
0

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.

Chris
  • 33
  • 1
  • 3