I'm looking for an optimized or cute way of checking if all the elements of an array have the same sign (strictly).
I've been thinking about doing:
N.all(my_array*my_array[0]>0)
as it would check if all the elements have the same sign as the first one, and so the same sign but it doesn't seem cute or elegant.
It seems like a waste to multiply the whole array. Just look at the sign of the first element and use that, I'd say:
N.all(my_array > 0) if my_array[0] > 0 else N.all(my_array < 0)
Try this:
len(N.unique(N.sign(a)))==1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With