Consider objects like

obj= (Sum[Subscript[A, i], {i, 1, 5}] Sum[Subscript[A, i], {i, 3, 6}] Sum[Subscript[A, i], {i, 2, 3}])/(
      Sum[Subscript[A, i], {i, 3, 8}] Sum[Subscript[A, i], {i, 1, 2}] Sum[Subscript[A, i], {i, 4, 6}])

enter image description here

I would like to have a substitution rule myRule that would perform the following replacement

obj /. myRule

enter image description here

for sequences of sums of $A_i$ of any lengths, and with indices not necessarily consecutive and not necessarily integer. How can this be done?

up vote 6 down vote accepted

Try the following:

obj /. HoldPattern@Plus@a : (h : Subscript)[A, _] .. :> h[A, ## & @@ Last /@ {a}]

It can be even shorter:

obj /. HoldPattern@+a : (h : Subscript)[A, _] .. :> h[A, ## & @@ Last /@ {a}]

Here's an alternative that will work with subscripted As that have more than one subscript, which may (or may not) be what you want:

obj /. HoldPattern[Plus[seq : Subscript[A, __] ..]] :>
  Subscript[A, Sequence @@ Cases[{seq}, Subscript[A, inds__] :> inds]]
Block[{Plus = Subscript[#[[1]], ## & @@ (#2 & @@@ {##})] &},  obj] // TeXForm

$\LARGE\frac{A_{2,3} A_{3,4,5,6} A_{1,2,3,4,5}}{A_{1,2} A_{4,5,6} A_{3,4,5,6,7,8}}$

Alternatively,

obj /. Plus -> (Subscript[#[[1]], ##&@@(#2&@@@{##})]&) // TeXForm

$\LARGE\frac{A_{2,3} A_{3,4,5,6} A_{1,2,3,4,5}}{A_{1,2} A_{4,5,6} A_{3,4,5,6,7,8}}$

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.