7
$\begingroup$

How can I apply greater than $>$ to a list?

Example 1

list={1,4,5,-2}

How can I apply $>$ to the list to find all elements of the list greater than 0?

Desired output

{True,True,True,False}

Example 2

list={-(r/(-d + k r)), -(r/(-d + k r)), 0}

How can I apply $>$ to the list to find all elements of the list greater than or equal to 0 in combination with Reduce?

Desired output

The same as

Reduce[{list[[1]] >= 0, list[[2]] >= 0, list[[3]] >= 0}]

which has output

(k < 0 && ((d < 0 && (r <= 0 || r > d/k)) || (d == 
    0 && (r < 0 || r > 0)) || (d > 
    0 && (r < d/k || r >= 0)))) || (k == 
0 && ((d < 0 && r <= 0) || (d > 0 && r >= 0))) || (k > 
0 && ((d < 0 && d/k < r <= 0) || (d > 0 && 0 <= r < d/k)))
$\endgroup$

4 Answers 4

6
$\begingroup$

For the first question, you can map GreaterThan over the list of values. If they are numeric, it will evaluate to True or False.

GreaterThan[0] /@ {1, 4, 5, -2}
(* {True, True, True, False} *)

If the list is not numeric, it does not evaluate,

GreaterThan[0] /@ {-(r/(-d + k r)), -(r/(-d + k r)), 0}
(* {-(r/(-d + k r)) > 0, -(r/(-d + k r)) > 0, False} *)

and so you can feed the above directly to Reduce after removing the last element because Reduce[{anything, False}] returns False.

Reduce[GreaterThan[0] /@ {-(r/(-d + k r)), -(r/(-d + k r))}]
(* (k < 
    0 && ((d <= 0 && (r < 0 || r > d/k)) || (d > 
        0 && (r < d/k || r > 0)))) || (k == 
    0 && ((d < 0 && r < 0) || (d > 0 && r > 0))) || (k > 
    0 && ((d < 0 && d/k < r < 0) || (d > 0 && 0 < r < d/k))) *)

Note

Reduce[GreaterEqualThan[0] /@ {-(r/(-d + k r)), -(r/(-d + k r)),0}]

solves the second example.

$\endgroup$
1
  • $\begingroup$ Thanks. This is what I was working for. $\endgroup$
    – AzJ
    Commented May 1, 2018 at 17:06
4
$\begingroup$
Positive @ {1, 4, 5, -2} (* or *)
Thread[Greater[{1, 4, 5, -2}, 0]]

{True, True, True, False}

FullSimplify @ Reduce[NonNegative /@ {-(r/(-d + k r)), -(r/(-d + k r))}] (* or *)
FullSimplify @ Reduce[Thread[GreaterEqual[{-(r/(-d + k r)), -(r/(-d + k r))}, 0]]]

(k < 0 && ((d == 0 && r != 0) || (d < 0 && k r < d) || (d > 0 && d < k r))) || (d < 0 && r <= 0 && (d < k r || k <= 0)) || (d > 0 && r >= 0 && (d > k r || k <= 0))

Timings: Positive@lst is faster than Thread[Greater[lst, 0]] which, in turn, is faster than GreaterThan[0]/@lst:

lst = RandomReal[{-1,1},1000000];
(r1 = Positive[lst];)//RepeatedTiming // First

0.017

(r2 = Thread[Greater[lst, 0]];)//RepeatedTiming // First 

0.414

(r3 = GreaterThan[0]/@lst;)//RepeatedTiming // First

0.538

r1==r2 ==r3

True

$\endgroup$
3
$\begingroup$

For numeric data, integer arithmetric is very fast:

list = RandomInteger[{-10, 10}, {1000000}];
threshold = 2;
a = Greater[#, threshold] & /@ list; // RepeatedTiming // First
b = Positive[Subtract[list, threshold]]; // RepeatedTiming // First
c = Subtract[1, UnitStep[Subtract[threshold, list]]]; // RepeatedTiming // First

a == b
a == (c /. {0 -> False, 1 -> True})

0.507

0.016

0.00370

True

True

You have to interpret 1 as True and 0 as False. For example, this works very well with the three argument version of Pick.

$\endgroup$
3
$\begingroup$

Most compact method:

SetAttributes[Greater, Listable];
{1, 4, 5, -2} > 0
(* {True, True, True, False} *)

SetAttributes[GreaterEqual, Listable]
{-(r/(-d + k r)), -(r/(-d + k r)), 0} >= 0 // Reduce
(* your expected output *)
$\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.