2
$\begingroup$

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

$\endgroup$
5
  • 1
    $\begingroup$ If you look at FullForm you'll see that you need {Sqrt[(t_)^2] -> t, 1/Sqrt[t_^2] -> 1/t}. $\endgroup$ Commented Nov 5, 2013 at 17:09
  • 2
    $\begingroup$ Yep, always use FullForm[] when a replacement doesn't work $\endgroup$ Commented Nov 5, 2013 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
    Commented Nov 5, 2013 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$ Commented Nov 5, 2013 at 17:58
  • $\begingroup$ This question has an answer here: Pitfalls. Related question: Why is ReplaceAll behaving like this? $\endgroup$
    – Michael E2
    Commented Nov 5, 2013 at 19:02

2 Answers 2

2
$\begingroup$

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])]
$\endgroup$
1
$\begingroup$

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])
$\endgroup$

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