4
$\begingroup$

Is there a simple way to merge equivalent conditions in Piecewise? For example, in the following we have (trivially) pw==x.

pw = Piecewise[{{x, x != 0}}, 0];

Is there some way to force this without cheating? I would call the following cheating:

Assuming[x != 0, Refine[pw]]
(* x *)

As a further example, can I get the simplification (for x real) pw2==Abs[x]?

pw2 = Piecewise[{{x, x > 0}}, -x];
$\endgroup$
5
$\begingroup$

In the first case, we have

Simplify[Piecewise[{{x, x != 0}}, 0], x ∈ Reals]
(*  x  *)

In the second case, Abs[x] is a function of a complex variable and will not be treated as equivalent to Piecewise[{{x, x > 0}}, -x]. (This seems correct to me.) If one wants the standard real absolute value, one can add a domain to PiecewiseExpand:

PiecewiseExpand[Abs[x], Reals]
(*  Piecewise[{{-x, x < 0}}, x]  *)
$\endgroup$
  • $\begingroup$ Oops -- wait -- maybe in the first case, x was supposed to be complex? $\endgroup$ – Michael E2 Sep 28 '16 at 22:30
  • $\begingroup$ @ +1 for a useful partial answer. Any insight on why an irrelevant assumption (x real) helps with the first case? In the second example, I was interested in the case with x real (I think this is implicit in any expression involving an inequality). In this case, I think the expression Abs[x] is a reasonable simplification result - just as Abs[x] simplifies to x when I assumes x>0. $\endgroup$ – mikado Sep 29 '16 at 18:14
  • $\begingroup$ @mikado Not really. I thought of it immediately, actually, but I think it was for a bogus reason (now kinda forgotten -- something like the Abs[] example). Note that UnsameQ instead of Unequal works without the assumption: Simplify[Piecewise[{{x, x =!= 0}}, 0]]. -- What I've noticed about Abs[] is that M will simplify it to something else when x is real or positive, etc., but it will not go from an expression to Abs[]. I suppose there's not a strong argument why this behavior is preferably, but I almost never want Abs[x] when x is real (differentiating it is a pain). $\endgroup$ – Michael E2 Sep 29 '16 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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