Skip to content

fix: rapidity of a massless vector along +z raises ZeroDivisionError - #754

Open
syahra712 wants to merge 1 commit into
scikit-hep:mainfrom
syahra712:fix/rapidity-massless-along-z
Open

syahra712 wants to merge 1 commit into
scikit-hep:mainfrom
syahra712:fix/rapidity-massless-along-z

Conversation

@syahra712

@syahra712 syahra712 commented Oct 21, 2026 •

Copy link
Copy Markdown

Description

On the object backend, rapidity of a massless vector along the beam axis depends on its direction:

>>> import vector
>>> vector.obj(px=0, py=0, pz=-5, E=5).rapidity
-inf
>>> vector.obj(px=0, py=0, pz=5, E=5).rapidity
ZeroDivisionError: division by zero
>>> vector.array({"px": [0.], "py": [0.], "pz": [5.], "E": [5.]}).rapidity
array([inf])

_compute/lorentz/rapidity.py computes 0.5 * lib.log((t + z) / (t - z)). With the object backend, t and z are plain Python numbers, so E == pz hits Python's float division and raises. With -z, the numerator is zero, so the division is fine and lib.log(0) gives -inf. NumPy arrays already return inf. This shows up with incoming beam particles in event records, which have pT = 0 and |pz| = E.

The fix uses lib.true_divide(t + z, t - z), so every backend returns +inf here. It also adds true_divide to the SymPy _lib shim as plain division, so the symbolic expression is the same as before and the existing SymPy tests that compare exact expressions are unchanged.

I added test_massless_along_z in tests/compute/lorentz/test_rapidity.py, covering both directions with int and float pz. It fails on main with ZeroDivisionError and passes with this change.

Checklist

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't any other open Pull Requests for the required change?
  • Does your submission pass pre-commit? (ruff check and format are clean on the changed files)
  • Does your submission pass tests? (pytest tests: 4862 passed. The one failure, test_issue_463, also fails on main in my environment because of floating-point precision. numba isn't available on my machine, so I also ran the numba-object and compute tests in a Linux container: 725 passed. Under njit, rapidity now gives inf / -inf.)
  • Does the documentation build with your changes? (no docs changes)
  • Does your submission pass the doctests?

AI usage

This PR was prepared with Claude Code (Anthropic's Claude). It was used to trace the bug through _compute/lorentz/rapidity.py, write the fix and the new test, and run the test suites listed in the checklist above. I directed the work and take responsibility for the change, and I'm happy to explain or rework any part of it.

…sionError

On the object backend, rapidity computed (t + z) / (t - z) with Python
scalars, so E == pz raised ZeroDivisionError while the mirror case along -z
returned -inf and the NumPy backend returned inf. Use lib.true_divide so all
backends give +inf, and add true_divide to the SymPy lib shim (plain
division, so the symbolic expression is unchanged).
@Saransh-cpp

Copy link
Copy Markdown
Member

Hi @syahra712, thanks for opening the PR. Sorry, I will not get any time in the next 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 :)

@syahra712

Copy link
Copy Markdown
Author

Thanks @Saransh-cpp, no problem at all. I've added an "AI usage" section to the description, and I'll follow the AI_POLICY in #761 once it's finalised.

This branch has not been deployed

No deployments
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