# [Maxima] RFC: extracting coefficients of a multivariate polynomial

S. Newhouse sen1 at math.msu.edu
Sun Apr 27 08:26:34 CDT 2008

```andre maute wrote:
> Hi all,
>
> I have to extract the coefficients of a multivariate polynomial.
> The monomials appearing in the multivariate polynomial are given.
>
> For now I'm using the following function,
> which is also the bottleneck of my application.
>
> ---------------------------------------------------------------
> 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);
> ---------------------------------------------------------------------
>
> Does someone know,
> if Maxima does have such a function, a potentially faster one?
>
> The documentation of Maxima revealed nothing for me :-(
>
>
Do you really need the whole list of coefficients, or just a particular one?

For the latter problem, you can simply define a function which extracts
the partial  derivatives of a given order at the origin and divide by
the appropriate factorials.
Of course, you can make the whole list from the parts if you need that,
too.  I don't know if this will improve the speed of the other routines

E.g.

(%i49) my_coeff(l,m,p):= block(a:
1/(factorial(l)*factorial(m))*diff(diff(p,x,l),y,m),ev(a,x=0,y=0));
Evaluation took 0.0000 seconds (0.0000 elapsed)
(%o49) my_coeff(l,m,p):=block(a:1/(l!*m!)*diff(diff(p,x,l),y,m),
ev(a,x = 0,y = 0))
(%i50) my_coeff(3,4,(1+x+y)^30);
Evaluation took 0.0000 seconds (0.0000 elapsed)
(%o50) 71253000
(%i51) my_coeff(12,15,(1+x+y)^30);
Evaluation took 0.0000 seconds (0.0000 elapsed)
(%o51) 70578471600

FWIW,
-sen
```