Getting the coefficients of a polynomial



Slightly modified code from Wilhelm: (only one call to expand)

(%i1) coefficient_list(p,s) :=
block([n],
  p : expand(p),
  n : hipow(p,s),
  map(lambda([u],coeff(p,s,u)),makelist(i,i,0,n)))$

Function that works when coefficients are rational numbers

  (%i2) cp(p,x) := (p : expand(p), subst(x=1, if ?mplusp(p) then args(p) else [p]))$

Return a fairly sparse polynomial of degree at most n:

  (%i3) rp(n) := block([p : 0], while n > 0 do (p : p + (if random(100)=0 then random(10) * x^n else 0), n : n-1), p)$

Tests:
 (%i4) p : rp(10^5)$
 (%i5) showtime : all$
 Evaluation took 0.0000 seconds (0.0000 elapsed)

 (%i6) coefficient_list(p,x)$
 Evaluation took 27.4900 seconds (27.4900 elapsed)
 (%i7) cp(p,x)$
 Evaluation took 0.3200 seconds (0.3200 elapsed)

The function cp fails on polynomials such as 1 + x^2 + sqrt(2)*x^2, but maybe it shows what kind of speed should be possible.

--Barton
________________________________________

Wilhelm Haager <wilhelm.haager at htlstp.ac.at> writes:


> coefficient_list(p,s) :=
> block(
>  [n:floor(hipow(p,s))],
>  p:expand(p),
>  map(lambda([u],coeff(p,s,u)),makelist(i,i,0,n))
> )$