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?
x
and ones withoutx
, together with your expected output? $\endgroup$Condition
seems part of the problem. Try it withOptional[fac_?(FreeQ[#1, x] &)]
instead offac_. ... /; ...
$\endgroup$Condition
forOptional
in 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 useOptional
withCondition
in this example. $\endgroup$x_ : 1 /; OddQ[x]
(Condition
andOptional
are next to each other), and this results in a warning. It's not your case, I think. $\endgroup$