Skip to content

fix: eta and cottheta of a rho-phi-z vector on the z axis raise ZeroDivisionError - #764

Open
syahra712 wants to merge 1 commit into
scikit-hep:mainfrom
syahra712:fix-eta-zero-rho
Open

syahra712 wants to merge 1 commit into
scikit-hep:mainfrom
syahra712:fix-eta-zero-rho

Conversation

@syahra712

Copy link
Copy Markdown

Description

With Python numbers, eta and cottheta of a rho-phi-z vector on the z axis raise, while the same vector in x-y-z form gives ±inf:

>>> vector.obj(x=0., y=0., z=-3.).eta
-inf
>>> vector.obj(rho=0., phi=0., z=-3.).eta
ZeroDivisionError: float division by zero

deltaR and deltaeta fail the same way, since they use eta. rhophi_z computes z / rho with Python floats (or ints). xy_z divides by lib.sqrt(x**2 + y**2), a NumPy float, so it gets inf instead.

This uses lib.true_divide(z, rho) in spatial/eta.py and spatial/cottheta.py. nan_to_num then gives the same ±inf (or 0 for eta at the origin) as the x-y-z form. It's the same approach as #754 for rapidity. The sympy backend needs a true_divide method for this, and I've added it with exactly the same lines as #754, so either PR can merge first without a conflict.

I found these by checking all properties for vectors with zero components in each coordinate system. The to_beta3 failures at exactly zero energy are a different case, so I left them out.

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 format is clean on the changed files; ruff check only reports existing findings in _methods.py)
  • Does your submission pass tests? New test_rhophi_z_on_z_axis in test_eta.py and test_cottheta.py (int and float zeros, z negative, zero and positive), all failing on main. Full suite: 4884 passed. The one failure, test_issue_463, also fails on main (the deltaangle precision issue in theta and deltaangle lose precision for small angles #759).
  • Does the documentation build with your changes? (no docs changes)
  • Does your submission pass the doctests? (not run)

AI usage

This PR was prepared with Claude Code (Anthropic's Claude). It found the failing cases, wrote the fix and tests, and ran the test suite. I directed the work and take responsibility for the change, and I'm happy to explain or rework any part of it.

…ivisionError

rhophi_z divided z by rho with Python numbers, so rho == 0 raised instead
of giving +-inf like the x-y-z form. Use lib.true_divide, as scikit-hep#754 does
for rapidity.

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.

1 participant