Getting the coefficients of a polynomial



It seems to me that if you want to create an array of coefficients, 
there are a couple of things:
sparse or dense?

e.g.  is  5*x^4+1   supposed to be [5,0,0,0,1]   or [[5,4],[0,1]]?


On 4/16/2013 2:40 PM, Stavros Macrakis wrote:
> How can I get the coefficients of a polynomial in x?
>
> Clearly I first put into into CRE form with main variable x:
>
>      poly: rat(input_poly,x)$
>
> Then what?  The internal form of the CRE lets you just "read off" the 
> coefficients at this point, but I can't think of any Maxima function 
> that lets you access that.
>
> I can loop like this:
>
>      coefs(p,v):=block([l:[]],while p # 0 do 
> (l:cons(r:ratcoef(p,v,0),l),p:(p-r)/v),l)

better would be somegthing like this  (no division, no re-ratting.)

    coefs(p,v) := (p:rat(p,v), block([l:[]], while p#0 for i:hipow(p,v) 
step -1 thru 0 do(  l:cons(r:ratcoef(p,v,i), p:p-r*v^i))  , l));

>
> but that seems like an awful lot of work (/n/ polynomial divisions). 
>  It also isn't very efficient for sparse polynomials like x^20000. 
>  You could do similar things with bothcoef or ratcoef, but again you 
> end up building /n/ intermediate polynomials as you remove terms.
>
> You might think you could use ratcoef for i:0../n/, but how do you 
> determine /n/?  hipow is inefficient in that it converts the 
> expression into general representation first (!).
>
> Am I missing some obvious solution, or do we need to add rat_hipow and 
> rat_coeffs or something?

rat_hipow(p,x):= ?cadadr(rat(p,x))

>
>       rat_hipow(x^3+a*x+1,x) => 3
> rat_coeffs(x^3+a*x+1,x) => [[3,1],[1,a],[0,1]]
>
> Edge cases to think about: CREs with negative powers, cf. 
> ratcoef(1/x^3,x,-3) => 1, ratcoef(a/(b-x),x,-5) => -a*b^4, etc.
>
>            -s
>
>
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima