# Patterns and replacements

I basically need to read off informations from an expression in order to apply some recursion relations. The terms I encounter have a form like

$f(k) = (1+ k_{\alpha}^{n1} k_\beta^{m1} \delta_\alpha^\beta + k_{\alpha}^{n2} k_\beta^{m2} k_\gamma^{l} (1-\delta_\alpha^\beta))/\tilde{k}^p$

which I want to translate into functions of the form

$B(p,n1,n2,n3,n4) = \frac{k_\alpha^{n1}k_\beta^{n2}k_\gamma^{n3}k_\eta^{n4}}{k^p}$

where $k_\alpha,k_\beta,k_\gamma,k_\eta$ are assumed to be different. In this way the above expression for instance becomes

$f(k)=B(p)+B(p,n1+m1)\delta_\alpha^\beta+[B(p,n2,m2,l)(1-\delta_{\alpha}^{\gamma})(1-\delta^{\beta}_{\gamma})+B(p,n2+l,m2)\delta^\alpha_\gamma+B(p,n2,m2+l)\delta^\beta_\gamma](1-\delta^{\beta}_{\alpha})$

in order to account for all possible values of the indices. So far I have accomplished this by manually removing the denominator and subsequently using successive ReplaceAll (/.) commands - for instance first

$\tilde{k}^pf(k)/.k_{n1\_}^{m1\_}k_{n2\_}^{m2\_}k_{n3\_}^{m3\_}\to B[p,m1+m2+m3]\delta^{n1}_{n2}\delta^{n1}_{n3}+B[p,m1+m2,m3]\delta^{n1}_{n2}(1-\delta^{n1}_{n3})+B[p,m1+m3,m2]\delta^{n1}_{n3}(1-\delta^{n1}_{n2})+B[p,m2+m3,m1]\delta^{n2}_{n3}(1-\delta^{n1}_{n2})$

and then with fewer k's and in the end seeing if any deltas are in conflict with each other. This approach is however rather cumbersome and also lacking. The problems are

1.: It does not cover the case of just a constant appearing (like the 1 above). I was thinking this might be solvable using some kind of If-statement; such as if no k's were found then it should insert a B(p). However I do not know how to combine that with the ReplaceAll.

2.: It does not seem like $k_\alpha$ is recognized as being on the form $k_{n\_}^{m\_}$

3.: The evaluation of the deltas is manual - is there a smart way to make Mathematica understand that $\delta_{\nu}^{\mu}(1-\delta^{\mu}_{\nu}) = 0$ and $\delta^{\mu}_{\nu}\delta^{\rho}_{\sigma}\delta^{\mu}_{\sigma}= \delta^{\nu}_{\mu}\delta^{\mu}_{\rho}\delta^{\nu}_{\sigma}\delta^{\nu}_{\sigma}$?

4.: Is there a way to not use successive ReplaceAlls? Can I get it to simply read off terms on the form

\begin{align}\prod_{i=1}^{n} k_{n_i}^{p_i}\end{align}

as $\{\{n_{i},p_{i}\},...,\{\{n_n,p_n\}\}$?

Sorry for asking a bit in all directions - I am hoping that a combined solution for 1,2 and 4 can be found. Unfortunately my Mathematica-fu is not yet strong enough.

-
Please post Mathematica code too. But leave LaTeX here since code will not be readable ;) –  Kuba Feb 12 at 14:07
Problem is that the real Mathematica code is a bit more involved (and I do not have it with me at the moment). When I get a bit of time I will write a small script to illustrate the approach instead. –  Christian LHC Feb 12 at 14:09
Good idea, minimal working examples are desired :) –  Kuba Feb 12 at 14:10
Re issue (2), use k^m_. (the dot is intentional) as the k-to-power pattern. It allows for the possibility that the power is 1, that is, not appearing. –  Daniel Lichtblau Feb 12 at 16:30