0
$\begingroup$

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.

$\endgroup$

1 Answer 1

2
$\begingroup$

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] *)
$\endgroup$
1
  • $\begingroup$ Very nice thank you $\endgroup$
    – NeAr
    Commented Jul 9, 2020 at 15:36

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.