Vector
The Vector library provides classes and utilities for representing 2D, 3D, and 4D vectors, primarily for scientific computing, especially within the Scikit-HEP ecosystem. It offers a NumPy-compatible interface for vector operations, with support for various backends including pure Python objects, NumPy arrays, Awkward Arrays, and Numba for JIT-compiled calculations. The current version is 1.8.0, and it maintains a regular release cadence with several minor versions released annually.
Warnings
- breaking Support for older Python versions has been dropped in recent releases. Vector v1.8.0 requires Python 3.10+, and v1.7.0 dropped Python 3.8 support. Users on older Python environments must use an earlier version of the `vector` library.
- breaking Support for Awkward Array v1 was removed in `vector` v1.5.0. If you use `vector` with Awkward Arrays, you must upgrade to Awkward Array v2 or later.
- gotcha Version 1.6.0 of the `vector` library was yanked from PyPI shortly after its release due to a bug where `numpy` functions were incorrectly called on `TypeTracerArray`s. It is recommended to avoid this specific version.
Install
-
pip install vector
Imports
- vector
import vector
- vector.obj
my_vec = vector.obj(x=1, y=2)
- vector.arr
vec_array = vector.arr(x=[1,2,3], y=[4,5,6])
- vector.awk
awk_array = vector.awk(x=ak.Array([1,2]), y=ak.Array([3,4]))
Quickstart
import vector
# Create a 2D Cartesian vector
v2d = vector.obj(x=3, y=4)
print(f"2D vector: {v2d}")
print(f"Magnitude: {v2d.rho}")
# Create a 4D Lorentz vector (momentum-like)
v4d = vector.obj(px=10, py=20, pz=30, mass=5)
print(f"4D vector (momentum): {v4d}")
print(f"Transverse momentum: {v4d.pt}")
# Add two vectors
v_sum = v2d + vector.obj(x=1, y=1)
print(f"Vector sum: {v_sum}")
# Calculate delta_phi between two 2D vectors
v1 = vector.obj(x=1, y=0)
v2 = vector.obj(x=0, y=1)
delta_phi = v1.delta_phi(v2)
print(f"Delta phi between v1 and v2: {delta_phi}")