# Replacing product of powers of subscripted symbol

I have an expression that involves subscripts, powers and products, for example:

$3(m-1)^2 \alpha_{a,b}^c \alpha_{d,e} + n \alpha_{f,g}^h$

(Sorry, I had to use TeX, but read it as mathematica input expression.)

I want to replace the product of only alpha's, $\alpha_{a,b}^c \alpha_{d,e}$ and $\alpha_{f,g}^h$, with a function y[{{c,{a,b}},{1,{d,e}}] and y[{{h,{f,g}}}], (note the 1 for the omitted exponent). That is turn it into the following form: 3(m-1)^2 y[{{c,{a,b}},{1,{d,e}}] + n y[{{h,{f,g}}}]

In general, in any expression, replace the product of powers of subscripted alpha's (could be one, two or more alpha's in each product), with the function y taking for argument a list of the exponents and subscripts of each term in the product.

Edit: In a general case, not all alphas have the same number of subscripts. Could be: $3(m-1)^2 \alpha_{a,b}^c \alpha_{d,e} + n \alpha_{f,g,i}^h$

Can this be done? I'm not sure how to approach this.

Here's my solution. Define the input expression:

expr = 3 (m-1)^2 Subscript[α,a,b]^c Subscript[α,d,e] + n Subscript[α,f,g]^h


Make the following replacements repeatedly:

expr //. {Subscript[α,s1_,s2_]^n_. :> y[{{n, {s1, s2}}}],
HoldPattern[Times[c : PatternSequence[_y, __y]]] :> y[{c}[[All, 1, 1]]]}


n y[{{h,{f,g}}}] + 3 (-1 + m)^2 y[{{1,{d,e}}, {c,{a,b}}}]

The terms and elements of y are out of order; I'm not sure if that's important.

• Will this work in the general case? for any number of subscripts and not all alpha's with the same number of subscripts? – Ivan May 26 '18 at 18:34
• It won't work with any number of subscripts. If you need any number of subscripts then change first rule to Subscript[α,sub__]^n_. :> y[{{n, {sub}}}] – QuantumDot May 26 '18 at 18:37
• Great! Thank you! – Ivan May 26 '18 at 18:47