My mathematica notebook begins with symbolizing all expressions of the form $x$ subscript an integer, "$x_k$",

Symbolize[ParsedBoxWrapper[SubscriptBox["x", "_"]]]

I then define a polynomial in terms of these symbols, eg.

$$p = x_1 x_2 x_3 + x_4 x_5 - 2x_1 + x_1^2$$

Now I would like to obtain the coefficient of say $x_1$. (I.e. for this example this would be $2$.) The command Coefficient[p, x_1] does not work because it treats all the other symbols as scalars, i.e. I get $x_2x_3$ as part of the answer.

Given this setup how can I obtain the coefficient of monomials like $x_1$ or $x_2x_3$ in the usual way?

Edit: the way p was coded is:

toVariableSymbol[i_] := Subscript["x", i];
vars = Map[toVariableSymbol, Range[n]];
pairwiseProducts = Total[Flatten[
   Table[Subscript["x", i]*Subscript["x", j], {i, 1, n}, {j, i, n}]]];
p = Total[vars] - pairwiseProducts - x_1 x_2 x_3 x_4 x_5;
  • 1
    $\begingroup$ would it be possible to give us how you coded p in your notebook? many thanks in advance! $\endgroup$
    – bmf
    Apr 17, 2022 at 17:12
  • $\begingroup$ @bmf I just edited it. This is just part of it but I think this should describe it well enough. There are some other degree 3 terms but that's all manually typed up for now. $\endgroup$
    – gen
    Apr 17, 2022 at 17:21
  • $\begingroup$ n is left undefined in your post. Am I correct to assume that n=5? $\endgroup$
    – bmf
    Apr 17, 2022 at 17:21
  • $\begingroup$ @bmf The reason why I didn't specify it, is because later on I would like to be able able to change the value of n, so I'd ideally like to find a solution that's general enough that it doesn't depend on the number of x_1's my polynomial involves... To answer your question, I actually have n=6 for now. $\endgroup$
    – gen
    Apr 17, 2022 at 17:26
  • $\begingroup$ thanks for that. I just pointed it out because if it is left undefined Mathematica complains. I copied the code you provided and used n=6. As you can see in this screenshot there's an issue... I am on V13.0.0 $\endgroup$
    – bmf
    Apr 17, 2022 at 17:29

3 Answers 3


You can use CoefficientArrays

I am doing the example given in the OP:

Symbolize[ParsedBoxWrapper[SubscriptBox["x", "_"]]]

p = Subscript["x", 1] Subscript["x", 2] Subscript["x", 3] + 
  Subscript["x", 4] Subscript["x", 5] - 2 Subscript["x", 1] + 
  Subscript["x", 1]^2




Another approach would be to use MonomialList



Edit: perhaps closer to what the author of the OP had in mind

Coefficient[p, Subscript["x", 1]]


and perhaps cleaner is to do

Coefficient[MonomialList[p], Subscript["x", 1]]


  • $\begingroup$ Thanks for posting this, I'll try to make it work. MonomialList looks very useful. $\endgroup$
    – gen
    Apr 17, 2022 at 17:53
  • $\begingroup$ @gen glad I was able to help :). These should work on longer polynomials as well. For CoefficientArrays I suggest that you read the documentation first and then use the Part command to extract specific terms $\endgroup$
    – bmf
    Apr 17, 2022 at 17:55
  • $\begingroup$ @gen since you mentioned the command Coefficient in the OP I managed to extract it using that command. you might want to have a look at the updated version $\endgroup$
    – bmf
    Apr 17, 2022 at 17:58
  • $\begingroup$ Yes, I like this one most. I have one finally question though: The last photo in your answer shows the result given by your code, {0, x_2 x_3, -2, 0}. Now I would like to extract -2 from this somehow. I tried filtering the array by Select[arr, _Real] but that didn't work... Can you think of a way of throwing away all entries of this list that involve an x_i? $\endgroup$
    – gen
    Apr 17, 2022 at 18:21
  • $\begingroup$ @gen this is why I suggested the Part command earlier. I guess you want something like {0, x_ 2 x_ 3, -2, 0}[[3]] if I understood correctly, yes? $\endgroup$
    – bmf
    Apr 17, 2022 at 18:23

Notice that the following preserves the order of the monomials in the polynomial:

enter image description here

Considering the previous observation, we can define the following function:

MyCoefficient[expr_?PolynomialQ, var_, position_Integer] := 
 Total[Take[Thread[Level[expr, {1}] -> Map[Total, CoefficientList[Level[expr, {1}], var]]][[All, -1]], {position}]]

Testing the function:

MyCoefficient[p, Subscript[x, 1], #] & /@ Range[4]
(*{-2, 1, Subscript[x, 2] Subscript[x, 3], Subscript[x, 4] Subscript[x, 5]}*)

You could do something like:

coef[p_, term_] := With[{powers = FirstPosition[CoefficientList[term, Variables[{p, term}]], 1]},
                    CoefficientList[p, Variables[{p, term}], powers][[Sequence @@ powers]]]
coef[x1 x2 + 3 x1 + 2 x2 + 4 x3 x2^5 + 2 x2 x3, x3 x2]
(* 2 *)

However, if p contains symbolic constants you would have to replace both of the Variables[p] by a manually set list like {x1, x2, x3}.

  • 1
    $\begingroup$ I tried to make it work the way you suggested, but something doesn't seem to work: imgur.com/a/7fRhrYk $\endgroup$
    – gen
    Apr 17, 2022 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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