Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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] - 
   x[2])^2])

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

share|improve this question
1  
If you look at FullForm you'll see that you need {Sqrt[(t_)^2] -> t, 1/Sqrt[t_^2] -> 1/t}. –  b.gatessucks Nov 5 '13 at 17:09
2  
Yep, always use FullForm[] when a replacement doesn't work –  belisarius Nov 5 '13 at 17:11
    
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. –  dbm Nov 5 '13 at 17:27
    
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. –  b.gatessucks Nov 5 '13 at 17:58
    
This question has an answer here: Pitfalls. Related question: Why is ReplaceAll behaving like this? –  Michael E2 Nov 5 '13 at 19:02
add comment

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.

2 Answers

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])]
share|improve this answer
add comment

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])
share|improve this answer
add comment

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