Since SeriesData
is a documented data structure, it seems suitable to take advantage of it. We can calculate the series just once, which in this case will be more than 40 times faster than DumpsterDoofus's method and 12 times faster than Dr. Wolgang Hintze's. (It's tricky to time, since Series
/SeriesData
cache their results to make subsequent calls a bit faster. I had forgotten that. In this comparison each timing was made with a fresh kernel. If the maximum power goes up to 100
, the ratios of the times decreases to 12 and 2.5 times, respective, although the absolute time difference increases.)
With[{s = Series[(x^3/6)/(E^x - 1 - x - x^2/2), {x, 0, 25}]},
PadRight[s[[3]], -s[[4]] + s[[5]]] Range[s[[4]], s[[5]] - 1]! // Denominator
]
(* {1, 4, 40, 160, ..., 24687170027520000} *)
SeriesData
has the structure
SeriesData[x, x0, coefficients, min, max, den]
where x
is the variable, x0
is the center, min
and max
are the limits on the powers, and den
is the denominator in fractional power series. The code above assumes the usual sort of power series with den == 1
. The coefficients are obtained with s[[3]]
. The range of powers is s[[4]]
to s[[5]]
. One thing to beware is that the coefficients are trimmed of zeros at the ends. This is the reason for the padding and the arguments to Range
. To always get the coefficients from the power 0
to the maximum power, one could use
PadRight[ArrayPad[s[[3]], {s[[4]], 0}], s[[5]]] Range[0, s[[5]] - 1]!