I am looking for help in writing transformation rules in which I wish to distinguish between eight variables $x,x^2,\bar{x},\bar{x}^2, x_p,x_p^2, \bar{x}_p,\bar{x}^2_p $.
For each of these eight variables, I would like eight separate transformation rules such that :
$x,x^2,\bar{x},\bar{x}^2, x_p,x_p^2, \bar{x}_p, \bar{x}^2_p \rightarrow (\bar{x}+\tilde{x}),(\bar{x}+\tilde{x})^2,(\bar{x}+\tilde{x}),(\bar{x}+\tilde{x})^2, (\bar{x}_p+\tilde{x}_p),(\bar{x}_p + \tilde{x}_p)^2, (\bar{x}_p+\tilde{x}_p), (\bar{x} + \tilde{x}_p)^2 $
I am having considerable trouble defining the correct constraints so that each of the transformation rules only activates for its corresponding variable. I have had some partial success, for example rule six for $x^2_p$:
rule6 := { Power[Subscript[x, p], power_] -> Power[Subscript[OverBar[x], p] + Subscript[OverTilde[x], p], power]}
when applied to the expression:
$x^2 x_p^2 y_p^2 \bar{x}_p^2\text{/.}\, \text{rule6}$
which in code is:
Times[Power[x,2],Power[Subscript[x,p],2],Power[Subscript[y,p],2],Power[Subscript[OverBar[x],p],2]] /. rule6
yields the desired output $x^2 y_p^2 \bar{x}_p^2 \left(\bar{x}_p+\tilde{x}_p\right){}^2$.
Similarly, rule two for $x^2$
rule2 := { Power[x, power_] -> Power[OverBar[x] + OverTilde[x], power]}
when applied to expression
$x^2 x_p^2 y_p^2 \bar{x}_p^2\text{/.}\, \text{rule2}$
which in code is
Times[Power[x,2],Power[Subscript[x,p],2],Power[Subscript[y,p],2],Power[Subscript[OverBar[x],p],2]] /. rule2
yields the desired output $x_p^2 y_p^2 \left(\bar{x}+\tilde{x}\right)^2 \bar{x}_p^2$.
However, I am having particular trouble defining a proper rule for $x$.
My attempt
rule1 := {var_ /; var === x -> (OverBar[var] + OverTilde[x])}
is too permissive, in that it matches the $x$ even when it appears with a Subscript, with a Power, or with an OverBar.
For example, when applied to the expression
$x^2 x_p^2 y_p^2 \bar{x}_p^2\text{/.}\, \text{rule1}$
which in code is
Times[Power[x,2],Power[Subscript[x,p],2],Power[Subscript[y,p],2],Power[Subscript[OverBar[x],p],2]] /. rule1
it yields the incorrect output $y_p^2 \left(\bar{x}+\tilde{x}\right)^2 \overline{\bar{x}+\tilde{x}}_p^2 \left(\bar{x}+\tilde{x}_p\right){}^2$; the correct output in this case would have been to leave the input untouched: $x^2 x_p^2 y_p^2 \bar{x}_p^2$.
Similarly, when applied to the expression
$x x_p y_p \bar{x}_p \text{/.}\, \text{rule1}$
which in code is
Times[x,Subscript[x,p],Subscript[y,p],Subscript[OverBar[x],p]] /. rule1
it yields the incorrect output $y_p \left(\bar{x}+\tilde{x}\right) \overline{\bar{x}+\tilde{x}}_p \left(\bar{x}+\tilde{x}_p\right)$; the correct output would have been $(\bar{x} + \tilde{x}) x_p y_p \bar{x}_p$.
How do I force the pattern matching to ignore the OverBar, Power and Subscript cases?
I have tried things such as
rule1ALT := var_ /; var === x && FreeQ[{Subscript, OverBar, Power}, Head[var]] -> OverBar[x] + OverTilde[x]
but the problem seems to be that Head[var] returns Symbol and not the OverBar, Subscript or Power encompassing it.
Subscriptetc in variable names. See e.g. this FAQ on "Basic syntax issues". If, on the other hand, you are only interested in typographical layout, then Mathematica might honestly not be the most straightforward method. – MarcoB Sep 06 '15 at 18:48