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.


1 Answer 1


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

enter image description here

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.

  • $\begingroup$ Will this work in the general case? for any number of subscripts and not all alpha's with the same number of subscripts? $\endgroup$
    – Ivan
    May 26, 2018 at 18:34
  • 2
    $\begingroup$ 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}}}] $\endgroup$
    – QuantumDot
    May 26, 2018 at 18:37
  • $\begingroup$ Great! Thank you! $\endgroup$
    – Ivan
    May 26, 2018 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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