# How does Simplify give two answers when they are the same?

I am trying to get the CDF of a random variable using TransformedDistribution.

Probability[-t v <= y - x <=
t v, {x \[Distributed] UniformDistribution[{0, l}],
y \[Distributed] UniformDistribution[{0, l}]}] //
Simplify[#, l > 0 && t > 0 && v > 0 && t v < l] &

The result is

$$\left\{ \begin{matrix} \frac{t v (2 l-t v)}{l^2} & l\neq 2 t v \\ \frac{2 t^2 v^2}{l^2}+\frac{1}{4} & l=2 t v \\ 0 & \text{True} \\ \end{matrix}\right.$$

Why does Simplify give an answer as a separate case when $l=2tv$? If I plug $l=2 tv$ into the first and second answers, they will come to the same result.

• Use FullSimplify instead of Simplify Sep 13, 2015 at 3:34
• @BobHanlon FullSimplify gives an answer Rational[3,4], which is the same result if I put l=2tv into the first expression above. So the problem remains. Sep 13, 2015 at 3:50
• This is normal, and I'd venture an optimization - using FullSimplify indeed gives two cases which are equivalent at l=2tv. One requires calculation to resolve, the other doesn't. I'd not want it done another way, particularly when the calc. involved might be time-consuming...
– ciao
Sep 13, 2015 at 5:13

You can simplify the expression using following rewrite rules:

Probability[-t v <= y - x <=
t v, {x \[Distributed] UniformDistribution[{0, l}],
y \[Distributed] UniformDistribution[{0, l}]}] //
Assuming[l > 0 && t > 0 && v > 0 && t v < l,
Simplify@# //. {HoldPattern@
Piecewise[{beg___, {a_, acond_}, mid___, {b_, bcond_},
rest___}, default___] /;
FullSimplify[a == b, And @@ Not /@ {beg}[[All, 2]] && acond] :>
FullSimplify@
Piecewise[{beg, {b,
acond || (! acond && And @@ Not /@ {mid}[[All, 2]] &&
bcond)}, mid, rest}, default],
HoldPattern@
Piecewise[{beg___, {a_, acond_}, mid___, {b_, bcond_},
rest___}, default___] /;
FullSimplify[a == b,
And @@ Not /@ {beg}[[All, 2]] && ! acond &&
And @@ Not /@ {mid}[[All, 2]] && bcond] :>
FullSimplify@
Piecewise[{beg, {a,
acond || (! acond && And @@ Not /@ {mid}[[All, 2]] &&
bcond)}, mid, rest}, default]}] &

This rule rewrites all non-default Piecewise regions that FullSimplify can pairwise reason to have the same value under their domain to combined regions, and simplifies the resulting expression. (Pedantic reconstruction of condition chain is a bit messy, I admit.) I can't guarantee this works gracefully in general, but on above case the result is clean:

$$\frac{t v (2 l-t v)}{l^2}$$

Using a in place of l for readability

pr[a_, t_, v_] = Assuming[
{a > 0, t > 0, v > 0, t v < a},
Probability[-t v <= y - x <= t v,
{x \[Distributed] UniformDistribution[{0, a}],
y \[Distributed] UniformDistribution[{0, a}]}] //
FullSimplify]

(* Piecewise[{{(t*v*(2*a - t*v))/a^2, a != 2*t*v}},
Rational[3, 4]] *)

You can overcome the limitation by either extraction

pr[a_, t_, v_] = Assuming[
{a > 0, t > 0, v > 0, t v < a},
Probability[-t v <= y - x <= t v,
{x \[Distributed] UniformDistribution[{0, a}],
y \[Distributed] UniformDistribution[{0, a}]}] //
FullSimplify][[1,
1, 1]]

(* (t*v*(2*a - t*v))/a^2 *)

Or by including the additional assumption

pr[a_, t_, v_] = Assuming[
{a > 0, t > 0, v > 0, t v < a, a != 2 t v},
Probability[-t v <= y - x <= t v,
{x \[Distributed] UniformDistribution[{0, a}],
y \[Distributed] UniformDistribution[{0, a}]}] //
FullSimplify]

(* (t*v*(2*a - t*v))/a^2 *)