Sympy: Simplifying Square Roots Of Squares
Sympy does not seem to be able to simplify an expression where the square root of a square of a variable is involved: In [28]: a = x**2 In [29]: b = a**(1/2) In [30]: b Out[30]:
Solution 1:
There are two problems with your example.
First sqrt(x**2)==x
only for positive real numbers.
Second, for SymPy 1/2
and 0.5
are not the same things. The first is a Rational
instance, the second is a float
instance.
Finally, an example:
>>>x = Symbol('x', real=True)>>>(x**2)**(1./2)
∣x∣**1.0
>>>(x**2)**(S(1)/2) # S() is short for sympify()
∣x∣
sympify
transforms python objects to more adequate SymPy objects.
Solution 2:
I assume you declare x
as x = Symbol('x')
. If you change it to x = Symbol('x', real=True)
the expression should be simplified. You can find the reason why you have to explicitly state that your variable is real
in the sympy bugtracker.
Solution 3:
The function you want is powdenest
. If passed the force=True
parameter, it will ignore assumptions
>>>powdenest(sqrt(x**2), force=True)
x
Post a Comment for "Sympy: Simplifying Square Roots Of Squares"