4
$\begingroup$

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?

$\endgroup$

3 Answers 3

6
$\begingroup$

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.

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

Try this:

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

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]:

Simplify`SimplifyCount[Abs[x]]
(*  2  *)

Simplify`SimplifyCount[-x]
(*  4  *)

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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