Suppose we have the following expression

A[j_] := Subscript[A, j]
EXPR=A[1]^2*A[2] + A[1]^4*A[3] + A[2]^2

As stated in the title, I am struggling to retain some summands in EXPR, in particular the ones for which the sum of the product of the subscript and the power in each term is equal to some integer c. For instance, how to get all those terms that satisfy this rule with $c=4$? The correct answer is

A[1]^2*A[2] + A[2]^2

as in the first term we have $1\cdot 2+2\cdot 1=4$, whereas in the second one $2\cdot 2=4$


1 Answer 1

Pick[EXPR, EXPR /. {Plus -> List, Times -> Plus, Subscript -> (#2 &), Power -> Times}, 4] 

% // TeXForm

$ A_2 A_1^2+A_2^2$


Select[EXPR, 4 == # /. { Times -> Plus, Subscript -> (#2 &), Power -> Times} &] // TeXForm

$A_2 A_1^2+A_2^2$

  • $\begingroup$ Thanks! Your proposal is partially right in that I looked at the outcome of EXPR /. Plus -> List /. Power[Subscript[A, w_], b_.] :> Times[w, b] and the result is {4,4,12}: 12 is wrong in that the term A[1]^4*A[3] should be "processed" as 1*4+3. This in turn implies that the correct answer your code gave is due because of a lucky structure of the terms involved. Is there a way to fix this? $\endgroup$
    – Bounded
    Commented Jun 29, 2018 at 11:40
  • $\begingroup$ @Bounded, i think EXPR /. {Plus -> List, Times -> Plus, Subscript -> (#2 &), Power -> Times } fixes the problem. $\endgroup$
    – kglr
    Commented Jun 29, 2018 at 11:48

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.