Combine sqrt?

47 views
Skip to first unread message

Paul Royik

unread,
Feb 26, 2015, 10:32:11 AM2/26/15
to sy...@googlegroups.com
What is the best way to convert sqrt(x)*sqrt(x-4) to sqrt(x^2-4x) or (x^2+5x+4)/sqrt(x)/sqrt(x-4) to (x^2+5x+4)/sqrt(x^2-4x)

I tried replace, but it doesn't work in second case.

John Peterson

unread,
Feb 26, 2015, 12:50:15 PM2/26/15
to sy...@googlegroups.com


On Thursday, February 26, 2015 at 8:32:11 AM UTC-7, Paul Royik wrote:
What is the best way to convert sqrt(x)*sqrt(x-4) to sqrt(x^2-4x) or (x^2+5x+4)/sqrt(x)/sqrt(x-4) to (x^2+5x+4)/sqrt(x^2-4x)

I tried replace, but it doesn't work in second case.

Be careful, they aren't equal for all values of x, but subs() will do this if you are willing to be explicit...

#!/usr/bin/env python
from sympy import *
print sympify('(x**2 + 5*x + 4) / sqrt(x) / sqrt(x-4)').subs(sympify('sqrt(x)*sqrt(x-4)'), sympify('sqrt(x**2 - 4*x)'))

Output:

(x**2 + 5*x + 4)/sqrt(x**2 - 4*x) 
 

Ondřej Čertík

unread,
Feb 26, 2015, 12:51:17 PM2/26/15
to sympy
Hi Paul,
How did you use replace?

Note that this conversion is only valid for some "x", not all complex
"x", for example if you use imaginary unit for "x":

In [14]: x = I

In [15]: sqrt(x)*sqrt(x - 4)
Out[15]: (-1)**(1/4)*sqrt(-4 + I)

In [16]: (sqrt(x)*sqrt(x - 4)).n()
Out[16]: -1.24962106768765 + 1.60048518044024*I

In [17]: sqrt(x**2 - 4*x)
Out[17]: sqrt(-1 - 4*I)

In [18]: sqrt(x**2 - 4*x).n()
Out[18]: 1.24962106768765 - 1.60048518044024*I

Ondrej

Paul Royik

unread,
Feb 26, 2015, 1:15:03 PM2/26/15
to sy...@googlegroups.com
I have assumption that x is positive.
a, b = Wild...
I use like f.replace(sqrt(a)*sqrt(b), sqrt((a*b).expand()))

Paul Royik

unread,
Feb 26, 2015, 1:18:42 PM2/26/15
to sy...@googlegroups.com
I need something more general

Aaron Meurer

unread,
Feb 26, 2015, 4:23:31 PM2/26/15
to sy...@googlegroups.com
See http://docs.sympy.org/latest/tutorial/simplification.html#powers.
powsimp(expr, force=True) will do what you want.

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 http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/a20128fc-d1bd-479e-8845-a581cfc6b7d4%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Paul Royik

unread,
Feb 26, 2015, 6:05:19 PM2/26/15
to sy...@googlegroups.com
It doesn't work if x is positive.

In fact, when x is positive, 1/sqrt(x*(x-4)) automatically converted to 1/sqrt(x)/sqrt(x-4)

Ondřej Čertík

unread,
Feb 26, 2015, 6:52:32 PM2/26/15
to sympy
On Thu, Feb 26, 2015 at 4:05 PM, Paul Royik <distan...@gmail.com> wrote:
> It doesn't work if x is positive.

Indeed, looks like a bug. As a workaround, you can always substitute
general "x" for the positive "x", then it will work:

In [4]: x = Symbol("x", positive=True)

In [5]: powsimp(sqrt(x)*sqrt(x-4), force=True)
Out[5]:
___ _______
╲╱ x ⋅╲╱ x - 4

In [6]: powsimp((sqrt(x)*sqrt(x-4)).subs(x, Symbol("x"), force=True)
...:
KeyboardInterrupt

In [6]: powsimp((sqrt(x)*sqrt(x-4)).subs(x, Symbol("x")), force=True)
Out[6]:
___________
╲╱ x⋅(x - 4)


> In fact, when x is positive, 1/sqrt(x*(x-4)) automatically converted to
> 1/sqrt(x)/sqrt(x-4)

The same trick:

In [7]: powsimp(1/(sqrt(x)*sqrt(x-4)).subs(x, Symbol("x")), force=True)
Out[7]:
1
─────────────
___________
╲╱ x⋅(x - 4)


Ondrej
> https://groups.google.com/d/msgid/sympy/dc099057-8529-4f12-ae42-ed7f9e89b98e%40googlegroups.com.

Paul Royik

unread,
Feb 26, 2015, 7:20:11 PM2/26/15
to sy...@googlegroups.com
Thank you.
Got it!

Aaron Meurer

unread,
Feb 26, 2015, 9:27:42 PM2/26/15
to sy...@googlegroups.com
Ah, apparently the core is looking at assumptions and automatically
simplifies based on them. I don't think that any automatic
simplification should happen based on assumptions.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/b9754512-debb-4099-bfab-3724b3be1568%40googlegroups.com.

Paul Royik

unread,
Feb 27, 2015, 3:59:03 AM2/27/15
to sy...@googlegroups.com
I agree.
Reply all
Reply to author
Forward
0 new messages