-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: allow python scalars in binary elementwise functions
Allow func(array, scalar) and func(scalar, array), raise on func(scalar, scalar) cross-ref data-apis/array-api#807
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Private helper routines. | ||
""" | ||
import numpy as np | ||
|
||
_py_scalars = (bool, int, float, complex) | ||
|
||
def _maybe_normalize_py_scalars(x1, x2): | ||
from ._array_object import Array | ||
|
||
if isinstance(x1, _py_scalars): | ||
if isinstance(x2, _py_scalars): | ||
raise TypeError(f"Two scalars not allowed, {type(x1) = } and {type(x2) =}") | ||
x1 = Array._new(np.asarray(x1, dtype=x2.dtype._np_dtype), device=x2.device) | ||
elif isinstance(x2, _py_scalars): | ||
x2 = Array._new(np.asarray(x2, dtype=x1.dtype._np_dtype), device=x1.device) | ||
else: | ||
# nothing to do | ||
pass | ||
return x1, x2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters