Skip to content

fix(units): 2-D np.cross on UnitAwareArray under numpy 2 (+ drop committed artifact) — #301 follow-up#305

Merged
lmoresi merged 1 commit into
developmentfrom
bugfix/numpy2-followup-cross-and-artifact
Jul 3, 2026
Merged

fix(units): 2-D np.cross on UnitAwareArray under numpy 2 (+ drop committed artifact) — #301 follow-up#305
lmoresi merged 1 commit into
developmentfrom
bugfix/numpy2-followup-cross-and-artifact

Conversation

@lmoresi

@lmoresi lmoresi commented Jul 3, 2026

Copy link
Copy Markdown
Member

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 UnitAwareArray np.cross handler delegated straight to np.cross, so np.cross(a2d, b2d) on unit-aware 2-D vectors raised ValueError: Both input arrays must be 3-dimensional vectors under numpy 2 — a gap left by #301 (which fixed only the direct 2-D cross in discretisation_mesh.py).

Fix: emulate the 2-D z-component for (...,2) inputs (matching numpy 1.x), delegate to np.cross for 3-D. Units are preserved. New tests/test_0758_unit_aware_cross.py (4 cases) — verified green under numpy 2.5.

2. Committed generated artifact

Expt_Grains/Particle_numbers.txt (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/.

(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

…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
Copilot AI review requested due to automatic review settings July 3, 2026 08:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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) for UnitAwareArray inputs 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.txt from version control and ignores Expt_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)
@lmoresi lmoresi merged commit eed3476 into development Jul 3, 2026
2 checks passed
@lmoresi lmoresi deleted the bugfix/numpy2-followup-cross-and-artifact branch July 3, 2026 09:17
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.

2 participants