Possible bug in simplify() and a question

54 views
Skip to first unread message

div...@umich.edu

unread,
Apr 11, 2018, 11:28:49 AM4/11/18
to sympy
The code below is run in Python 3.6 (Anaconda install), sympy 1.1.1:
#!/usr/bin/env python
import sympy as spy
import sympy.abc as abc
from sympy import pprint

n = abc.n
N = abc.N

EY = 2*N*(1-(1-1/(2*N))**n)
pprint(n-EY)
print('-'*80, '\n')
pprint((n-EY).series(N, x0=spy.oo).simplify())
print('-'*80, '\n')
pprint((n-EY).series(N, x0=spy.oo))
print('-'*80, '\n')
pprint((n-EY).series(N, x0=spy.oo).removeO().simplify())

When the code is run, I see the following output:

      ⎛           n    ⎞    
      ⎜  ⎛     1 ⎞     ⎟    
- 2⋅N⋅⎜- ⎜1 - ───⎟  + 1⎟ + n
      ⎝  ⎝    2⋅N⎠     ⎠    
-------------------------------------------------------------------------------- 

 ⎛1        ⎞
O⎜──; N → ∞⎟
 ⎜ 6       ⎟
 ⎝N        ⎠
-------------------------------------------------------------------------------- 

   6      5        4      3        2             5      4      3      2       
  n      n     17⋅n    5⋅n    137⋅n     n       n      n    7⋅n    5⋅n    n   
───── - ──── + ───── - ──── + ────── - ───   - ──── + ─── - ──── + ──── - ──  
23040   1536    4608   512    11520    192     1920   192   384    192    80  
────────────────────────────────────────── + ─────────────────────────────── +
                     5                                       4                
                    N                                       N                 

   4    3       2           3    2         2                   
  n    n    11⋅n    n      n    n    n    n    n               
 ─── - ── + ───── - ──   - ── + ── - ──   ── - ─               
 192   32    192    32     24   8    12   4    4    ⎛1        ⎞
 ───────────────────── + ────────────── + ────── + O⎜──; N → ∞⎟
            3                   2           N       ⎜ 6       ⎟
           N                   N                    ⎝N        ⎠
-------------------------------------------------------------------------------- 

  ⎛      4                3 ⎛   2          ⎞        2 ⎛ 3      2           ⎞  
n⋅⎝5760⋅N ⋅(n - 1) + 960⋅N ⋅⎝- n  + 3⋅n - 2⎠ + 120⋅N ⋅⎝n  - 6⋅n  + 11⋅n - 6⎠ +
──────────────────────────────────────────────────────────────────────────────
                                                                              
                                                                              

      ⎛   4       3       2            ⎞    5       4       3        2        
 12⋅N⋅⎝- n  + 10⋅n  - 35⋅n  + 50⋅n - 24⎠ + n  - 15⋅n  + 85⋅n  - 225⋅n  + 274⋅n
──────────────────────────────────────────────────────────────────────────────
       5                                                                      
23040⋅N                                                                       

      ⎞
 - 120⎠
───────


simplify() seems to be removing all terms in the asymptotic expansion (second printed item). The third printed item is correct and does not use simplify(). The fourth print item, which uses simplify() after removing the O(), is also correct.

Question: If I want to factor()/simplify() each term in the asymptotic expansion, what is the best way to do that?
Message has been deleted

div...@umich.edu

unread,
Apr 12, 2018, 8:03:30 AM4/12/18
to sympy
To factor/simplify each term, the best way appears to be to use collect and pass the simplification function as an argument. So I can use the following syntax:

spy.collect((n-EY).series(N,x0 = spy.oo).removeO(),N, spy.factor)

It would be really nice if sympy can support collect() as a member function of expression objects. Only the part about a possible bug in sympy.simplify() stands. 

Aaron Meurer

unread,
Apr 12, 2018, 5:52:08 PM4/12/18
to sy...@googlegroups.com
Looks like the issue comes from as_numer_denom. I opened
https://github.com/sympy/sympy/issues/14622 for this.

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/a79426c5-ff10-454b-b79a-f5381d9fbcc7%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages