Skip to main content
3 of 4
added 143 characters in body
swish
  • 8.1k
  • 1
  • 30
  • 50

Condition on effect of replacement

How to make a replacement rule that will only be applied on parts of an expression that would not break some condition on a whole expression?

For example if we have $\frac{xy}{(x-y)(x+y)}$ and I want to change $y$ to $x$ without turning it to infinity so that it becomes $\frac{x^2}{(x-y)2x}$. Or it not gonna rise some warning, error, etc.

At least I need a way to do a replacement just once which seems very easy but I have no idea how to do it.

I came up with ReplaceOnce[expr_, rule_Rule] := MapAt[# /. rule &, expr, Position[expr, rule[[1]]][[1]]]. Now I can take off from here.

swish
  • 8.1k
  • 1
  • 30
  • 50