I want to divide a product into two pieces, one is the terms of form f[x, _], the other is the leftover factors free of x:
rule = Times[fac_., (fxs : Repeated[f[x, _]])] /; FreeQ[Times[fac], x] :> FAC[Times[fac]]*FX[Times[fxs]] For example:
Replace[f[y, 1]*f[x, 1]*f[x, 2], rule] (* FAC[f[y, 1]] FX[f[x, 1] f[x, 2]] *) I use fac_. in rule because there may be no other coefficients present. For example, I hope
Replace[f[x, 1]*f[x, 2], rule] returns
FX[f[x, 1] f[x, 2]] but the actual output is
(* f[x, 1] f[x, 2] *) In fact, the following returns False:
MatchQ[f[x, 1]*f[x, 2], Times[fac_., (fxs : Repeated[f[x, _]])] /; FreeQ[Times[fac], x]] It seems that _. does not work. Why?
xand ones withoutx, together with your expected output? $\endgroup$Conditionseems part of the problem. Try it withOptional[fac_?(FreeQ[#1, x] &)]instead offac_. ... /; ...$\endgroup$ConditionforOptionalin a wrong form. But unluckly, your suggestion does not work either. Now I use a a rule list for my exception{HoldPattern@Times[fxs : Repeated[f[x, _]]] :> FX[Times[fxs]], Times[fac_ /; FreeQ[x][fac], (fxs : Repeated[f[x, _]])] :> FAC[Times[fac]]*FX[Times[fxs]] }. But I still want to know how to useOptionalwithConditionin this example. $\endgroup$x_ : 1 /; OddQ[x](ConditionandOptionalare next to each other), and this results in a warning. It's not your case, I think. $\endgroup$