enh(array, iop): add new Array attributes and dunders, and corresponding iop functions#7
enh(array, iop): add new Array attributes and dunders, and corresponding iop functions#7nicola-bastianello wants to merge 7 commits into
Conversation
…s corresponding to new dunders in Array
There was a problem hiding this comment.
Pull request overview
This PR expands decent_array.Array and the decent_array.interoperability (“iop”) surface area to support additional unary/binary/in-place/reflected operators, plus new Array attributes (dtype, mT) and corresponding backend/iop functions (notably matrix_transpose, floor_divide, remainder, and additional bitwise ops).
Changes:
- Added new
Arraydunder operators for floor-division/modulo, unary plus, additional bitwise ops/shifts, and scalar coercions (__bool__,__int__,__index__). - Added
iopfunctions formatrix_transpose,floor_divide,remainder, and bitwise operators, with backend implementations across NumPy/JAX/PyTorch/TensorFlow. - Added tests covering the new iop functions and
Arraybehaviors (includingmTand additional operators).
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
tests/test_iop_functions.py |
Adds tests for matrix_transpose, floor_divide, remainder, and new backend in-place ops + bitwise iop functions. |
tests/test_array.py |
Adds tests for new Array operators (//, %, bitwise ops/shifts) and mT behavior/validation. |
decent_array/types.py |
Introduces _STRING_TO_DTYPE lookup map for DTypes. |
decent_array/_array.py |
Implements new dunders, adds dtype and mT properties, and updates operator hot-path notes. |
decent_array/interoperability/_abstracts/backend.py |
Extends the backend interface with matrix_transpose, new in-place ops, floor-div/mod, and bitwise ops/shifts. |
decent_array/interoperability/_iop/math.py |
Adds floor_divide, remainder, and positive iop functions. |
decent_array/interoperability/_iop/manipulations.py |
Adds matrix_transpose iop function. |
decent_array/interoperability/_iop/bit_operators.py |
Adds iop wrappers for new bitwise ops and shifts. |
decent_array/interoperability/_numpy/numpy_backend.py |
Implements matrix_transpose, new in-place ops, floor-div/mod, and bitwise ops/shifts. |
decent_array/interoperability/_jax/jax_backend.py |
Implements matrix_transpose, new in-place ops, floor-div/mod, and bitwise ops/shifts. |
decent_array/interoperability/_pytorch/pytorch_backend.py |
Implements matrix_transpose, new in-place ops, floor-div/mod, and bitwise ops/shifts. |
decent_array/interoperability/_tensorflow/tensorflow_backend.py |
Implements matrix_transpose, new in-place ops, floor-div/mod, and bitwise ops/shifts. |
decent_array/interoperability/__init__.py |
Exposes newly added iop functions via imports and __all__. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Simpag
left a comment
There was a problem hiding this comment.
Just one question and one change. The rest looks good
| def __pos__(self) -> Array: | ||
| """Return the array itself.""" | ||
| return self |
| dtype_name = str(self.value.dtype).split(".")[-1] | ||
|
|
||
| dtype = _STRING_TO_DTYPE.get(dtype_name) |
There was a problem hiding this comment.
This is a bit hacky and hard to extend. It would be cleaner if this is added to the iop functions. Maybe "dtype_of" or just "dtype" but the former is a bit clearer. Doing it in the iop layer would just require a reversed map of the one thats already in each backend, it would basically be something like:
_INV_DTYPE_MAP = {v: k for k, v in _DTYPE_MAP.items()}
def dtype_of(x):
v = _INV_DTYPE_MAP.get(x.dtype)
if not v:
# Should never reach here since we control dtype but can be if user does something hacky
raise SOME_ERROR
return v
this PR adds the following dunders to Array:
__pos__,__floordiv__,__mod____invert__,__or__,__xor__,__lshift__,__rshift____ifloordiv__,__ipow__,__imod__,__imatmul__,__iand__,__ior__,__ixor__,__ilshift__,__irshift____rfloordiv__,__rpow__,__rmod__,__ror__,__rxor__,__rlshift__,__rrshift____bool__,__index__,__int__it also adds the Array attributes:
dtype,mT(matrix transpose)finally, it adds the iop functions corresponding to the new dunders/attributes:
for iop:
positive,remainder,bitwise_left_shift,bitwise_invert,bitwise_or,bitwise_right_shift,bitwise_xor,floor_dividematrix_transposetests for all new features were also added