Fix: ufuncs with scalar-first arg now work on Variables (#486)#1201
Open
gaoflow wants to merge 2 commits into
Open
Fix: ufuncs with scalar-first arg now work on Variables (#486)#1201gaoflow wants to merge 2 commits into
gaoflow wants to merge 2 commits into
Conversation
…snistgov#486) numpy ufuncs reach Variable subclasses via __array_priority__ and __array_wrap__, which preserves argument order: when a scalar is the first argument and a Variable the second, __array_wrap__ wraps the scalar as a _Constant and calls _Constant._BinaryOperatorVariable(..., other=var). The result class is then resolved by _Constant._getArithmeticBaseClass(var), which returned None because neither isinstance(_Constant, CellVariable) nor isinstance(CellVariable, _Constant) holds. baseClass=None makes _BinaryOperatorVariable return NotImplemented, so e.g. numerix.fmax(0.1, var) # NotImplemented np.add(0.1, var) # NotImplemented np.subtract(0.1, var) # NotImplemented np.power(0.1, var) # NotImplemented np.minimum(0.1, var) # NotImplemented even though the symmetric cases (var-first) all work, and even though the plain Python operators (0.1 + var, etc.) work via __radd__. Add the case @guyer described in the issue: elif isinstance(self, _Constant): return other._getArithmeticBaseClass() When self is a bare _Constant wrapping a scalar/array, the result's class is dictated by other. Verified that all the ufuncs listed above now return CellVariable-typed results, with the expected non-commutative arithmetic (0.1 - var == -(var - 0.1), 0.1 / var == 1/(var/0.1), 0.1**var == 1/(...) * etc.). Doctest added in _getArithmeticBaseClass. Local: 112 tests / 27 failures, identical to master baseline (all pre-existing numpy-2.x repr drift).
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
The custom wordlist accepts the plural 'ufuncs' (line 106) but not the singular 'ufunc'. Reword without altering the doctest.
guyer
added a commit
that referenced
this pull request
Jun 16, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: ufuncs with a scalar first arg now work on Variables
Fixes #486.
Problem
numpyufuncs reachVariablesubclasses via__array_priority__ = 100.0andVariable.__array_wrap__, which preserves argument order: when the first argument is a scalar and the second is aVariable,__array_wrap__wraps the scalar as a_Constantand calls_Constant._BinaryOperatorVariable(..., other=var). The result's class is then resolved by…which returns
Nonebecause neitherisinstance(_Constant, CellVariable)norisinstance(CellVariable, _Constant)holds.baseClass = Noneshort-circuits_BinaryOperatorVariableto returnNotImplemented, so every scalar-first ufunc call breaks:The issue (#486, opened by @guyer) titles this "fmax with non-CellVariable" but the bug is much broader: every numpy ufunc with a scalar in the first slot —
np.add,np.subtract,np.multiply,np.divide,np.power,np.minimum,np.maximum,np.fmin,np.fmax, … — fails the same way.Fix
Apply the case @guyer sketched in the issue body:
When
selfis a bare_Constantwrapping a scalar/array, the result's class is dictated byother. The previously-handled cases (isinstance(self, other._getArithmeticBaseClass())andisinstance(other, _Constant)) keep their original behaviour; this only fires whenselfitself is the_Constant.Verification
All previously-broken ufuncs now produce
CellVariable-typed results, with the correct non-commutative semantics preserved:fmax(0.1, var=2.)NotImplemented[2, 2, 2, 2, 2]fmin(0.1, var=2.)NotImplemented[0.1, 0.1, 0.1, 0.1, 0.1]add(0.1, var=2.)NotImplemented[2.1, 2.1, 2.1, 2.1, 2.1]subtract(0.1, var=2.)NotImplemented[-1.9, -1.9, -1.9, -1.9, -1.9]divide(0.1, var=2.)NotImplemented[0.05, 0.05, 0.05, 0.05, 0.05]power(0.1, var=2.)NotImplemented[0.01, 0.01, 0.01, 0.01, 0.01]A doctest is added inline in
Variable._getArithmeticBaseClassthat exercises the_Constant._getArithmeticBaseClass(CellVariable)resolution and thenumerix.fmax(scalar, var) == numerix.fmax(var, scalar)symmetry.Test impact
113 vs 112 = +1 doctest (the new one). 27 vs 27 = same baseline of pre-existing numpy-2.x repr drift in
fipy/variables/*— none of those touch_getArithmeticBaseClass.Scope
Only
fipy/variables/variable.py: a four-lineelifplus a doctest. No public API change.