Skip to content

Conversation

@Zeroto521
Copy link
Contributor

@Zeroto521 Zeroto521 commented Jan 23, 2026

The subclass of np.ndarray uses __array_priority__ to decide the priority.
If we have a (np.ndarray), b (MatrixExpr), and c (MatrixExprCons).

Without setting __array_priority__, __array_priority__ are 0.0.
The priority is the same. And the operation will be called from left to right.
So we call a >= b, it will be a.__ge__(b). Call a + b, it will be a.__add__(b).

With setting __array_priority__. The operation will call the bigger one.
So we call a >= b, it will be b.__le__(a). Call a + b, it will be b.__radd__(a).

This is useful for a + c. It could skip a.__add__(c). And call c.__radd__(a) directly and raise NotImplementError.

See numpy.class.__array_priority__

Added __array_priority__ attributes to MatrixExpr and MatrixExprCons classes to ensure correct precedence in NumPy operations involving these custom ndarray subclasses.
Added a changelog entry noting that `__array_priority__` is now set for MatrixExpr and MatrixExprCons.
Copilot AI review requested due to automatic review settings January 23, 2026 05:52
Copy link
Contributor

Copilot AI left a comment

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 adds __array_priority__ attributes to the MatrixExpr and MatrixExprCons classes to ensure that when these custom array-like types are used in operations with numpy arrays, their custom __array_ufunc__ methods are called instead of numpy's default behavior.

Changes:

  • Set __array_priority__ = 100 for MatrixExpr class
  • Set __array_priority__ = 101 for MatrixExprCons class
  • Added corresponding entry to CHANGELOG.md

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/pyscipopt/matrix.pxi Added __array_priority__ class attributes to MatrixExpr (priority 100) and MatrixExprCons (priority 101) to control numpy ufunc dispatch order
CHANGELOG.md Documented the addition of __array_priority__ for both matrix classes

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

1 participant