4
$\begingroup$

Why does this not work? It returns empty while I expect the first two elements are selected.

Select[{{x1, ConditionalExpression[1, 1 <= t <= 5]}, {y1, 
   ConditionalExpression[1, 1 <= t <= 5]}, {y1, 
   ConditionalExpression[1, t >= 1]}}, #[[2]] == 
   ConditionalExpression[1, 1 <= t <= 5] &]

Also, how can I include the third element here?

Select[{{x1, ConditionalExpression[1, 1 <= t <= 5]}, {y1, 
   ConditionalExpression[1, 1 <= t <= 5]}, {y2, 
   ConditionalExpression[1, t >= 1 && t <= 5]}, {y1, 
   ConditionalExpression[1, t >= 1]}}, #[[2]] == 
   ConditionalExpression[1, 1 <= t <= 5] &]
$\endgroup$

2 Answers 2

5
$\begingroup$

Edit 3

I am not explaining the logic again, as it is explained below, but this is much cleaner.

This is the second example

list = {{x1, ConditionalExpression[1, 1 <= t <= 5]}, {y1, 
    ConditionalExpression[1, 1 <= t <= 5]}, {y2, 
    ConditionalExpression[1, t >= 1 && t <= 5]}, {y1, 
    ConditionalExpression[1, t >= 1]}};
rule = {ConditionalExpression[1, 1 <= t <= 5] -> x, 
   ConditionalExpression[1, t >= 1 && t <= 5] -> y};
invrule = Map[Reverse, rule];

Select[list /. rule, ContainsAny[Join[{x, y}]]] /. invrule

list

Edit 2

In more complicated situations, where the rules do not combine nicely, you can create two separate ones, select them individually and join the list. Like so:

list = {{x1, ConditionalExpression[1, 1 <= t <= 5]}, {y1, 
    ConditionalExpression[1, 1 <= t <= 5]}, {y2, 
    ConditionalExpression[1, t >= 1 && t <= 5]}, {y1, 
    ConditionalExpression[1, t >= 1]}};
rule = {ConditionalExpression[1, 1 <= t <= 5] -> x, 
   ConditionalExpression[1, t >= 1 && t <= 5] -> y};
invrule = Map[Reverse, rule];

then

Join[Select[list /. rule, #[[2]] == x &], 
  Select[list /. rule, #[[2]] == y &]] /. invrule

list

Edit 1

You can use the following to invert the rule

list = {{x1, ConditionalExpression[1, 1 <= t <= 5]}, {y1, 
    ConditionalExpression[1, 1 <= t <= 5]}, {y1, 
    ConditionalExpression[1, t >= 1]}};
rule = {ConditionalExpression[1, 1 <= t <= 5] -> x};
invrule = Map[Reverse, rule];

and then

Select[list /. rule, #[[2]] == x &] /. invrule

list

And for the second example

list = {{x1, ConditionalExpression[1, 1 <= t <= 5]}, {y1, ConditionalExpression[1, 1 <= t <= 5]}, {y2, ConditionalExpression[1, t >= 1 && t <= 5]}, {y1, ConditionalExpression[1, t >= 1]}}; rule = {ConditionalExpression[1, 1 <= t <= 5] -> x, ConditionalExpression[1, t >= 1 && t <= 5] -> x}; invrule = Map[Reverse, rule];

and then

Select[list /. rule, #[[2]] == x &] /. invrule

gives

Blockquote

Original

@Nasser gave one way, here's another. Set the condition you want to pick to be x using a rule.

list = {{x1, ConditionalExpression[1, 1 <= t <= 5]}, {y1, 
    ConditionalExpression[1, 1 <= t <= 5]}, {y1, 
    ConditionalExpression[1, t >= 1]}};
rule = ConditionalExpression[1, 1 <= t <= 5] -> x;
invrule = x -> ConditionalExpression[1, 1 <= t <= 5];

and then

Select[list /. rule, #[[2]] == x &] /. invrule

list

$\endgroup$
4
$\begingroup$

while I expect the first two elements are selected.

Use === and not ==

Select[{
  {x1, ConditionalExpression[1, 1 <= t <= 5]},
  {y1, ConditionalExpression[1, 1 <= t <= 5]},
  {y1, ConditionalExpression[1, t >= 1]}},
 #[[2]] === ConditionalExpression[1, 1 <= t <= 5] &]

Mathematica graphics


how can I include the third element here?

Ok, I found why Select does not find y2 for your second question. It is more subtle than I first thought it is.

To see why, it is due to the difference between ConditionalExpression[1, t >= 1 && t <= 5] and ConditionalExpression[1, 1 <= t <= 5]. We see the following

Mathematica graphics

That is why Select did not find the third entry. It is not the same!

Internally one is Inequality and the other is not. Hence did not select it. One workaround is to first change the Inequality like this and then call Select

expr = {{x1, ConditionalExpression[1, 1 <= t <= 5]},
   {y1, ConditionalExpression[1, 1 <= t <= 5]},
   {y2, ConditionalExpression[1, t >= 1 && t <= 5]},
   {y1, ConditionalExpression[1, t >= 1]}};
expr = expr /. Inequality[a_, LessEqual, b_, LessEqual, c_] :> a <= b <= c;
Select[expr, #[[2]] === ConditionalExpression[1, 1 <= t <= 5] &]

Mathematica graphics

So the main reason why it did not find the third entry before, is that under the cover t >= 1 && t <= 5 is not the same as 1 <= t <= 5

$\endgroup$

Your Answer

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

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