Skip to content

enh(array, iop): add new Array attributes and dunders, and corresponding iop functions#7

Open
nicola-bastianello wants to merge 7 commits into
team-decent:mainfrom
nicola-bastianello:dunder
Open

enh(array, iop): add new Array attributes and dunders, and corresponding iop functions#7
nicola-bastianello wants to merge 7 commits into
team-decent:mainfrom
nicola-bastianello:dunder

Conversation

@nicola-bastianello

Copy link
Copy Markdown
Member

this PR adds the following dunders to Array:

  • arithmetic operations: __pos__, __floordiv__, __mod__
  • bitwise operations: __invert__, __or__, __xor__, __lshift__, __rshift__
  • in-place: __ifloordiv__, __ipow__, __imod__, __imatmul__, __iand__, __ior__, __ixor__, __ilshift__, __irshift__
  • reflected: __rfloordiv__, __rpow__, __rmod__, __ror__, __rxor__, __rlshift__, __rrshift__
  • methods: __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:

  • element-wise functions: positive, remainder, bitwise_left_shift, bitwise_invert, bitwise_or, bitwise_right_shift, bitwise_xor, floor_divide
  • linear algebra: matrix_transpose

tests for all new features were also added

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Array dunder operators for floor-division/modulo, unary plus, additional bitwise ops/shifts, and scalar coercions (__bool__, __int__, __index__).
  • Added iop functions for matrix_transpose, floor_divide, remainder, and bitwise operators, with backend implementations across NumPy/JAX/PyTorch/TensorFlow.
  • Added tests covering the new iop functions and Array behaviors (including mT and 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.

Comment thread decent_array/interoperability/_iop/math.py
Comment thread decent_array/_array.py Outdated
Comment thread decent_array/_array.py
Comment thread decent_array/interoperability/_tensorflow/tensorflow_backend.py
Comment thread decent_array/interoperability/_abstracts/backend.py

@Simpag Simpag left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one question and one change. The rest looks good

Comment thread decent_array/_array.py
Comment on lines +328 to +330
def __pos__(self) -> Array:
"""Return the array itself."""
return self

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this needed for?

Comment thread decent_array/_array.py
Comment on lines +414 to +416
dtype_name = str(self.value.dtype).split(".")[-1]

dtype = _STRING_TO_DTYPE.get(dtype_name)

@Simpag Simpag Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants