# How to simplify function with Abs?

I'm trying to remove Abs below.

mylist = {{ConditionalExpression[Abs[x], x <= -1],
ConditionalExpression[x, x >= 1]}, {ConditionalExpression[
Abs[-1 + x], x <= 0],
ConditionalExpression[-1 + x,
x >= 2]}, {ConditionalExpression[-1 + 2 x, x >= 1],
ConditionalExpression[1 - 2 x, x <= 0]}};


Expected result:

{{ConditionalExpression[-x, x <= -1],
ConditionalExpression[x, x >= 1]}, {ConditionalExpression[1 - x,
x <= 0],
ConditionalExpression[-1 + x,
x >= 2]}, {ConditionalExpression[-1 + 2 x, x >= 1],
ConditionalExpression[1 - 2 x, x <= 0]}}


I wrote this function but somehow it doesn't work well.

f1 = ConditionalExpression[
Simplify[#, #[[2]] && x \[Element] Reals], #[[2]]] &;
Map[f1, mylist, {2}]


Any idea how to do it?

PiecewiseExpand does it.

Edit and // ComplexExpand // Simplify  does it

mylist // ComplexExpand // Simplify

mylist // PiecewiseExpand

(*   {{ConditionalExpression[-x,        x <= -1],
ConditionalExpression[ x,        x >= 1]},
{ConditionalExpression[ 1 - x,    x <= 0],
ConditionalExpression[-1 + x,    x >= 2]},
{ConditionalExpression[-1 + 2 x,  x >= 1],
ConditionalExpression[ 1 - 2 x,  x <= 0]}}   *)


But applying simple // ComplexExpand implies, that you already know that variables are at least Real.

• I thought of that, but like an idiot, I didn't try it. +1 Commented May 2, 2022 at 6:24
• @MichaelE2 , happy that a real expert leaves something for average people to do. Commented May 2, 2022 at 21:03

Try this:

Refine[Simplify[mylist], Assumptions -> x \[Element] Reals]

• Simply Refine[mylist, Assumptions -> x \[Element] Reals] works for me. Commented May 2, 2022 at 0:11
• One might also add that in terms of Simplify, -x is not simpler than Abs[x]. Compare SimplifySimplifyCount[Abs[x]] and SimplifySimplifyCount[-x]. So something like Refine or a complicated ComplexityFunction would need to be used. Commented May 2, 2022 at 0:18
• That's right, @MichaelE2, thanks for clarifying that point. :) Commented May 2, 2022 at 1:09

Here's the ComplexityFunction way I referred to in my comment just to illustrate the alternative:

Simplify[mylist,
ComplexityFunction -> (LeafCount[#] +
5 Count[#, _Abs, {0, Infinity}] &)]
(*
{{ConditionalExpression[-x,        x <= -1],
ConditionalExpression[ x,        x >= 1]},
{ConditionalExpression[ 1 - x,    x <= 0],
ConditionalExpression[-1 + x,    x >= 2]},
{ConditionalExpression[-1 + 2 x,  x >= 1],
ConditionalExpression[ 1 - 2 x,  x <= 0]}}
*)


Note that Simplify is smart enough to use the condition in ConditionalExpression. (I didn't realize that until now. Or I forgot about it.)

The reason the OP was having trouble is that -x is not "simpler" than Abs[x]:

SimplifySimplifyCount[Abs[x]]
(*  2  *)

SimplifySimplifyCount[-x]
(*  4  *)


Examine the FullForm of -x and Abs[x] to see if you can spot why.