Here's the code and the sample expression in text:

>>> def combine_like_radicals(expr):
...     from sympy.utilities.iterables import sift
...     reps = {}
...     for m in expr.atoms(Mul):
...         rads = [p for p in m.atoms(Pow) if p.exp.is_Rational]
...         sifted = sift(rads, lambda x: x.args[1].as_numer_denom())
...         for k, v in sifted.items():
...             if len(sifted[k]) > 1:
...                 e = Rational(*k)
...                 b = Mul(*[p.base for p in v])
...                 reps[Mul(*v)] = Pow(b, e, evaluate=False)
...     return expr.xreplace(reps)
...
>>> sqrt(pi*x)+root(3*pi*x,3)
3**(1/3)*pi**(1/3)*x**(1/3) + sqrt(pi)*sqrt(x)
>>> combine_like_radicals(_)
sqrt(pi*x) + (3*pi*x)**(1/3)



On Wednesday, January 27, 2016 at 10:49:28 AM UTC-6, Chris Smith wrote:
>
> Paste of the result looks bad but it does something like you are asking.
>
> /c
>
> On Wednesday, January 27, 2016 at 10:48:53 AM UTC-6, Chris Smith wrote:
>
>> So you might try a helper function something like:
>>
>> >>> combine_like_radicals(sqrt(x)*sqrt(y) + root(2*pi*x,3))
>> xy − −  √ +2πx − − −  √ 3  
>>
>> See http://codepad.org/lqcmqzwm for code snippet.
>>
>>
>>
>> On Thursday, January 21, 2016 at 2:55:32 PM UTC-6, Aaron Meurer wrote:
>>
>>> You can do it if you omit the assumptions. Otherwise, the only way is to 
>>> use Pow(2*pi, Rational(1, 2), evaluate=False).
>>>
>>> Aaron Meurer
>>>
>>> On Wed, Jan 20, 2016 at 4:31 PM, Jonathan Crall <[email protected]> 
>>> wrote:
>>>
>>>> I saw under 
>>>> http://docs.sympy.org/dev/tutorial/simplification.html#powsimp 
>>>> that it is impossible to combine radicals using powersimp:
>>>>
>>>> "This means that it will be impossible to undo this identity with 
>>>> powsimp(), because even if powsimp() were to put the bases together, 
>>>> they would be automatically split apart again."
>>>>
>>>> I was wondering if it was possible to do this any other way. 
>>>>
>>>> For a toy example I have 
>>>>
>>>>         import sympy
>>>>         L = sympy.symbols('L', real=True, finite=True, positive=True)
>>>>         sympy.sqrt(L) * sympy.sqrt(pi)
>>>>
>>>> and I would like to have it return sympy.sqrt(L * pi)
>>>> Is there any way to do this?
>>>>
>>>> What I'd really like is if it combined these terms in this real 
>>>> example: 
>>>>
>>>>         import simplify
>>>>         import vtool as vt
>>>>         import sympy
>>>>         sigma, dist, L = sympy.symbols('sigma, distij, L', real=True, 
>>>> finite=True, positive=True)
>>>>         kernel = (1 / sympy.sqrt(sigma ** 2 * 2 * sympy.pi)) * 
>>>> sympy.exp((-dist ** 2) / (2 * sigma ** 2))
>>>>         phi = (1 / L) * kernel
>>>>         logphi = sympy.simplify(sympy.log(phi))
>>>>         logphi = sympy.logcombine(logphi)
>>>>
>>>> So I would get 
>>>> -distij**2/(2*sigma**2) - log(sqrt(2 * pi)*L*sigma)
>>>>
>>>> instead of 
>>>>
>>>> -distij**2/(2*sigma**2) - log(sqrt(2)*sqrt(pi)*L*sigma)
>>>>
>>>>
>>>>
>>>> -- 
>>>> 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 post to this group, send email to [email protected].
>>>> Visit this group at https://groups.google.com/group/sympy.
>>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/sympy/c2d73b5e-60d0-4140-af8e-033ec7234890%40googlegroups.com
>>>>  
>>>> <https://groups.google.com/d/msgid/sympy/c2d73b5e-60d0-4140-af8e-033ec7234890%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>>

-- 
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 post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/48265525-a858-438c-b574-25c98bacc18d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to