I have a list of terms of the form

f[1]g[[a,b]]g[[c,d]]g[[e,f]] + f[2]g[[a,f]]g[[e,d]]g[[c,b]] +...

some terms have the same $ggg$ form but have different $f$.. for instance

f[146]g[[a,f]]g[[e,d]]g[[c,b]] + f[21]g[[a,f]]g[[e,d]]g[[c,b]] +...+ f[7]g[[a,f]]g[[e,d]]g[[c,b]]

I'd like to take all the terms in this list that differ only by the front $f$ and leave just one of them, ie: f[146]g[[a,f]]g[[e,d]]g[[c,b]] + f[21]g[[a,f]]g[[e,d]]g[[c,b]] +...+ f[7]g[[a,f]]g[[e,d]]g[[c,b]] should become: f[146]g[[a,f]]g[[e,d]]g[[c,b]]

and it doesn't matter which one is saved!.

Any help will be greatly appreciated! Thank you!!

  • 2
    $\begingroup$ g[[a,b]] should never remain unevaluated -- it will either generate an error or evaluate to something else. Do you mean g[a,b] (and so on) instead? $\endgroup$ – jjc385 May 11 '17 at 21:33

If I understand correctly this is it. You have the sum (I will use functions for g instead of parts):

tst = f[1] g[1, 2] g[4, 5] g[6, 7] + f[2] g[1, 2] g[4, 5] g[6, 7] + 
  f[3] g[1, 2] g[1, 3] g[3, 3] + f[4] g[1, 2] g[4, 5] g[6, 7]

And Then

Plus @@ Times @@@ 
  DeleteDuplicatesBy[List @@ tst /. f[x_]*g_ -> {f[x], g}, Last]
| improve this answer | |
  • $\begingroup$ I recommend using :> here rather than ->, as the latter does nothing but encourage bad habits (and possible mistakes down the road) for new users. For an explanation why, see the "Why use RuleDelayed rather than Rule" section of [this answer]((mathematica.stackexchange.com/a/144904/11035). +1 $\endgroup$ – jjc385 May 12 '17 at 2:35
  • $\begingroup$ Yeap, of course , thanx $\endgroup$ – David Baghdasaryan May 12 '17 at 6:05

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.