fix(units): 2-D np.cross on UnitAwareArray under numpy 2 (+ drop committed artifact) — #301 follow-up#305
Merged
Conversation
…ted artifact Follow-up to #301 (Copilot review): 1. numpy 2.0 removed the 2-D cross product. The UnitAwareArray np.cross handler delegated straight to np.cross, so `np.cross(a2d, b2d)` on unit-aware 2-D vectors raised ValueError under numpy 2. Emulate the 2-D z-component for (...,2) inputs (matching numpy 1.x), delegate to np.cross for 3-D. New tests/test_0758_unit_aware_cross.py (4 cases). 2. Expt_Grains/Particle_numbers.txt (a generated example output, written by docs/examples/porous_flow/advanced/Ex_Explicit_Flow_Grains.py) was swept into #301 by an over-broad `git add -A`. Remove it from version control and gitignore Expt_Grains/. Underworld development team with AI support from Claude Code
Contributor
There was a problem hiding this comment.
Pull request overview
This PR follows up on #301 by restoring NumPy 1.x-style 2-D np.cross behavior for UnitAwareArray under NumPy 2 (which removed the 2-D scalar z-component cross), and by removing an accidentally committed generated artifact while ensuring it won’t be re-added.
Changes:
- Emulates 2-D
np.cross(z-component) forUnitAwareArrayinputs shaped(..., 2)while preserving units; delegates to NumPy for 3-D cross. - Adds a regression test module covering unit-aware 2-D and 3-D
np.cross. - Removes
Expt_Grains/Particle_numbers.txtfrom version control and ignoresExpt_Grains/going forward.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/underworld3/utilities/unit_aware_array.py |
Updates the np.cross array-function implementation to emulate 2-D cross behavior under NumPy 2 while preserving units. |
tests/test_0758_unit_aware_cross.py |
Adds regression tests for 2-D and 3-D unit-aware cross products and basic unit retention. |
Expt_Grains/Particle_numbers.txt |
Removes a generated artifact file that was accidentally committed previously. |
.gitignore |
Ignores the generated Expt_Grains/ directory to prevent future accidental commits. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1443
to
+1451
| # Compute cross product. numpy 2.0 removed the 2-D cross that | ||
| # returned the scalar z-component, so emulate it for (...,2) inputs | ||
| # (matching numpy 1.x behaviour) and delegate to np.cross for 3-D. | ||
| aa = np.asarray(a) | ||
| bb = np.asarray(b) | ||
| if not args and not kwargs and aa.shape[-1] == 2 and bb.shape[-1] == 2: | ||
| result = aa[..., 0] * bb[..., 1] - aa[..., 1] * bb[..., 0] | ||
| else: | ||
| result = np.cross(aa, bb, *args, **kwargs) |
Comment on lines
+18
to
+22
| def test_2d_unit_aware_cross_returns_z_component(): | ||
| a = UnitAwareArray([1.0, 2.0], units="m") | ||
| b = UnitAwareArray([3.0, 4.0], units="m") | ||
| r = np.asarray(np.cross(a, b)) # 1*4 - 2*3 = -2 | ||
| assert r == pytest.approx(-2.0) |
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.
Addresses the two actionable findings from @copilot-pull-request-reviewer on #301.
1. 2-D unit-aware np.cross (real numpy-2 bug)
numpy 2.0 removed the 2-D cross product (scalar z-component). The
UnitAwareArraynp.crosshandler delegated straight tonp.cross, sonp.cross(a2d, b2d)on unit-aware 2-D vectors raisedValueError: Both input arrays must be 3-dimensional vectorsunder numpy 2 — a gap left by #301 (which fixed only the direct 2-D cross indiscretisation_mesh.py).Fix: emulate the 2-D z-component for
(...,2)inputs (matching numpy 1.x), delegate tonp.crossfor 3-D. Units are preserved. Newtests/test_0758_unit_aware_cross.py(4 cases) — verified green under numpy 2.5.2. Committed generated artifact
Expt_Grains/Particle_numbers.txt(written bydocs/examples/porous_flow/advanced/Ex_Explicit_Flow_Grains.py) was swept into #301 by an over-broadgit add -A. Remove it from version control and gitignoreExpt_Grains/.(Copilot's third comment — the free-surface docs landing in #301 — is a scope observation; the docs are legitimate content, so I've left them in place rather than churn them out.)
Underworld development team with AI support from Claude Code