1

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 Answers2

1

[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
acer
  • 5,293
  • I actually wanted something that would work with many more than just 2 variables and where I wouldn't even know exactly which monomials occur in the expression. The problem with coeffs is that I can't match the coefficients to the monomials and also see which ones are missing.

    But thanks for your help.

    – domoremath Jun 11 '13 at 00:38
  • You originally wrote that you "want to specify a monomial term...", which contradicts your new claim that you "wouldn't even know exactly which monomials occur...". Do you perhaps want a table of the coefficient values, indexed by possible monomial terms. Or perhaps just a sequence of pairs? Your use of the word "missing" is also unclear -- do you mean w.r.t. all mixed powers from 0 up to the degree in each variable? – acer Jun 11 '13 at 01:24
  • A table of coefficient values indexed by monomial terms would be fine. Maybe I was unclear above but when I said "specify a monomial term", I mean that I can input any monomial term regardless of whether it occurs or not and output the coefficient. – domoremath Jun 11 '13 at 01:33
  • I don't understand why the procedure G that I gave does not suffice. You can call G(p, e) for an arbitrary term e and get its coefficient in p. The coding of G does not require knowledge of the number of variables in the p to be used subsequently. – acer Jun 11 '13 at 01:48
  • Wow I never knew about the freeze command. But if I input G(2xy^2+3x^2y,x); then it returns 2*y^2; when I expect it to return 0 since there is no x term. – domoremath Jun 11 '13 at 02:40
  • I suppose I input the list of all variables as a list along with the expression p and the monomial e. I hope that clarifies things. – domoremath Jun 11 '13 at 02:43
  • One last clarification. You said that "You mentioned in comments that you don't know how many variables there may be in the multivariable polynomial." but I do know the list of variables just not which terms appear. For instance, in the example just above, I know there are only 2 variables x,y but I don't know that there is no x term but there is a x*y^2 term. – domoremath Jun 11 '13 at 02:51
  • That works. Thanks! – domoremath Jun 11 '13 at 21:14
0

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