Hello Paul,

Expanding on the above answer ,there are a few more different ways to
assume the positiveness of expressions under the square root in *SymPy *(using
the new assumptions) . However, it seems that we still cannot directly
simplify terms of the form x**(3/2) to x * sqrt(x), if that is the intended
goal. That said, expressions where the exponent in the numerator is *even*
can indeed be reduced, as demonstrated in the following examples:

from sympy import *

x = symbols('x')
with assuming(Q.positive(x)): # all assumptions can be put within assuming ()
    print(refine(sqrt(x**2)))  # Output: x
# Using refine syntax of type refine(expr,
assumptions)print(refine(sqrt(x**2), Q.positive(x)))  # Output: x
# A few more examples:print(refine(sqrt(x**3), Q.positive(x)))  #
Output: x**(3/2)print(refine(sqrt(x**4), Q.positive(x)))  # Output:
x**2print(refine(sqrt(x**5), Q.positive(x)))  # Output: x**(5/2)

This approach effectively simplifies square roots for *even exponents*,
while odd exponents retain their fractional form.

That said, if anyone in the community knows a way to express odd exponents
(e.g., y**(3/2)) in the form of y * sqrt(y), it would be nice to know that!

Best regards,

Krishnav Bajoria.

On Fri, Mar 21, 2025 at 3:10 PM Pratyksh Gupta <[email protected]>
wrote:

> Hello,
>
> Yes, you can assume that an expression under a square root is positive
> using *assumptions* and simplify(). However, sympy does not automatically
> assume non-negative values for symbolic expressions unless explicitly
> specified.
>
> Example 1: Handling sqrt(x^2) = x
>
> You can assume x is positive using the symbols function:
> *from sympy import symbols, sqrt, simplify *
> * x = symbols('x', positive=True) *
> *expr = sqrt(x**2) *
> *simplified_expr = simplify(expr) *
> *print(simplified_expr) # Output: x*
>
> If x is not explicitly assumed to be positive, sympy will return sqrt(x^2)
> = |x| instead.
>
>
> Example 2: Handling sqrt((x - 2)^3) = (x - 2)*sqrt(x - 2)
>
> You need to ensure that x - 2 is positive:
>
>
>
> *y = symbols('y', positive=True) # Let y = x - 2 be positive expr =
> sqrt(y**3) simplified_expr = simplify(expr) print(simplified_expr) #
> Output: y*sqrt(y)*
>
> For an arbitrary expression, you can try using simplify() along with
> assumptions, but sympy may not always simplify it in the way you expect.
>
> Regards,
> Pratyksh
> On Friday, March 21, 2025 at 2:56:52 PM UTC+5:30 [email protected]
> wrote:
>
>> Hello.
>> Is it possible to assume the positiveness of expressions under the square
>> root, so sqrt(x^2)=x or sqrt((x-2)^3)=(x-2)sqrt(x-2)?
>>
>> Can this be specified fo any expression?
>>
>> Thank you.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion visit
> https://groups.google.com/d/msgid/sympy/b75269b4-7d2b-423b-939f-7d7d4322e670n%40googlegroups.com
> <https://groups.google.com/d/msgid/sympy/b75269b4-7d2b-423b-939f-7d7d4322e670n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/sympy/CAF0TZuv1uot5yGfEsgTKAR6upFUY8oVFa1VVgEgXLRW74qOZgg%40mail.gmail.com.

Reply via email to