Skip to content

Commit e1f678a

Browse files
committed
Switch to __array_ufunc__ as a way to avoid numpy broadcasting
1 parent 03229d3 commit e1f678a

File tree

2 files changed

+6
-30
lines changed

2 files changed

+6
-30
lines changed

arraycontext/context.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -306,34 +306,6 @@ def _get_fake_numpy_namespace(self) -> Any:
306306
def __hash__(self) -> int:
307307
raise TypeError(f"unhashable type: '{type(self).__name__}'")
308308

309-
@abstractmethod
310-
def empty(self,
311-
shape: Union[int, Tuple[int, ...]],
312-
dtype: "np.dtype[Any]") -> Array:
313-
pass
314-
315-
@abstractmethod
316-
def zeros(self,
317-
shape: Union[int, Tuple[int, ...]],
318-
dtype: "np.dtype[Any]") -> Array:
319-
pass
320-
321-
def empty_like(self, ary: Array) -> Array:
322-
from warnings import warn
323-
warn(f"{type(self).__name__}.empty_like is deprecated and will stop "
324-
"working in 2023. Prefer actx.np.zeros_like instead.",
325-
DeprecationWarning, stacklevel=2)
326-
327-
return self.empty(shape=ary.shape, dtype=ary.dtype)
328-
329-
def zeros_like(self, ary: Array) -> Array:
330-
from warnings import warn
331-
warn(f"{type(self).__name__}.zeros_like is deprecated and will stop "
332-
"working in 2023. Use actx.np.zeros_like instead.",
333-
DeprecationWarning, stacklevel=2)
334-
335-
return self.zeros(shape=ary.shape, dtype=ary.dtype)
336-
337309
@abstractmethod
338310
def from_numpy(self,
339311
array: NumpyOrContainerOrScalar

test/test_arraycontext.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def __init__(self, actx, data):
136136
self.array_context = actx
137137
self.data = data
138138

139-
__array_priority__ = 10
139+
# prevent numpy broadcasting
140+
__array_ufunc__ = None
140141

141142
def __bool__(self):
142143
if len(self) == 1 and self.data[0].size == 1:
@@ -222,6 +223,8 @@ class MyContainer:
222223
momentum: np.ndarray
223224
enthalpy: Union[DOFArray, np.ndarray]
224225

226+
__array_ufunc__ = None
227+
225228
@property
226229
def array_context(self):
227230
if isinstance(self.mass, np.ndarray):
@@ -1367,7 +1370,8 @@ def test_container_equality(actx_factory):
13671370
class Foo:
13681371
u: DOFArray
13691372

1370-
__array_priority__ = 1 # disallow numpy arithmetic to take precedence
1373+
# prevent numpy arithmetic from taking precedence
1374+
__array_ufunc__ = None
13711375

13721376
@property
13731377
def array_context(self):

0 commit comments

Comments
 (0)