# Retain summands in an expression that match a given rule

Suppose we have the following expression

A[j_] := Subscript[A, j]
EXPR=A^2*A + A^4*A + A^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^2*A + A^2


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

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

% // TeXForm


$A_2 A_1^2+A_2^2$

or

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


$A_2 A_1^2+A_2^2$

• 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^4*A 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? – Bounded Jun 29 '18 at 11:40
• @Bounded, i think EXPR /. {Plus -> List, Times -> Plus, Subscript -> (#2 &), Power -> Times } fixes the problem. – kglr Jun 29 '18 at 11:48