# How to delete certain terms in a polynomial with variables in subscript?

I have a polynomial in the following style,

polynomialdata= Subscript[xy, 1, 1][0]*Subscript[xy, 1, 2][0] + Subscript[xy, 1, 1][0]*Subscript[xy, 2, 1][0] +  Subscript[xy, 1, 2][0]*Subscript[xy, 2, 2][0] +  Subscript[xy, 2, 1][0]*Subscript[xy, 2, 2][0] + Subscript[xy, 1, 1][0]*Subscript[xy, 1, 2][0]*Subscript[xy, 2, 1][0]*Subscript[xy, 2, 2][0] + Subscript[xy, 2, 1][0]*Subscript[xy, 3, 1][0] + Subscript[xy, 1, 1][0]*Subscript[xy, 1, 2][0]*Subscript[xy, 2, 1][0]*Subscript[xy, 3, 1][0]


I wonder how to remove certain terms in the polynomialdata such as Subscript[xy, 2, 1][0] Subscript[xy, 3, 1][0] or keeping terms such as  Subscript[xy, 1, 1][0]*Subscript[xy, 1, 2][0]*Subscript[xy, 2, 1][0]*Subscript[xy, 3, 1][0].

Probably one stupid way is to do the following

Keep[expr_] :=
expr /. {Subscript[xy, 1, 1][0]*Subscript[xy, 1, 2][0]-> 0, Subscript[xy, 1, 1][0]*Subscript[xy, 2, 1][0]-> 0, Subscript[xy, 1, 2][0]*Subscript[xy, 2, 2][0]-> 0, Subscript[xy, 2, 1][0]*Subscript[xy, 2, 2][0] -> 0, Subscript[xy, 2, 1][0]*Subscript[xy, 3, 1][0]-> 0}

Keep[data]


the result gives 0; which is not I expected.

I want the outcome to be Subscript[xy, 1, 1][0]*Subscript[xy, 1, 2][0]*Subscript[xy, 2, 1][0]*Subscript[xy, 2, 2][0] + Subscript[xy, 1, 1][0]*Subscript[xy, 1, 2][0]*Subscript[xy, 2, 1][0]*Subscript[xy, 3, 1][0]. Maybe I could use DeleteCases function.

However, if the data has many terms, it will be very inefficient to do the above removing. Is there any way to solve it? Thank you very much in advance!

as a very simple example:

• Yes, just use DeleteCases / Cases, e.g DeleteCases[polynomialdata, Subscript[xy, 2, 1][0], Infinity] Commented May 1, 2021 at 11:38
• yes, what if I only want to delate terms Subscript[xy,1, 1][0]*Subscript[xy, 2, 1][0] but not the single term Subscript[xy, 2, 1][0]*? Commented May 1, 2021 at 14:18
• how about DeleteCases[polynomialdata, HoldPattern[Subscript[xy, _, _][0] Subscript[xy, _, _][0]]]?
– kglr
Commented May 2, 2021 at 8:17
• @kglr, thank you. that works! Commented May 2, 2021 at 9:27

DeleteCases[polynomialdata,
HoldPattern[Subscript[xy, _, _][0] Subscript[xy, _, _][0]]]


First, let us determine the position of the term you want to preserve:

pos = Position[polynomialdata,
Subscript[xy, 1, 1][0]*Subscript[xy, 1, 2][0]*
Subscript[xy, 2, 1][0]*Subscript[xy, 3, 1][0]][[1, 1]]

(*  7  *)


Now let us extract this term and keep it under the name "ex":

ex = Extract[polynomialdata, pos]

(*  Subscript[xy, 1, 1][0] Subscript[xy, 1, 2][0] Subscript[xy, 2, 1][
0] Subscript[xy, 3, 1][0]  *)


Now, let us kill all the terms containing the elements you want to get rid of. During this procedure the term you need to leave intact will also disappear. Therefore, we will add the term "ex" to the result:

 (polynomialdata /. {Subscript[xy, 2, 1][0] -> 0,
Subscript[xy, 3, 1][0] -> 0}) + ex

(*  Subscript[xy, 1, 1][0] Subscript[xy, 1, 2][0] +
Subscript[xy, 1, 2][0] Subscript[xy, 2, 2][0] +
Subscript[xy, 1, 1][0] Subscript[xy, 1, 2][0] Subscript[xy, 2, 1][
0] Subscript[xy, 3, 1][0]  *)


Please notice the use of the round parentheses. Without them, Mma will be confused and will return an error message.

Here is this result as the image to make it better visible:

Have fun!

• thank you for the solution! What if there are hundred terms such as xy_{k,n}*xy_{i,j}, do I need to list all the removed terms in Position[] function? Commented May 1, 2021 at 14:12
• If all of these terms differ from one another and have no common features I do not see another way. If you can find some pattern common for all terms to be preserved that one does not meet in the terms needed to be killed one can think about a different algorithm. There must be some criterion according to which you decide that some terms should be kept, mustn't it? Commented May 1, 2021 at 19:47
• yes, you are right. what I do now is first keeping the terms and then use expr/.{terms->KEEP,Subscript[xy, l1_, l2_][0]^n_ -> 0}, and it works fine. Commented May 2, 2021 at 9:25