Coefficients of a series in Maple

369 Views Asked by At

I have been using

seq(coeff(series( f(x), x, n+1), x, n), n = 0 .. 40);

to obtain the coefficients of series expansions. It seems efficient enough until some comparisons are made with other platforms.
For example: the script

seq(coeff(series(1/mul(1-x^j, j = 1 .. 11), x, n+1), x, n), n = 0 .. 70);

takes 0.134 seconds to calculate in Maple but the same script in Mathematica

CoefficientList[Series[1/Product[1 - x^j, {j, 1, 11}], {x, 0, 70}], x]

takes 0.00739489 seconds. Pari has a similar fast calculation time.

The question becomes: Is there a faster, or more efficient, method to calculate the coefficients of a series?

2

There are 2 best solutions below

0
On BEST ANSWER

This takes approximately 0.005 sec in my Maple 2019:

PolynomialTools:-CoefficientList(convert(series(1/mul(1-x^j,j=1..11),x,71),
                                         polynom),x);

These two steps take (in total) approximately 0.002 sec in my Maple 2019:

S := series(1/mul(1-x^j,j=1..11),x,71):
seq(coeff(S,x,j),j=0..70):

The relative performance of the above two techniques may vary as the degree gets very large. If I recall correctly the CoefficientList procedure was intended for doing a single pass through the structure, and thus may scale up better than the approach of calling coeff many times.

On such a small example as yours, measuring the performance of this operation is delicate -- you have to be careful not to trick yourself.

For example, you may well wish to suppress printing of the results, because the computations are so very fast here. You don't really want a major portion of your measurements to reflect the time taken for the programs' interfaces to render/print/pretty-print these results, do you?

And, as James mentioned, you may want to avoid inadvertent duplicate re-creation of the series 70 times. Even deliberately doing such duplication in all those programs may be problematic for a comparison, as the programs may differ in how they each memoize parts of the computation.

Also, how will you ensure that none of these mentioned program are not (by chance) doing a garbage-collection (memory management act) during your requested computation. That might conceivably consume as much time as your actual computation.

And what about initialization of the commands in question? Are you deliberately timing how long the first such computation takes in a fresh session, or how long such computations take on average (after all relevant functions are loaded into memory)?

Just to illustrate how delicate such a timing measurement may be for such a small and quick computation, compare these:

restart;
CodeTools:-Usage( seq(coeff(series(1/mul(1-x^j,j=1..11),x,71),x,j),j=0..70) ):
  memory used=0.81MiB, alloc change=0 bytes, cpu time=3.00ms, real time=2.00ms, gc time=0ns

restart;
CodeTools:-Usage( PolynomialTools:-CoefficientList(convert(series(1/mul(1-x^j,j=1..11),x,71),polynom),x) ):
  memory used=0.61MiB, alloc change=0 bytes, cpu time=3.00ms, real time=2.00ms, gc time=0ns

restart;
CodeTools:-Usage( seq(coeff(series(sin(x),x,12),x,j),j=0..11), quiet ):
CodeTools:-Usage( seq(coeff(series(1/mul(1-x^j,j=1..11),x,71),x,j),j=0..70) ):
  memory used=0.81MiB, alloc change=0 bytes, cpu time=2.00ms, real time=2.00ms, gc time=0ns

restart;
CodeTools:-Usage( seq(coeff(series(sin(x),x,12),x,j),j=0..11), quiet ):
CodeTools:-Usage( PolynomialTools:-CoefficientList(convert(series(1/mul(1-x^j,j=1..11),x,71),polynom),x) ):
  memory used=0.58MiB, alloc change=0 bytes, cpu time=6.00ms, real time=6.00ms, gc time=0ns

restart;
CodeTools:-Usage( seq(coeff(series(1/mul(1-x^j,j=1..11),x,71),x,j),j=0..70),
                  iterations=100 ):
  memory used=358.69KiB, alloc change=0 bytes, cpu time=1.13ms, real time=1.13ms, gc time=136.30us

restart;
CodeTools:-Usage( PolynomialTools:-CoefficientList(convert(series(1/mul(1-x^j,j=1..11),x,71),polynom),x),
                  iterations=100 ):
  memory used=27.55KiB, alloc change=0 bytes, cpu time=140.00us, real time=140.00us, gc time=0ns

restart;
S := CodeTools:-Usage( series(1/mul(1-x^j,j=1..11),x,71) ):
  memory used=0.51MiB, alloc change=0 bytes, cpu time=1000.00us, real time=1000.00us, gc time=0ns
CodeTools:-Usage( seq(coeff(S,x,j),j=0..70) ):
  memory used=3.52KiB, alloc change=0 bytes, cpu time=0ns, real time=0ns, gc time=0ns

restart;
S := CodeTools:-Usage( series(1/mul(1-x^j,j=1..11),x,71), iterations=100 ):
  memory used=9.60KiB, alloc change=0 bytes, cpu time=30.00us, real time=30.00us, gc time=0ns
CodeTools:-Usage( seq(coeff(S,x,j),j=0..70), iterations=100 ):
  memory used=2.84KiB, alloc change=0 bytes, cpu time=50.00us, real time=50.00us, gc time=0ns
0
On
S:= series(1/mul(1-x^j,j=1..11),x,71):
seq(coeff(S,x,j),j=0..70);

took 0.031 seconds in Maple 2019 on my computer. YMMV.