3
$\begingroup$

I have this list with some duplicate elements and I want to delete duplicate ones.

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

Now I tried to delete duplicate elements with this.

DeleteDuplicates[mylist]

Code output

This doesn't work. I expected that two of these three elements {ConditionalExpression[-x, x <= -1], ConditionalExpression[-x, x <= -1], ConditionalExpression[Abs[x], x <= -1]} should be deleted.

Then I thought probably that was due to Mathematica thinking that x is complex, so I added the assumption that x is real, but this didn't work either.

Assuming[x \[Element] Reals, DeleteDuplicates[mylist]]

results from assumptions code; no difference in result

How can I delete duplicate elements so the three elements above are considered duplicate?

$\endgroup$

4 Answers 4

4
$\begingroup$
DeleteDuplicates[Refine[mylist, Assumptions -> x ∈ Reals]]

gives what you want

res

$\endgroup$
5
$\begingroup$

Try this:

DeleteDuplicates[Refine[Map[LogicalExpand, mylist], Assumptions -> x \[Element] Reals]]

enter image description here

$\endgroup$
1
  • $\begingroup$ This one is a bit longer than bmf's answer. I wonder if there is any case this works but bmf's answer or vice versa. $\endgroup$
    – hana
    Commented Apr 18, 2022 at 15:55
5
$\begingroup$

Assuming you do want to keep one of the equivalent ConditionalExpressions

DeleteDuplicatesBy[mylist, Refine[#, #[[2]]] &]

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

$\endgroup$
2
  • $\begingroup$ I think this would consider {ConditionalExpression[1 - x, 0 <= x <= 1/2], ConditionalExpression[1 - x, x <= 1/2]} duplicates while they aren't. $\endgroup$
    – hana
    Commented Apr 17, 2022 at 23:08
  • $\begingroup$ DeleteDuplicatesBy[mylist, Refine] seems sufficient. $\endgroup$
    – Roman
    Commented May 3 at 19:51
1
$\begingroup$
list =
  {ConditionalExpression[1, -1 <= x <= 0],
   ConditionalExpression[-x, x <= -1],
   ConditionalExpression[-x, x <= -1],
   ConditionalExpression[Abs[x], x <= -1]};

In this case we only need

DeleteDuplicates @ Refine @ list

enter image description here

From the documentation:

"Refine[expr] uses default assumptions ..."

$\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.