math - How can we truncate a power series and convert it to a polynomial in sympy? -
i truncate power series in sympy , use result polynomial. i'm doing bit ugly (example degree 4):
truncated_series = 0 i,term in zip(range(5), f.lseries()): truncated_series += term
is there better way?
do mean this?
in [1]: f = sin(x) in [2]: s = series(f, x, 0, 5) in [3]: s out[3]: 3 x / 5\ x - -- + o\x / 6 in [4]: s.removeo() out[4]: 3 x - -- + x 6
these steps straightforward.
if instead instist on using .lseries(), do:
in [5]: import itertools in [6]: add(*itertools.islice(f.lseries(), 5)) out[6]: 9 7 5 3 x x x x ------ - ---- + --- - -- + x 362880 5040 120 6
explanation: islice picks 5 elements generator, passed arguments add class, managed sympy's additions.
Comments
Post a Comment