4

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];
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
mikado
  • 16,741
  • 2
  • 20
  • 54

1 Answers1

5

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]  *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • Oops -- wait -- maybe in the first case, x was supposed to be complex? – Michael E2 Sep 28 '16 at 22:30
  • @ +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. – mikado Sep 29 '16 at 18:14
  • 1
    @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). – Michael E2 Sep 29 '16 at 18:33