It is possible to combine terms under a radical?

74 views
Skip to first unread message

Jonathan Crall

unread,
Jan 20, 2016, 4:31:15 PM1/20/16
to sympy
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)



Aaron Meurer

unread,
Jan 21, 2016, 3:55:32 PM1/21/16
to sy...@googlegroups.com
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

--
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 sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
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.
For more options, visit https://groups.google.com/d/optout.

Chris Smith

unread,
Jan 27, 2016, 11:48:53 AM1/27/16
to sympy
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.

Chris Smith

unread,
Jan 27, 2016, 11:49:28 AM1/27/16
to sympy
Paste of the result looks bad but it does something like you are asking.

/c

Chris Smith

unread,
Jan 27, 2016, 9:33:48 PM1/27/16
to sympy
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)
Reply all
Reply to author
Forward
0 new messages