Apply greater than to a list
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.
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
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
.