This question already has an answer here:

I have the following expression in Mathematica

    -((E^(2 - 2 Sqrt[(x[1] - x[2])^2]) (x[1] - x[2]))/Sqrt[(x[1] - 

I want to replace anything of the form Sqrt[t^2] to t. I tried

-((E^(2 - 2 Sqrt[(x[1] - x[2])^2]) (x[1] - x[2]))/
  Sqrt[(x[1] - x[2])^2]) /. Sqrt[(t_)^2] -> t

But it just does the changes in the numerator and not in the denominator. What is that I am doing wrong here? Thanks in advance! -dbm


marked as duplicate by Michael E2, Artes, Sjoerd C. de Vries, m_goldberg, rm -rf Nov 6 '13 at 0:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ If you look at FullForm you'll see that you need {Sqrt[(t_)^2] -> t, 1/Sqrt[t_^2] -> 1/t}. $\endgroup$ – b.gates.you.know.what Nov 5 '13 at 17:09
  • 2
    $\begingroup$ Yep, always use FullForm[] when a replacement doesn't work $\endgroup$ – Dr. belisarius Nov 5 '13 at 17:11
  • $\begingroup$ It worked. But I don't understand the concept. Let's say, we want to replace E^(1 - x[1] + x[2]) to m[1,2]. Then the replacement doesn't change E^(2 - 2 (x[1] - x[2])) even thought it should be replaced by m[1,2]^2. $\endgroup$ – dbm Nov 5 '13 at 17:27
  • 1
    $\begingroup$ Because FullForm[Sqrt[x]] == Power[x, Rational[1,2]] while FullForm[1/Sqrt[x]] == Power[x, Rational[-1,2]] and therefore your pattern will not match. $\endgroup$ – b.gates.you.know.what Nov 5 '13 at 17:58
  • $\begingroup$ This question has an answer here: Pitfalls. Related question: Why is ReplaceAll behaving like this? $\endgroup$ – Michael E2 Nov 5 '13 at 19:02

When you have expressions you want to simplify in this way, I like to use PowerExpand. Try

PowerExpand[-((E^(2 - 2 Sqrt[(x[1] - x[2])^2]) (x[1] - x[2]))/Sqrt[(x[1] - x[2])^2])]

You can also use FullSimplify with an assumption that the parameters are real-valued.

FullSimplify[-((E^(2 - 2 Sqrt[(x[1] - x[2])^2]) (x[1] - x[2]))/
             Sqrt[(x[1] - x[2])^2]), Assumptions->{x[1] > x[2] > 0}]

-E^(2 - 2 x[1] + 2 x[2])

Not the answer you're looking for? Browse other questions tagged or ask your own question.