Skip to main content
Made English clearer and more idiomatic.
Source Link
m_goldberg
  • 108.6k
  • 16
  • 107
  • 263

Condition on effect Using a predicate to control the application of a rule in a replacement

How tocan I make a replacement rule that will only be applied onto those parts of an expression that wouldwill not break somea condition placed on athe whole expression?

For example if we, suppose I have $\frac{xy}{(x-y)(x+y)}$ and I want to change $y$ to $x$ without turning it to infinity so that it becomesgetting the result $\frac{x^2}{(x-y)2x}$; i. Or it not gonna rise some warninge., errorwithout turning original expression into infinity. Further, etcI don't want the replacement to raise warnings or errors.

At the least, I need a way to do a replacement just once, which seems like it should be 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

ReplaceOnce[expr_, rule_Rule] := MapAt[# /. rule &, expr, Position[expr, rule[[1]]][[1]]] 

How can take offI get to my goal from here.?

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.

Using a predicate to control the application of a rule in a replacement

How can I make a replacement rule that will only be applied to those parts of an expression that will not break a condition placed on the whole expression?

For example, suppose I have $\frac{xy}{(x-y)(x+y)}$ and I want to change $y$ to $x$ getting the result $\frac{x^2}{(x-y)2x}$; i.e., without turning original expression into infinity. Further, I don't want the replacement to raise warnings or errors.

At the least, I need a way to do a replacement just once, which seems like it should be 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]]] 

How can I get to my goal from here?

added 143 characters in body
Source Link
swish
  • 8.1k
  • 1
  • 30
  • 50

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.

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.

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.

added 110 characters in body
Source Link
swish
  • 8.1k
  • 1
  • 30
  • 50

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.

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.

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.

Source Link
swish
  • 8.1k
  • 1
  • 30
  • 50
Loading