Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Series expansion of the square root gives wrong result #11407

Closed
astholkohtz opened this issue Jul 18, 2016 · 2 comments · Fixed by #19824
Closed

Series expansion of the square root gives wrong result #11407

astholkohtz opened this issue Jul 18, 2016 · 2 comments · Fixed by #19824
Labels
Easy to Fix This is a good issue for new contributors. Feel free to work on this if no one else has already. series

Comments

@astholkohtz
Copy link

Whenever the constant term on the square root is a sum of 2 or more symbols, the series() function gets the expansion wrong

In [1]: from sympy import *

In [2]: a,b,c,x = symbols('a b c x')

In [3]: sqrt(a+b + c*x).series(x,0,1)
Out[3]: 5*sqrt(a + b)/16 + 15*b/(16*sqrt(a + b)) - 5*b**2*sqrt(a + b)/(16*(a**2 + 2*a*b + b**2)) + b**3*sqrt(a + b)/(16*(a**3 + 3*a**2*b + 3*a*b**2 + b**3)) + 15*a/(16*sqrt(a + b)) - 5*a*b*sqrt(a + b)/(8*(a**2 + 2*a*b + b**2)) + 3*a*b**2*sqrt(a + b)/(16*(a**3 + 3*a**2*b + 3*a*b**2 + b**3)) - 5*a**2*sqrt(a + b)/(16*(a**2 + 2*a*b + b**2)) + 3*a**2*b*sqrt(a + b)/(16*(a**3 + 3*a**2*b + 3*a*b**2 + b**3)) + a**3*sqrt(a + b)/(16*(a**3 + 3*a**2*b + 3*a*b**2 + b**3)) + O(x)

In [4]: sqrt(a+b+c + c*x).series(x,0,1)
Out[4]: 5*sqrt(a + b + c)/16 + 15*c/(16*sqrt(a + b + c)) - 5*c**2*sqrt(a + b + c)/(16*(a**2 + 2*a*b + 2*a*c + b**2 + 2*b*c + c**2)) + c**3*sqrt(a + b + c)/(16*(a**3 + 3*a**2*b + 3*a**2*c + 3*a*b**2 + 6*a*b*c + 3*a*c**2 + b**3 + 3*b**2*c + 3*b*c**2 + c**3)) + 15*b/(16*sqrt(a + b + c)) - 5*b*c*sqrt(a + b + c)/(8*(a**2 + 2*a*b + 2*a*c + b**2 + 2*b*c + c**2)) + 3*b*c**2*sqrt(a + b + c)/(16*(a**3 + 3*a**2*b + 3*a**2*c + 3*a*b**2 + 6*a*b*c + 3*a*c**2 + b**3 + 3*b**2*c + 3*b*c**2 + c**3)) - 5*b**2*sqrt(a + b + c)/(16*(a**2 + 2*a*b + 2*a*c + b**2 + 2*b*c + c**2)) + 3*b**2*c*sqrt(a + b + c)/(16*(a**3 + 3*a**2*b + 3*a**2*c + 3*a*b**2 + 6*a*b*c + 3*a*c**2 + b**3 + 3*b**2*c + 3*b*c**2 + c**3)) + b**3*sqrt(a + b + c)/(16*(a**3 + 3*a**2*b + 3*a**2*c + 3*a*b**2 + 6*a*b*c + 3*a*c**2 + b**3 + 3*b**2*c + 3*b*c**2 + c**3)) + 15*a/(16*sqrt(a + b + c)) - 5*a*c*sqrt(a + b + c)/(8*(a**2 + 2*a*b + 2*a*c + b**2 + 2*b*c + c**2)) + 3*a*c**2*sqrt(a + b + c)/(16*(a**3 + 3*a**2*b + 3*a**2*c + 3*a*b**2 + 6*a*b*c + 3*a*c**2 + b**3 + 3*b**2*c + 3*b*c**2 + c**3)) - 5*a*b*sqrt(a + b + c)/(8*(a**2 + 2*a*b + 2*a*c + b**2 + 2*b*c + c**2)) + 3*a*b*c*sqrt(a + b + c)/(8*(a**3 + 3*a**2*b + 3*a**2*c + 3*a*b**2 + 6*a*b*c + 3*a*c**2 + b**3 + 3*b**2*c + 3*b*c**2 + c**3)) + 3*a*b**2*sqrt(a + b + c)/(16*(a**3 + 3*a**2*b + 3*a**2*c + 3*a*b**2 + 6*a*b*c + 3*a*c**2 + b**3 + 3*b**2*c + 3*b*c**2 + c**3)) - 5*a**2*sqrt(a + b + c)/(16*(a**2 + 2*a*b + 2*a*c + b**2 + 2*b*c + c**2)) + 3*a**2*c*sqrt(a + b + c)/(16*(a**3 + 3*a**2*b + 3*a**2*c + 3*a*b**2 + 6*a*b*c + 3*a*c**2 + b**3 + 3*b**2*c + 3*b*c**2 + c**3)) + 3*a**2*b*sqrt(a + b + c)/(16*(a**3 + 3*a**2*b + 3*a**2*c + 3*a*b**2 + 6*a*b*c + 3*a*c**2 + b**3 + 3*b**2*c + 3*b*c**2 + c**3)) + a**3*sqrt(a + b + c)/(16*(a**3 + 3*a**2*b + 3*a**2*c + 3*a*b**2 + 6*a*b*c + 3*a*c**2 + b**3 + 3*b**2*c + 3*b*c**2 + c**3)) + O(x)

