# Replacing Kronecker delta times expression with the expression with inserted index

Consider the following symbolic expression:

expr = f[a,b]*g[c,d]KroneckerDelta[a,m]KroneckerDelta[d,k] + KroneckerDelta[a,p]f[x,a]+const


Could you please tell me how to replace this expression with the one where the Kroneckers are replaced with 1 and the indices in f, g, f by the values from the Kroneckers?

I.e.,

exprfinal=f[m,b]*g[c,k]+f[x,p]+const

• Can you more explicitly explain how to generate the indices from the parameters of the KroneckerDelta expressions? I don't understand why e.g. f[a, b] * g[c, d] KroneckerDelta[a, m] KroneckerDelta[d, k] should have become f[m, b] * g[c, k]. Aug 1, 2023 at 14:40
• @MarcoB : in reality, there must be a sum Sum[f[a,b]*g[c,d],{a,...},{b,...},{c,...},{d,...}], but in my expressions it is assumed to be implicit. Aug 1, 2023 at 14:44

\$Version

(* "13.3.0 for Mac OS X ARM (64-bit) (June 3, 2023)" *)

Clear["Global*"]

expr = f[a, b]*g[c, d] KroneckerDelta[a, m] KroneckerDelta[d, k] +
KroneckerDelta[a, p] f[x, a] + const;

expr //. e_ *KroneckerDelta[x_, y_] :> (e /. x -> y)

(* const + f[x, p] + f[m, b] g[c, k] *)

rule = (func : (f | g))[s : OrderlessPatternSequence[a_, b_]] *
KroneckerDelta[a_, c_] :> func[Sequence @@ ({s} /. a :> c)];

expr //. rule
(* const + f[x, p] + f[m, b] g[c, k] *)
`