# Replacement rule for terms that are separated by operators?

My main problem is how one can create a "transcending between operators rule" in the same term according to the minimal example bellow.

Suppose I need to perform a replacement of the form

G[b_]*F[b_][Y_]->FG[Y]


where G has 1 argument, F has 2 arguments of the form above and FG is a new function that has only 1 argument.

The problem is that I need to apply this kind of rule in an expression of the form

G[a]*F1[a1]@F2[a2]@..@Fj[aj]@..@FN[aN]@F[a]@Y


so that in the end

 F1[a1]@F2[a2]@..@Fj[aj]@..@FN[aN]@FG[a]@Y


where the Fj's have 2 arguments as F.

Notice that the ingredients of left hand-side of the rule G[b_]*F[b_][Y_]->FG[Y] cannot be next to each other in such expressions, therefore we cannot apply the rule. Also note:

1. The "separator" F1[a1]@F2[a2]@..@Fj[aj]@..@FN[N] might be anything with any number of operators.
2. F[a] might be sandwiched between Fj[aj]'s and not in general the first one to be applied on Y.

So my question is:

Is there a way to modify the rule G[b_]*F[b_][Y_]->FG[Y] so that it can be applied to the minimal example?

Okay, I'm only mostly certain I understand the question being asked, so please let me know if this isn't quite right.

The replacement rule you're looking for seems to be:

rr = (G[a_]*b_ /; (! FreeQ[b, F[a]]) :> (b /. F[a][y_] :> FG[y]));


In plain English, this pattern is something like: "If something like G[a] is multiplied by something (b) which contains F[a], replace the first instance of F[a][...] in b with FG[...] and drop the G[a] entirely."

Applied to the example expression (trimmed down some):

G[a]*F1[a1]@F2[a2]@Fj[aj]@FN[aN]@F[a]@Y /. rr


F1[a1][F2[a2][Fj[aj][FN[aN][FG[Y]]]]]

• this is exactly what I was looking for and you actually summarized it very well as "In plain English...drop the G[a] entirely.". Thank you, I learnt quite a lot from your answer. Feb 21, 2019 at 21:07

This may not be general enough, but it works in the minimal example.

expr = G[a]*F1[a1]@F2[a2]@Fj[aj]@FN[aN]@F[a]@Y

expr1 = expr/G[a] /. { F[a] -> (FG[#1] &)}

(*F1[a1][F2[a2][Fj[aj][FN[aN][FG[Y]]]]]*)