Discussion:
[Numpy-discussion] three-way comparisons
Phillip Feldman
2016-05-14 07:23:07 UTC
Permalink
I often find a need to do the type of comparison done by function shown
below. I suspect that this would be more efficient for large arrays if
implemented direction in C. Is there any possibility of adding something
like this to NumPy?

def three_way(x, y):
"""
This function performs a 3-way comparison on `x` and `y`, which must be
either lists or arrays of compatible shape. Each pair of items or
elements--
let's call them x[i] and y[i]--are compared. The corresponding element
in
the output array is 1 if `x[i]` is greater then `y[i]`, -1 of `x[i]` is
less,
and zero if the two are equal.
"""
numpy.greater(y, x).astype(int) - numpy.less(y, x).astype(int)

Phillip
j***@gmail.com
2016-05-14 15:03:29 UTC
Permalink
On Sat, May 14, 2016 at 3:23 AM, Phillip Feldman <
Post by Phillip Feldman
I often find a need to do the type of comparison done by function shown
below. I suspect that this would be more efficient for large arrays if
implemented direction in C. Is there any possibility of adding something
like this to NumPy?
"""
This function performs a 3-way comparison on `x` and `y`, which must be
either lists or arrays of compatible shape. Each pair of items or
elements--
let's call them x[i] and y[i]--are compared. The corresponding element
in
the output array is 1 if `x[i]` is greater then `y[i]`, -1 of `x[i]` is
less,
and zero if the two are equal.
"""
numpy.greater(y, x).astype(int) - numpy.less(y, x).astype(int)
isn't that the same as sign( x- y) ?

Josef
Post by Phillip Feldman
Phillip
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Loading...