summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorAaron Meurer <asmeurer@gmail.com>2021-07-15 16:27:59 -0600
committerAaron Meurer <asmeurer@gmail.com>2021-07-15 16:27:59 -0600
commit49bd66076439a1de85c9924d800546973c857f95 (patch)
treed97e7f85e178ce13cfd5e0f850ee25c598cf4f28 /numpy
parent2bdc5c33b1e9eb394eb62533f4ae4df081ea1452 (diff)
downloadnumpy-49bd66076439a1de85c9924d800546973c857f95.tar.gz
Use ndim == 0 instead of shape == () in the array API code
Diffstat (limited to 'numpy')
-rw-r--r--numpy/_array_api/_array_object.py32
1 files changed, 16 insertions, 16 deletions
diff --git a/numpy/_array_api/_array_object.py b/numpy/_array_api/_array_object.py
index 6b9647626..7b5531b9b 100644
--- a/numpy/_array_api/_array_object.py
+++ b/numpy/_array_api/_array_object.py
@@ -55,9 +55,9 @@ class Array:
"""
obj = super().__new__(cls)
- # Note: The spec does not have array scalars, only shape () arrays.
+ # Note: The spec does not have array scalars, only 0-D arrays.
if isinstance(x, np.generic):
- # Convert the array scalar to a shape () array
+ # Convert the array scalar to a 0-D array
xa = np.empty((), x.dtype)
xa[()] = x
x = xa
@@ -130,13 +130,13 @@ class Array:
broadcasting, so the resulting shape is the same, but this prevents NumPy
from not promoting the dtype.
"""
- if x1.shape == () and x2.shape != ():
+ if x1.ndim == 0 and x2.ndim != 0:
# The _array[None] workaround was chosen because it is relatively
# performant. broadcast_to(x1._array, x2.shape) is much slower. We
# could also manually type promote x2, but that is more complicated
# and about the same performance as this.
x1 = Array._new(x1._array[None])
- elif x2.shape == () and x1.shape != ():
+ elif x2.ndim == 0 and x1.ndim != 0:
x2 = Array._new(x2._array[None])
return (x1, x2)
@@ -181,8 +181,8 @@ class Array:
Performs the operation __bool__.
"""
# Note: This is an error here.
- if self._array.shape != ():
- raise TypeError("bool is only allowed on arrays with shape ()")
+ if self._array.ndim != 0:
+ raise TypeError("bool is only allowed on arrays with 0 dimensions")
res = self._array.__bool__()
return res
@@ -216,8 +216,8 @@ class Array:
Performs the operation __float__.
"""
# Note: This is an error here.
- if self._array.shape != ():
- raise TypeError("float is only allowed on arrays with shape ()")
+ if self._array.ndim != 0:
+ raise TypeError("float is only allowed on arrays with 0 dimensions")
res = self._array.__float__()
return res
@@ -278,12 +278,12 @@ class Array:
- Boolean array indices are not allowed as part of a larger tuple
index.
- - Integer array indices are not allowed (with the exception of shape
- () arrays, which are treated the same as scalars).
+ - Integer array indices are not allowed (with the exception of 0-D
+ arrays, which are treated the same as scalars).
Additionally, it should be noted that indices that would return a
- scalar in NumPy will return a shape () array. Array scalars are not allowed
- in the specification, only shape () arrays. This is done in the
+ scalar in NumPy will return a 0-D array. Array scalars are not allowed
+ in the specification, only 0-D arrays. This is done in the
``Array._new`` constructor, not this function.
"""
@@ -335,8 +335,8 @@ class Array:
return key
elif isinstance(key, Array):
if key.dtype in _integer_dtypes:
- if key.shape != ():
- raise IndexError("Integer array indices with shape != () are not allowed in the array API namespace")
+ if key.ndim != 0:
+ raise IndexError("Non-zero dimensional integer array indices are not allowed in the array API namespace")
return key._array
elif key is Ellipsis:
return key
@@ -374,8 +374,8 @@ class Array:
Performs the operation __int__.
"""
# Note: This is an error here.
- if self._array.shape != ():
- raise TypeError("int is only allowed on arrays with shape ()")
+ if self._array.ndim != 0:
+ raise TypeError("int is only allowed on arrays with 0 dimensions")
res = self._array.__int__()
return res