Can someone help me with this command?

162 views
Skip to first unread message

Belkiss Anane

unread,
Feb 12, 2018, 2:56:16 AM2/12/18
to sympy
Hello! I have SymPy on my computer but it crashed and I'm using  someone else's to do some math problems. Unfortunately, the online SymPy times out really fast. Can someone run these commands for me please?

from sympy import *
t = Symbol('t')
f = 1 / ((1-t) * (1-t**5) * (1-t**10) * (1-t**25) * (1-t**50) * (1-t**100))
f.series(t,0,784)

I am just looking for the coefficient of t**783!

Thank you very much, you'll be saving my night! 

Kalevi Suominen

unread,
Feb 12, 2018, 3:09:05 AM2/12/18
to sympy

I get  683772⋅t^783, but I have not checked that for correctness.

Kalevi Suominen  

Chris Smith

unread,
Feb 16, 2018, 8:50:46 PM2/16/18
to sympy
Just out of curiosity, do we have anything to generate the coefficients of terms of a rational function's Taylors series? I read the wiki (https://en.wikipedia.org/wiki/Rational_function) on the "method of generating functions" but that seems to be pretty significant computation for this particular problem. I was able to compute the term with the series (as Belkiss indicated) but is there a better way?

Leonid Kovalev

unread,
Feb 16, 2018, 11:51:29 PM2/16/18
to sympy
There are much more efficient tools in sympy/polys/ring_series.py that could in principle replace series but are more limited in the expressions they support.
For example, this function is the reciprocal of a polynomial. Introducing this polynomial (called g) and calling rs_series_inversion yields the answer at once (683772*t**783): 
 
from sympy import *
from sympy.polys.ring_series import rs_series_inversion
R
, t = ring('t', QQ)
g
= (1-t) * (1-t**5) * (1-t**10) * (1-t**25) * (1-t**50) * (1-t**100)
rs_series_inversion
(g, t, 784)

Well, it took a bit of time, measured with timeit at 654ms. But that's nothing compared to series which took 28.8 seconds. 

To get the specific coefficient, one can use

rs_series_inversion(g, t, 784).coeff(t**783)


Chris Smith

unread,
Feb 17, 2018, 12:36:10 PM2/17/18
to sympy
So for a rational function one could do this to get the series approximation?

def rseries(p, x, o):
    """Return truncated series of univariate rational p in
    variables x up to order o about the point x = 0.
    """
    n, d = p.as_numer_denom()
    if not all(i.is_polynomial() for i in (n, d)):
        return
    R, r = ring(x.name, QQ)
    def rp(p):
        rv = 0
        for a in Add.make_args(p.expand()):
            c, b = a.as_coeff_Mul()
            if b == 1:
                e = 0
            else:
                e = b.as_base_exp()[1]
            rv += c*r**e
        return rv
    n, d = map(rp, (n, d))
    return rs_mul(n, rs_series_inversion(d, r, o), r, o)

>>> x = var('x')
>>> rseries(x/(1-x+x**2+3*x**10),x,12)
-4*x**11 - x**10 + x**8 + x**7 - x**5 - x**4 + x**2 + x
>>> _.as_expr().coeff(x**11)
-4

Given that this is so much faster, I wonder why this is not implemented for series.Perhaps that is
part of the work that can yet be done on series: using input type to tailor that method used
to give the output.

BTW, this runs for Anane's expression without timing out on live.sympy.org.

/c

Leonid Kovalev

unread,
Feb 17, 2018, 1:22:54 PM2/17/18
to sympy
> I wonder why this is not implemented for series

It seems the idea (as stated on GSoC page) was to replace series with rs_series after rs_series is expanded to handle all functions. There was not much recent progress on that. Like with assumptions or solveset, complete replacement is hard. One can make gradual process by making series call rs_series first, and if NotImplementedError is raised, proceed with the usual series expansion. In this case, I think that the logic in your rseries function could be a part of rs_series function. Or the existing logic of rs_series function could be improved to handle quotients of two functions where each can be handled by rs_series already. I was surprised that rs_series could not handle a rational function directly, it looks like the primary use case for ring-based series manipulations.  

Chris Smith

unread,
Feb 18, 2018, 5:00:37 PM2/18/18
to sympy
So even though rs_series can produce the answer quickly, is this still the best way to do this? Following the wiki I was able to cobble together a recurrence relationship generator but the problem posed here at the outset would be "cruel and unusual" for a homework problem, wouldn't it? :-) By that I mean that the recurrence relationship has many relationships that need to be solved before even generating the coefficient of such a high ordered term. But the expression factors nicely and perhaps the individual recurrence relationships can be found fairly easily and then combined to find the answer.

factored expression: 

```
(t - 1)**6*
(t + 1)**3*
(t**2 + 1)*
(t**4 - t**3 + t**2 - t + 1)**3*
(t**4+ t**3 + t**2 + t + 1)**5*
(t**8 - t**6 + t**4 - t**2 + 1)*
(t**20 - t**15 + t**10 - t**5 + 1)**2*
(t**20 + t**15 + t**10 + t**5 + 1)**3*
(t**40 - t**30 + t**20 - t**10 + 1)
```

parag goyal

unread,
Feb 21, 2018, 6:23:25 AM2/21/18
to sympy

This code is working fine for me .I am using sympy- 1.1.1


from sympy import *
    ...: t = Symbol('t')
    ...: f = 1 / ((1-t) * (1-t**5) * (1-t**10) * (1-t**25) * (1-t**50) * (1-t**100))
    ...: f.series(t,0,784).coeff(t**783)
    ...:
Out[50]: 683772

Aaron Meurer

unread,
Feb 21, 2018, 2:55:29 PM2/21/18
to sy...@googlegroups.com
Here is some background on this problem
https://www.johndcook.com/blog/2014/07/09/making-change/.

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/321d8bc0-2978-4135-8167-f9d16e1c4ab5%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages