RFC: extracting coefficients of a multivariate polynomial



andre maute wrote:
>>> I have to extract the coefficients of a multivariate polynomial.
>>> The monomials appearing in the multivariate polynomial are given.
>>>       
>
>   
>>> ---------------------------------------------------------------
>>> display2d : false;
>>>
>>> v : [x,y];
>>> exps : [[0,2], [0,0], [1,1], [0,1], [1,0], [2,0]];
>>> poly : a00 + a10*x + a01*y + a20*x^2 + a11*x*y + a02*y^2;
>>>
>>> my_coeff(v,exps,poly) := block(
>>>
>>>         [c,k,l,h],
>>>
>>>         c : [],
>>>         for k : 1 thru length(exps) do block(
>>>                 h : poly,
>>>                 for l : 1 thru length(v) do block(
>>>                         if exps[k][l] # 0 then block(
>>>                                 h : coeff(h,v[l],exps[k][l])
>>>                         )
>>>                 ),
>>>                 for l : 1 thru length(v) do block(
>>>                         h : subst(0,v[l],h)
>>>                 ),
>>>                 h : ratsimp(h),
>>>                 c : append(c,[h])
>>>         ),
>>>
>>>         return(c)
>>>
>>> )$
>>>
>>> my_coeff(v,exps,poly);
>>> ---------------------------------------------------------------------
>
>> You can use the 'coeff' function, perhaps several times. Of course, you
>> can write a little routine to apply to any polynomial automatically and
>> do what your my_coeff does. Don't know if it will be faster, but you can
>> try.
>
> The function my_coeff above clearly does use 'coeff' several times.
> I'm looking for a faster variant.
>
> Regards
> Andre
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
You're doing several substitutions, re-assignments, and three loops. Did 
you try Fateman's code in his reply? I would bet that it will run 
faster. It does one nested loop and functions written in lisp. Also, you 
could try the 'compile' option.

Having written your program, try
compile(all);

and then execute it.

HTH,
-sen