1
$\begingroup$

I have a set of polynomial expressions in multiple variables:

U = x[1] x[2] + x[1] x[3] + x[2] x[3]
F = m1^2 x[1]^2 x[2] + m2^2 x[1] x[2]^2 + m1^2 x[1]^2 x[3] + m1^2 x[1] x[2] x[3] + m2^2 x[1] x[2] x[3] + m3^2 x[1] x[2] x[3] - p^2 x[1] x[2] x[3] + m2^2 x[2]^2 x[3] + m3^2 x[1] x[3]^2 + m3^2 x[2] x[3]^2
G = U + F

The relevant variables in this case (and for all of my examples) are the various "x[i]"-s, where "i" is an integer.

To split the terms in G, I use this answer.

ClearAll[toList]
toList = # /. {_[a__] :> {a}, a_?AtomQ :> {a}} &;
listresG = toList[G];

What I need: A way to extract the terms appearing in U and F from the expression of G. I find doing this non-trivial since Mathematica sum does some kind of monomial ordering of its own that might change the place of each term. For eg, in this example, the explicit expression for G happens to be

G = x[1] x[2] + m1^2 x[1]^2 x[2] + m2^2 x[1] x[2]^2 + x[1] x[3] + m1^2 x[1]^2 x[3] + x[2] x[3] + m1^2 x[1] x[2] x[3] + m2^2 x[1] x[2] x[3] + m3^2 x[1] x[2] x[3] - p^2 x[1] x[2] x[3] + m2^2 x[2]^2 x[3] + m3^2 x[1] x[3]^2 + m3^2 x[2] x[3]^2

which has clearly altered the positioning of the terms relative to where they appear in U and F.

I have tried doing Cases[listresG, x[_Integer]*x[_Integer]] to extract the terms appearing in U from G, but this doesn't work.

$\endgroup$
2
  • 1
    $\begingroup$ Can you give an example of desired outcome, perhaps on a simpler sample expression? I find it quite difficult to follow your description of the process. Additionally, have you considered a more processing-friendly format for your variables? For instance, it is commonly recommended to replace Subscript[x, n] with just x[n] and double index variables with e.g. x[n, m] or x[n][m]. The latter formulations are much easier for pattern matching, substitution, etc. $\endgroup$
    – MarcoB
    Commented Aug 22, 2022 at 12:27
  • $\begingroup$ @MarcoB Please check the modified question. $\endgroup$
    – abstract
    Commented Aug 22, 2022 at 16:16

3 Answers 3

2
$\begingroup$
UTerms = List @@ U
(*{x[1] x[2], x[1] x[3], x[2] x[3]}*)

FTerms = List @@ F
(*{m1^2 x[1]^2 x[2], m2^2 x[1] x[2]^2, m1^2 x[1]^2 x[3], 
 m1^2 x[1] x[2] x[3], m2^2 x[1] x[2] x[3], 
 m3^2 x[1] x[2] x[3], -p^2 x[1] x[2] x[3], m2^2 x[2]^2 x[3], 
 m3^2 x[1] x[3]^2, m3^2 x[2] x[3]^2}*)

GTerms = List @@ G
(*{x[1] x[2], m1^2 x[1]^2 x[2], m2^2 x[1] x[2]^2, x[1] x[3], 
 m1^2 x[1]^2 x[3], x[2] x[3], m1^2 x[1] x[2] x[3], 
 m2^2 x[1] x[2] x[3], m3^2 x[1] x[2] x[3], -p^2 x[1] x[2] x[3], 
 m2^2 x[2]^2 x[3], m3^2 x[1] x[3]^2, m3^2 x[2] x[3]^2}*)

I"m not 100% clear what you are meaning by "terms". If you mean terms in the sum then:

UinG = Intersection[Gterms, UTerms]
(*{x[1] x[2], x[1] x[3], x[2] x[3]}*)

FinG = Intersection[Gterms, FTerms]
(*{m1^2 x[1]^2 x[2], m2^2 x[1] x[2]^2, m1^2 x[1]^2 x[3], 
 m1^2 x[1] x[2] x[3], m2^2 x[1] x[2] x[3], 
 m3^2 x[1] x[2] x[3], -p^2 x[1] x[2] x[3], m2^2 x[2]^2 x[3], 
 m3^2 x[1] x[3]^2, m3^2 x[2] x[3]^2}*)

If that's not what you meant, let me know in a comment.

$\endgroup$
3
$\begingroup$

THIS IS AN EXTENDED COMMENT RATHER THAN AN ANSWER

To amplify upon MarcoB's comment about indexed variables:

Clear["Global`*"]

To format specified indexed variables (e.g., x and m) as subscripts use

(Format[#[n_]] := Subscript[#, n]) & /@ {x, m};

U = x[1] x[2] + x[1] x[3] + x[2] x[3]

enter image description here

F = (-p^2 + m[1]^2 + m[2]^2 + m[3]^2) x[1] x[2] x[3] + 
  m[3]^2 (x[1] + x[2]) x[3]^2 + m[2]^2 x[2]^2 (x[1] + x[3]) + 
  m[1]^2 x[1]^2 (x[2] + x[3])

enter image description here

G = x[1] x[2] + x[1] x[3] + 
  x[2] x[3] + (-p^2 + m[1]^2 + m[2]^2 + m[3]^2) x[1] x[2] x[3] + 
  m[3]^2 (x[1] + x[2]) x[3]^2 + m[2]^2 x[2]^2 (x[1] + x[3]) + 
  m[1]^2 x[1]^2 (x[2] + x[3])

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks a lot. I have modified the question. $\endgroup$
    – abstract
    Commented Aug 22, 2022 at 16:17
1
$\begingroup$

To gather terms from U and F in G, you may first transform the polynomials into lists and then use "GatherBy" checking for membership in e.g. U. This gives a one liner:

GatherBy[List @@ G, MemberQ[List @@ U, #] &]

enter image description here

$\endgroup$

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.