# Get coefficient of a symbolized symbol in a sum that involves other symbolized expressions

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

Needs["Notation"]
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;

• would it be possible to give us how you coded p in your notebook? many thanks in advance!
– bmf
Apr 17, 2022 at 17:12
• @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.
– gen
Apr 17, 2022 at 17:21
• n is left undefined in your post. Am I correct to assume that n=5?
– bmf
Apr 17, 2022 at 17:21
• @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.
– gen
Apr 17, 2022 at 17:26
• 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
– bmf
Apr 17, 2022 at 17:29

You can use CoefficientArrays

I am doing the example given in the OP:

Needs["Notation"]
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


Normal@CoefficientArrays[p]


Another approach would be to use MonomialList

MonomialList[p]


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]]


• Thanks for posting this, I'll try to make it work. MonomialList looks very useful.
– gen
Apr 17, 2022 at 17:53
• @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
– bmf
Apr 17, 2022 at 17:55
• @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
– bmf
Apr 17, 2022 at 17:58
• 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?
– gen
Apr 17, 2022 at 18:21
• @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?
– bmf
Apr 17, 2022 at 18:23

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

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}.

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