# How to apply ReplaceAll to denominator?

Here is the simple task. In reality, it's far more complicated, but it comes down to this.

I want to take the expression $\frac{y}{x^2}$ and replace $x^2$ with $k$. This doesn't work:

y/x^2 /. x^2 -> k


More confusing thing is that it works if denominator contains sum, e.g. if you take $\frac{y}{x^2+1}$,

y/(x^2+1) /. x^2 -> k


will give desired result.

How should I approach this?

• y/x^2 // FullForm – Kuba Jun 1 '15 at 11:54
• In mma,y/x^2 is regraded as y*Power[x,-2] – WateSoyan Jun 1 '15 at 11:56
• Yes, this is solution. Instead of using $x^2 \to \dots$, I should be using $x^{-2} \to \dots$. It works fine then. Thanks! – Luka Jun 1 '15 at 12:07

When using replacements, always remember that the actual expression you are looking at could have a different internal representation. As Kuba already suggested, by looking at the FullForm you can reveal very easily what is really going on.

In your case, I would suggest to use Numerator and Denominator to first extract the parts of the rational. After this, the replacement works as expected

expr = y/x^2;
Numerator[expr]/(Denominator[expr] /. x^2 -> k)
(* y/k *)


This will work for your second case and hopefully for real example too.

Division and subtraction do not really exist outside of explicit uses of the literal heads Divide and Subtract, otherwise these operations are directly converted into addition and multiplication. Although this can make pattern matching easier in some cases it also makes it more confusing in others like yours. See Why are numeric division and subtraction not handled better in Mathematica? for another consequence of this.

Pattern matching takes place on (something close to) the FullForm of an expression, not the "pretty-printed" display form that you see in the Notebook. You need to either write patterns mindful of this or find ways around it. One way around it is processing like Numerator and Denominator as halirutan shows. This is preferred when possible. Another is to leverage the low-level Box form behind the display form. For example the cleanest way to match radicals in output is to look for SqrtBox expressions, and some expressions may need to be converted to and from boxes to make seemingly apparent replacements possible.

You can see that in FullForm your two expressions contain x differently:

y/x^2       // FullForm
y/(x^2 + 1) // FullForm

Times[Power[x, -2], y]

Times[Power[Plus[1, Power[x, 2]], -1], y]


Now in Box form as used by the Front End for display:

y/x^2       // ToBoxes
y/(x^2 + 1) // ToBoxes

FractionBox["y", SuperscriptBox["x", "2"]]

FractionBox["y", RowBox[{"1", "+", SuperscriptBox["x", "2"]}]]


One can work off of the SuperscriptBox similarity. This (overly simple) utility function will convert both an expression and a the rule to Box form, do the ReplaceAll, then convert it back again:

boxReplace[expr_, {rule__} | rule_] :=
ToBoxes[expr] /. Map[ToBoxes, {rule}, {2}] // ToExpression


Example:

boxReplace[{y/x^2, y/(x^2 + 1)}, x^2 -> k]

{y/k, y/(1 + k)}


Unevaluated[y/x^2] /. x^2 -> k

returns

y/k