TransWikia.com

ReplaceAll works and does not work for the same expression

Mathematica Asked on April 27, 2021

I will give two cases, where the rule replacement works in the first one and does not work in the second one, but they are essentially the same!

Consider the following definition, whose details are not important:

vall = -Sqrt[Δ^2 + μ^2 + 2*μ*Sqrt[px^2 + py^2] + px^2 + py^2]

Now, we check the following:

vall /. {ϕ -> 0, py -> pyy, px -> 0, μ -> 0}

As expected, I can obtain the desired replacement:

minus square root of pyy^2 + Delta^2: substitution achieved

Now, consider the following expression, which contains the above expression as a subexpression:

Exists[
 {py, pyy}, 
 123 == (vall /. {ϕ -> 0, py -> pyy, px -> 0, μ -> 0}) == 0  ]

The result is surprising:

no substitution was achieved

As can be seen in the picture, no replacement py -> pyy is made!

Why does this happen?

One Answer

I suspect that this has to do with the HoldAll attribute of Exists (see Attributes[Exists]). However, if you force evaluation of its second argument, i.e. the expression you are trying to modify, you obtain the desired result:

Exists[
  {py, pyy}, 
  Evaluate[ 123 == (vall /. {ϕ -> 0, py -> pyy, px -> 0, μ -> 0}) == 0 ]
]

(* Out: Exists[{pyy}, 123 == -Sqrt[pyy^2 + Δ^2] == 0] *)

Correct answer by MarcoB on April 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP