Maple - finding constant coefficient in multivariate polynomial

1.4k Views Asked by At

Is there a command relevant to finding the constant coefficient of any term in a multivariate polynomial in Maple?

For example.

If I have p=2*x*y^2+3*x^2*y. Then I want to specify a monomial term(x*y^2) and be able to extract the coefficient(2). So if I want to find the coefficient of x^2*y^2 it should return 0 and if I want to find the coefficient of x*y it should also return 0 and not 2*y+3*x.

This seems like something "natural" that should be programmed into a "good" software like Maple.

2

There are 2 best solutions below

8
On BEST ANSWER

[completely revised: hopefully with better understanding of the question, on account of ensuing comments]

G := proc(P,e,vlist::list(name)) local g, res;
        if e=1 then res:=p
        else
          g:=freeze(e);
          res:=coeff(subs(e=g,P),g);
        end if;
        eval(res,[seq(v=0,v in vlist)]);
     end proc:

p := 7*x^2*y*z^2 + 2*x*y^2 + 3*x^2*y + 17*x*y + 8*x^3*y - 33:

G(p, x, [x,y,z]);
                           0

G(p, y*x^2, [x,y,z]);
                           3

G(p, y^2*x, [x,y,z]);
                           2

G(p, y*x, [x,y,z]);
                           17

G(p, y^2*x^2, [x,y,z]);
                           0

G(p, z^2*x^2*y, [x,y,z]);
                           7

G(p, 1, [x,y,z]);
                          -33
1
On

Just loop over the variables of the monomial, taking degrees and using coeff. You can optimize by adding while a <> 0 to the loop.

cof := proc(f,m)
  local v, a, i;
  v := indets(m,name);
  a := f;
  for i in v do 
    a := coeff(a,i,degree(m,i));
  end do;
  a;
end proc;

f := -62*x^2*z^3+97*x*y^3*z-73*y*z^4-56*x*y*z^2+87*x*y;
cof(f, y*z^4);  # -73
cof(f, y*z^5);  # 0