Repository navigation
fix: compute theta and deltaangle with arctan2 for small angle precision - #760
robinbishtt wants to merge 17 commits into
Conversation
Refactor delta angle calculations to use cross product for better precision and introduce a dispatch map for conversion functions.
Added tests for small angle precision and forward momentum precision in theta calculation.
Add tests for small angle precision and vector angles in different coordinates.
|
The small-angle case improves, but scaling both vectors can now change a 45° angle. With a = vector.obj(x=s, y=0.0, z=0.0)
b = vector.obj(x=s, y=s, z=0.0)
a.deltaangle(b)The cross product is still representable at both scales, but squaring it overflows or underflows. Could we compute its norm with a scaled norm or |
|
@Sinkleberg Thanks, you're right. Squaring the cross product components overflows or underflows for vectors above about 1e77 or below 1e-77 so a 45 degree angle came out as pi/2 or 0. I replaced the norm with a nested hypot so deltaangle is accurate over the whole range again and added your s = 1e100 and 1e-100 cases plus 1e-150, 1e-77, 1e77 and 1e150 as regression tests. SymPy's lib had no hypot so I added one there as well. I checked it against a 600 case random sweep from 1e-150 to 1e150 the worst error was 4e-16. The range is still about 1e154 because mag and rho overflow there too. |
|
Ping @jpivarski @Saransh-cpp |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #760 +/- ##
==========================================
- Coverage 88.88% 88.78% -0.11%
==========================================
Files 96 96
Lines 10783 10691 -92
==========================================
- Hits 9585 9492 -93
- Misses 1198 1199 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Hi @robinbishtt, thanks for opening the PR. Jim has moved on from Scikit-HEP. Sorry, I will not get any time in the couple of months to review this. We will finalize an AI_POLICY (#761) soon. Could you please add AI usage to the PR description. Another maintainer will take a look before me if they have spare time :) |
|
Thanks for letting me know @Saransh-cpp! I just updated the PR description to include the AI usage disclosure at the bottom and synced the branch with cc @henryiii @ikrommyd whenever someone has a moment to take a look or re trigger the CI workflow. Thanks! |
Description
Fixes #759.
theta was computed as arccos(z / mag) and deltaangle as arccos(dot / (|v1| |v2|)). Both lose precision for small angles. Below about 1e-8 rad the result is exactly 0, and theta(1e-6) is already off by 4e-5 relative. The same vector in xytheta and in rhophiz also gave a deltaangle of 1.5e-8 instead of 0. The reproducer is in #759.
This PR switches both to arctan2:
deltaangle.py gets much shorter. Like cross.py, it now implements only the Cartesian case and builds the other 35 coordinate combinations by converting to x, y, z (710 lines down to 146).
Two behaviour changes, also raised in #759:
If you prefer preserving the old behavior for either edge case, I can add an explicit guard.
Tests:
tests/compute/spatial/test_theta.pyandtests/compute/spatial/test_deltaangle.pycovering small angles down to1e-12rad across object and NumPy backends, forward-momentum kinematics (pt = 0.01,pz = 6800.0), origin and pole vectors, antiparallel and perpendicular vectors, and identical vectors converted across all 36 coordinate-system pairs (13 of these cases fail on currentmain).tests/backends/test_sympy.py,tests/compute/sympy/test_conversions.py,tests/compute/sympy/spatial/test_theta.py, andtests/compute/sympy/spatial/test_deltaangle.pywhere the previoussympy.acosexpression was pinned directly.Checklist
pytest: 4960 passed, 17 skipped, including the new small-angle and coordinate-pair precision tests)pytest --doctest-plus src/vector/passes)Before Merging
Summary of commits (12 commits across 8 files, ready to squash on merge):
src/vector/_compute/spatial/theta.py: Switchedxy_zandrhophi_zfromarccos(costheta)toarctan2(rho, z)usingvector._compute.planar.rho.src/vector/_compute/spatial/deltaangle.py: Replaced the 36-combinationarccosdispatch table witharctan2(|v1 x v2|, v1 . v2)routed through Cartesian coordinates.tests/backends/test_sympy.py,tests/compute/sympy/test_conversions.py,tests/compute/sympy/spatial/test_theta.py, andtests/compute/sympy/spatial/test_deltaangle.py: Updated symbolic test expectations fromsympy.acostosympy.atan2and cleaned up indentation/whitespace.tests/compute/spatial/test_theta.pyandtests/compute/spatial/test_deltaangle.py: Added numerical regression tests for small angles, forward scattering, poles/origin, antiparallel vectors, and cross-coordinate consistency.