scikit-hep / scikit-hep/vector

Add keyword argument for 3-vector magnitude, so that they can be constructed entirely in spherical coordinates

Open
#167 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

duplicate feature
Dominant language
Python
Stars
99
Forks
42
Avg merge
1d 13h
Merged PRs (30d)
6

Description

(a feature request)

The system to construct vectors is elegant: first 2D, add an argument for 3D, add another for 4D. To quote the docs

In all, there are 12 coordinate systems [in 4-space]: {x-y vs ρ-ϕ in the azimuthal plane} × {z vs θ vs η longitudinally} × {t vs τ temporally}.

But as a consequence, there is no argument to set the magnitude of a 3-vector. Using rho (or pt) remains the radius in the azimuthal (xy) plane.

  • If the user knows that pt stands for "transverse momentum" (having read the docs), this is reasonably clear. But a user who knows rho from spherical coordinates could be unaware that they need to set rho = magnitude * sin(theta).
  • If a 3-vector is "close" to the z-axis, any step that involves a conversion to (rho, phi, theta) introduces rounding errors. I wouldn't call a dealbreaker for a numerical library!
  • If a 3-vector is aligned with the z-axis, it however stops working entirely.

Examples:

import vector
print(vector.__version__)
# 0.8.4

# Naively using `rho` in its spherical meaning
v_spherical = vector.VectorObject3D.from_rhophitheta(1, 0, 0)
print(v_spherical.z) # showing that `rho` is from cylindrical coordinates
# 1.7976931348623157e+308

# Converting the cartesian 3-vector (0,0,1) to "spherical" coordinates
v_cartesian = vector.obj(x=0, y=0, z=1)
print(v_cartesian.mag)
# 1.0
print(v_cartesian.to_rhophitheta()) # can not be represented
# vector.obj(rho=0.0, phi=0.0, theta=0.0)
print(v_cartesian.to_rhophitheta().to_xyz()) # z-component is lost in conversion
# vector.obj(x=0.0, y=0.0, z=0.0)
print(v_cartesian.to_rhophitheta().to_xyz().mag / v_cartesian.mag) # uh-oh!
# 0.0

# Rounding errors for a vector close to the z-axis
v_close = vector.obj(x=0.01, y=0.01, z=1)
print(v_close.to_rhophitheta().mag / v_close.mag) # rounding error
# 1.000000000000742

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the 3D constructor entry point VectorObject3D.from_rhophitheta and the vector.obj factory, then compare them with to_rhophitheta(). Use the examples in this issue as behavioral checks, including a vector aligned with the z-axis and one close to it. Done means a 3-vector can be constructed from spherical coordinates with its magnitude preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.