As long as the constant term is only one, series() gives the correct result

In [5]: sqrt(a + c*x).series(x,0,1)
Out[5]: sqrt(a) + O(x)

In [6]: sqrt(a + (a+b)*x).series(x,0,1)
Out[6]: sqrt(a) + O(x)

In [7]: sqrt(a + (a+b+c)*x).series(x,0,1)
Out[7]: sqrt(a) + O(x)

In [8]: sqrt(a + (a+b+c)*x).series(x,0,2)
Out[8]: x*(sqrt(a)/2 + b/(2*sqrt(a)) + c/(2*sqrt(a))) + sqrt(a) + O(x**2)

@astholkohtz astholkohtz changed the title series expansion of the square root gives wrong result Series expansion of the square root gives wrong result Jul 18, 2016
skirpichev added a commit to skirpichev/diofant that referenced this issue Jul 19, 2016
skirpichev added a commit to skirpichev/diofant that referenced this issue Jul 22, 2016
@oscarbenjamin
Copy link
Contributor

It seems that SymPy comes out with an unnecessarily complicated form:

In [56]: sqrt(a+b+c*x).series(x, 0, 1)                                                                                                                        
Out[56]: 
    _______                        2   _______                3   _______                                     _______                 2   _______           
5╲╱ a + b        15b          5b ╲╱ a + b                b ╲╱ a + b                 15a         5ab╲╱ a + b             3ab ╲╱ a + b            
─────────── + ──────────── - ──────────────────── + ────────────────────────────── + ──────────── - ─────────────────── + ────────────────────────────── -16            _______      ⎛ 2            2⎞      ⎛ 3      2          2    3⎞        _______     ⎛ 2            2⎞      ⎛ 3      2          2    316╲╱ a + b    16⎝a  + 2ab + b ⎠   16⎝a  + 3a b + 3ab  + b ⎠   16╲╱ a + b    8⎝a  + 2ab + b ⎠   16⎝a  + 3a b + 3ab  + b ⎠   1

     2   _______                2     _______                    3   _______                
  5a ╲╱ a + b              3a b╲╱ a + b                    a ╲╱ a + b                 
─────────────────── + ────────────────────────────── + ────────────────────────────── + O(x)
  ⎛ 2            2⎞      ⎛ 3      2          2    3⎞      ⎛ 3      2          2    36⎝a  + 2ab + b ⎠   16⎝a  + 3a b + 3ab  + b ⎠   16⎝a  + 3a b + 3ab  + b ⎠       

In [57]: simplify(_)                                                                                                                                          
Out[57]: 
 3        2      2      3       
b  + 3ab  + 3a b + a  + O(x)
────────────────────────────────
    _______ ⎛ 2            2⎞   
  ╲╱ a + b ⎝a  + 2ab + b ⎠   

In [58]: factor(_)                                                                                                                                            
Out[58]: 
 3        2      2      3       
b  + 3ab  + 3a b + a  + O(x)
────────────────────────────────
                  5/2           
           (a + b)  

The numerator there is (a+b)**3 so it should somehow simplify to sqrt(a+b).

@sachin-4099
Copy link
Contributor

sachin-4099 commented Jun 1, 2020

On the current master:

In [1]: a,b,c,x = symbols('a b c x')

In [2]: sqrt(a+b + c*x).series(x,0,1)
Out[2]:
  _______
\/ a + b  + O(x)

In [3]: sqrt(a+b+c + c*x).series(x,0,1)
Out[3]:
  ___________
\/ a + b + c  + O(x)

This issue seems resolved and thus, only requires a test in order to close.

@sachin-4099 sachin-4099 added the Easy to Fix This is a good issue for new contributors. Feel free to work on this if no one else has already. label Jun 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Easy to Fix This is a good issue for new contributors. Feel free to work on this if no one else has already. series
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants