1
$\begingroup$

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: enter image description here

$\endgroup$
4
  • $\begingroup$ Yes, just use DeleteCases / Cases, e.g DeleteCases[polynomialdata, Subscript[xy, 2, 1][0], Infinity] $\endgroup$
    – flinty
    May 1 at 11:38
  • $\begingroup$ 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]*```? $\endgroup$
    – Xuemei
    May 1 at 14:18
  • $\begingroup$ how about DeleteCases[polynomialdata, HoldPattern[Subscript[xy, _, _][0] Subscript[xy, _, _][0]]]? $\endgroup$
    – kglr
    May 2 at 8:17
  • $\begingroup$ @kglr, thank you. that works! $\endgroup$
    – Xuemei
    May 2 at 9:27
1
$\begingroup$
DeleteCases[polynomialdata, 
 HoldPattern[Subscript[xy, _, _][0] Subscript[xy, _, _][0]]]

enter image description here

$\endgroup$
1
$\begingroup$

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:

enter image description here

Have fun!

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$
    – Xuemei
    May 1 at 14:12
  • $\begingroup$ 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? $\endgroup$ May 1 at 19:47
  • $\begingroup$ 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. $\endgroup$
    – Xuemei
    May 2 at 9:25

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.