Expr.series get non-zero coefficients

44 views
Skip to first unread message

nikolas

unread,
Sep 12, 2012, 8:42:30 PM9/12/12
to sy...@googlegroups.com
Hey guys, 

for a series expansion of some function f(x) about some point x0 up to order n+1, I would like to easily generate the sequence of all coefficients.
I.e. if we have

f(x) = a0 + a1 * (x-x0) + a2 * (x-x0)**2 + ... + an (x-x0)**n + O((x-x0)**n)

is there a straightforward way to obtain a generator for the sequence a0, a1, a2, ... an that includes the non-zero terms?
With the current implementation of series, I get:

>>> f = x + 5 * x**3
>>> coeffs = series(f, x, 0, n = None)
>>> for c in coeffs:
>>>     print str(c) + ", ",
x, 5 * x**3,

It would be nice to add a flag, e.g. include_nozero=True such that
>>> f = x + 5 * x**3
>>> coeffs = series(f, x, 0, n = None)
>>> for c in coeffs:
>>>     print str(c) + " + ",
0, x, 0, 5 * x**3,

Then I could simply divide each term by (x-x0)**k to get the actual coefficient...
Is there some other way to achieve this functionality easily? It would seem that under the hood the machinery to do all this would already be in place...
Thanks!

Nikolas



nikolas

unread,
Sep 12, 2012, 9:07:44 PM9/12/12
to sy...@googlegroups.com
Follow-up: I think I have found a way that works.
I guess if one is interested in expanding about 0, one can simply do 
>>> f = x + 5 * x**3
>>> coeffs = reversed(series(f, x, 0, n = 4).as_poly(x).all_coeffs())
>>> list(coeffs)
    [0, 1, 0, 5]

So, to expand about x0!= 0, I can substitute x->x0 + x1 and expand in orders of x1.
If this is a bad way to do it, please let me know!
Thanks,
Nikolas

Chris Smith

unread,
Sep 12, 2012, 10:15:11 PM9/12/12
to sy...@googlegroups.com
On Thu, Sep 13, 2012 at 6:52 AM, nikolas <nte...@stanford.edu> wrote:
> Follow-up: I think I have found a way that works.
> I guess if one is interested in expanding about 0, one can simply do
>>>> f = x + 5 * x**3
>>>> coeffs = reversed(series(f, x, 0, n = 4).as_poly(x).all_coeffs())
>>>> list(coeffs)
> [0, 1, 0, 5]
>
> So, to expand about x0!= 0, I can substitute x->x0 + x1 and expand in orders
> of x1.
> If this is a bad way to do it, please let me know!

I wrote about this in the docstring of Expr.series, I believe. Try
"help(Expr.series)".

Nikolas Tezak

unread,
Sep 12, 2012, 10:32:40 PM9/12/12
to sy...@googlegroups.com
Hmm, I might be missing something, but it seems to me that that is not addressing the particular point I asked about, namely that the current behavior of the n=None lazy term generation does not yield 0 whenever there is no contribution at a particular order.
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>

Aaron Meurer

unread,
Sep 12, 2012, 10:35:19 PM9/12/12
to sy...@googlegroups.com
You could also use the coeff() method, like

s = series(...)
coeffs = [s.coeff(x, n) for n in range(10)]

I think in SymPy 0.7.1 or earlier, you have to use just coeff(x**n),
which won't work for n == 0.

Aaron Meurer

Chris Smith

unread,
Sep 13, 2012, 12:31:49 AM9/13/12
to sy...@googlegroups.com
On Thu, Sep 13, 2012 at 6:27 AM, nikolas <nte...@stanford.edu> wrote:
> Hey guys,
>
> for a series expansion of some function f(x) about some point x0 up to order
> n+1, I would like to easily generate the sequence of all coefficients.
> I.e. if we have
>
> f(x) = a0 + a1 * (x-x0) + a2 * (x-x0)**2 + ... + an (x-x0)**n + O((x-x0)**n)
>
> is there a straightforward way to obtain a generator for the sequence a0,
> a1, a2, ... an that includes the non-zero terms?


Part of the problem of including this in general is that in general
the exponents need not be integers and if you got a list of
coefficients, how would you know what terms they went with.

>>> series(sqrt(x)/(x+sin(x)), x)
1/(2*x**(1/2)) + x**(3/2)/24 + x**(7/2)/720 - x**(11/2)/120960 + O(x**6)

Above, the powers are -1/2+2*i for i in range(n). If one had a series
object then one of its methods might be "coeffs" and such a method
might first return (-1/2, 2) -- the first exponent and the step size
-- followed by the coefficients, 1/2, 1/24, 1/720, -1/120960. But that
seems a little complicated.

Aaron Meurer

unread,
Sep 13, 2012, 1:00:33 AM9/13/12
to sy...@googlegroups.com
Actually, this can be useful, but you're right that it is nontrivial.
See https://github.com/sympy/sympy/wiki/UD-series and
https://github.com/sympy/sympy/wiki/UD-Sequences-and-formal-power-series-prototype
for (mostly Alexey's) ideas on this.

And by the way, even if the exponents are integers, they won't be
positive integers if you expand around a pole, and that gives the same
issue.

Aaron Meurer
Reply all
Reply to author
Forward
0 new messages