Is there a way to compute the nth term of a Taylor series expansion without defining the value of n? In the case of sine it is (-1)**n*x**(2*n+1)/(2*n+1)!
. In Maxima it is a (somewhat) related form to do it with powerseries(sin(x), x, 0)
.
Almost ten years after the question was raised, Sympy improved and Formal Power Series are implemented. Only issue is that part of the API isn't covered in the doc (in particular the class properties, thus see sympy/series/formal.py source code to dig into it).
So here is an example to obtain the nth term of the power series of the sine function, as originally asked:
from sympy import simplify, fps, sin
from sympy.abc import x, n
FormalPowerSeries
are computed with the key function fps
:
sin_fps = fps(sin(x))
The result isn't as compact as what stated in the question since the series summation is done for all integers while only odd terms are non zeros.
From that Series, the formula of each coefficient can be accessed using the .ak
property which returns a SeqFormula
sequence, which itself has a .formula
property:
ak = sin_fps.ak.formula
Now, on the special case of sine and cosine, the coefficient formula is a Piecewise-defined function object). From it, I could extract only the odd coefficients by digging into the .args
of that Piecewise expression:
ak_odd = ak.args[0][0]
Finally, this expression can be simplified by replacing k by 2*n+1. However, this requires getting a reference to the Dummy variable k that got actually used when building the formula. Looking at FormalPowerSeries
's source code gave me this:
k = sin_fps.ak.variables[0]
ak_odd.subs(k, 2*n+1)
and this matches the classical formulation of sine series coefficients by having in mind that Γ(2n+2) = (2n+1)! (cf Gamma function).