0
$\begingroup$

Say I have a polynomial like

$x y^2+15x^2 y+x+3y+10$

and I want to obtain, say, only the coefficient in $x$ alone, namely a 1. Using

Coefficient[x y^2+15x^2 y+x+3y+10,x]

I obtain

1+y^2.

How can I just obtain the 1?

Thank you in advance.

$\endgroup$
  • 1
    $\begingroup$ What about Coefficient[x y^2 + 15 x^2 y + x + 3 y + 10, x] /. y -> 0? $\endgroup$ – BlacKow May 13 '16 at 16:08
  • 1
    $\begingroup$ ... or {1, 0} /. CoefficientRules[x y^2 + 15 x^2 y + x + 3 y + 10]? $\endgroup$ – kglr May 13 '16 at 16:14
  • $\begingroup$ @BlacKow The actual problem I am facing is more complicated than this example, so I would prefer to make it as neat as possible. Anyway, I appreciate the tip. $\endgroup$ – Alex May 13 '16 at 16:14
  • $\begingroup$ @Alex Can you give an appropriate example that would show deficiency of my proposal? $\endgroup$ – BlacKow May 13 '16 at 16:28
  • $\begingroup$ It is essentially the number of different variables in the problem. At the moment I have around 15 and it may grow even larger. However, the answer you have given seems to work. Thank you very much. $\endgroup$ – Alex May 13 '16 at 16:37
2
$\begingroup$

A little bit more general way:

coeff[p_, x_] := Coefficient[p, x] /. (# -> 0 & /@ Variables[p])
p = x y^2 + 15 x^2 y + x + 3 y + 10;
p2 = 3 t^2 + z;
p3 = 3 t^2 x + z;

coeff[p, x]
coeff[p2, t^2]
coeff[p3, t^2]
coeff[p3, x t^2]

(* 1 3 0 3 *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.