# How to define the correct replacement

I would like to replace a specific expression

denrule={Den1[vk]^e1_*Den2[l]^e2_*Den3[k+l-v*\[Omega]]^e3_*Den4[k]^e4_*Den5[kl]^e5_->j[MI,e1,e2,e3,-e4,-e5]};



The idea is replace such a expression with j[MI,e1,e2,e3,-e4,-e5], where the e12345 describes the exponent.

Here is an example what it should do:

Den1[vk]/.denrule = j[MI,1,0,0,0,0],

Den1[vk]*Den4[k]^2 = j[MI,1,0,0,-2,0]


I do not know how to write denrule so that it reads the exponent automatically.

## 1 Answer

I do not know how to do this with patterns alone, but the following works:

myExponent[expr_, patt_] :=
Exponent[expr /. patt :> \[FormalX], \[FormalX]]
denrepl :=
With[{patts = {Den1[_], Den2[_], Den3[_], Den4[_], Den5[_]}},
Function[{expr},
j[MI, #1, #2, #3, -#4, -#5] & @@ (myExponent[expr, #] & /@ patts)]
]

Den1[vk] // denrepl
(* j[MI, 1, 0, 0, 0, 0] *)

Den1[vk]^2*Den4[k]^2 // denrepl
(* j[MI, 2, 0, 0, -2, 0] *)

• Very nice thank you – NeAr Jul 9 '20 at 15:36