-
Notifications
You must be signed in to change notification settings - Fork 24.3k
support for scalars in logical_(and|not|or|xor)
#58740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
We would accept a PR implementing this functionality. |
This can be closed (or remove the
If this is the line that you are referring to, then it probably means that all elements in the tensor should follow this and not that a scalar should be accepted replacing an array/tensor. Maybe @pmeier has more insight? |
@AnirudhDagar @rgommers IIRC, I got this from here:
|
For >>> x = np.array([True, False, True])
>>> x2 = np.arange(3)
>>> x & False
array([False, False, False])
>>> x & True
array([ True, False, True])
>>> x2 & 0
array([0, 0, 0])
>>> x2 & 1
array([0, 1, 0])
>>> x2 & 2
array([0, 0, 2])
>>> x2 & 3
array([0, 1, 2])
>>> x2 & 4
array([0, 0, 0]) It's not impossible that's a NumPy bug, but I do have a vague memory about a decision that |
This is from the NumPy docs here stating the difference between the two.
Also, I tried to confirm and compare the behaviour for the recently merged array API PR into NumPy main branch. #### Extending @rgommers code snippet ####
>>> import numpy as np
>>> np.__version__
'1.22.0.dev0+930.ge778c0cf7'
>>> from numpy import array_api as xp
<stdin>:1: UserWarning: The numpy.array_api submodule is still experimental. See NEP 47.
#### Boolean Inputs Behaviour ####
# Array API
>>> x = xp.asarray([True, False, True])
>>> xp.logical_and(x, True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/gollum/Desktop/Work/numpy/numpy/array_api/_elementwise_functions.py", line 492, in logical_and
if x1.dtype not in _boolean_dtypes or x2.dtype not in _boolean_dtypes:
AttributeError: 'bool' object has no attribute 'dtype'
>>> x & True
Array([ True, False, True], dtype=bool)
### NumPy
>>> x = np.asarray([True, False, True])
>>> np.logical_and(x, True)
array([True, False, True])
>>> x & True
array([True, False, True])
#### Non-logical {0,1} Inputs Behaviour ####
### NumPy
>>> x2 = np.arange(3)
>>> x2 & 0
array([0, 0, 0])
>>> x2 & 1
array([0, 1, 0])
>>> x2 & 2
array([0, 0, 2])
>>> np.logical_and(x2, 0)
array([False, False, False])
>>> np.logical_and(x2, 1)
array([False, True, True])
>>> np.logical_and(x2, 2)
array([False, True, True])
### Array API
>>> x2 = xp.arange(3)
>>> x2 & 0
Array([0, 0, 0], dtype=int64)
>>> x2 & 1
Array([0, 1, 0], dtype=int64)
>>> x2 & 2
Array([0, 0, 2], dtype=int64)
### Raises when using scalars
>>> xp.logical_and(x2, 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/gollum/Desktop/Work/numpy/numpy/array_api/_elementwise_functions.py", line 493, in logical_and
raise TypeError("Only boolean dtypes are allowed in logical_and")
TypeError: Only boolean dtypes are allowed in logical_and
>>> xp.logical_and(x2, 1) # same as above
>>> xp.logical_and(x2, 2) # same as above In conclusion
but for NumPy, all of that works.
Notably, the behaviour in NumPy for |
Thank you for the thorough review, @AnirudhDagar. We should also look at C++ and Python we we consider consistency, and in both C++ and Python a single ampersand is a bitwise and. Matlab just seems to be wrong about this. |
Uh oh!
There was an error while loading. Please reload this page.
The array API specification stipulates that the operators
logical_(and|not|or|xor)
should accept one scalar the same way arithmetic functions do.PyTorch currently does not support that, although the magic methods seem to work just fine:
cc @mruberry @rgommers @pmeier
The text was updated successfully, but these errors were encountered: