"
]
},
"execution_count": 71,
@@ -2224,31 +2234,31 @@
{
"data": {
"text/html": [
- "[[1.28, 0.791, 1.69],\n",
- " [1.49, 0.902],\n",
- " [0.256],\n",
- " [1.38],\n",
- " [0.617],\n",
- " [1.76, 1.25],\n",
- " [1.73, 1.15],\n",
- " [0.847],\n",
- " [0.488, 0.737, 1.49],\n",
- " [1.02, 1.91],\n",
+ "[[2.35, 1.12],\n",
+ " [0.668, 0.985, 0.807],\n",
+ " [0.868, 0.321, 0.209, 1.04],\n",
+ " [0.721],\n",
+ " [0.505, 0.661, 0.426, 0.879],\n",
+ " [0.285],\n",
+ " [0.26, 1.61],\n",
+ " [2.26],\n",
+ " [1.52],\n",
+ " [0.775],\n",
" ...,\n",
+ " [1.39, 0.181],\n",
+ " [1.94, 1.24],\n",
" [],\n",
- " [2.61],\n",
- " [1.03],\n",
- " [1.59],\n",
- " [1.28, 0.39, 1.87],\n",
- " [0.605, 1.02],\n",
- " [],\n",
- " [],\n",
- " [1.1, 0.83, 1.39]]\n",
- "------------------------\n",
+ " [1.02],\n",
+ " [1.19, 1.12],\n",
+ " [0.22],\n",
+ " [0.729, 1.51],\n",
+ " [1.78, 1.03],\n",
+ " [0.945, 0.514, 2.46, 1.27]]\n",
+ "------------------------------\n",
"type: 50 * var * float64
"
],
"text/plain": [
- ""
+ ""
]
},
"execution_count": 72,
@@ -2270,31 +2280,31 @@
{
"data": {
"text/html": [
- "[3,\n",
+ "[2,\n",
+ " 3,\n",
+ " 4,\n",
+ " 1,\n",
+ " 4,\n",
+ " 1,\n",
" 2,\n",
" 1,\n",
" 1,\n",
" 1,\n",
+ " ...,\n",
" 2,\n",
" 2,\n",
- " 1,\n",
- " 3,\n",
- " 2,\n",
- " ...,\n",
" 0,\n",
" 1,\n",
+ " 2,\n",
" 1,\n",
- " 1,\n",
- " 3,\n",
" 2,\n",
- " 0,\n",
- " 0,\n",
- " 3]\n",
+ " 2,\n",
+ " 4]\n",
"----------------\n",
"type: 50 * int64
"
],
"text/plain": [
- ""
+ ""
]
},
"execution_count": 73,
@@ -2316,31 +2326,31 @@
{
"data": {
"text/html": [
- "[3,\n",
+ "[2,\n",
+ " 3,\n",
+ " 4,\n",
+ " 1,\n",
+ " 4,\n",
+ " 1,\n",
" 2,\n",
" 1,\n",
" 1,\n",
" 1,\n",
+ " ...,\n",
" 2,\n",
" 2,\n",
- " 1,\n",
- " 3,\n",
- " 2,\n",
- " ...,\n",
" 0,\n",
" 1,\n",
+ " 2,\n",
" 1,\n",
- " 1,\n",
- " 3,\n",
" 2,\n",
- " 0,\n",
- " 0,\n",
- " 3]\n",
+ " 2,\n",
+ " 4]\n",
"----------------\n",
"type: 50 * int64
"
],
"text/plain": [
- ""
+ ""
]
},
"execution_count": 74,
@@ -3435,27 +3445,27 @@
{
"data": {
"text/html": [
- "[[{x: -0.328, y: 0.127, z: -1.39, t: 11.1}, ..., {x: -0.225, y: -0.658, ...}],\n",
- " [{x: -1.2, y: -0.87, z: -0.965, t: 10.3}],\n",
- " [],\n",
- " [{x: -1.22, y: 0.729, z: -0.103, t: 10.1}, {x: -0.029, y: -0.567, ...}],\n",
+ "[[],\n",
+ " [{x: 0.68, y: 0.799, z: -0.423, t: 10.4}, ..., {x: -0.17, y: 1.11, ...}],\n",
+ " [{x: -1.23, y: 0.529, z: -0.974, t: 11.5}, {x: -1.95, y: 0.159, ...}],\n",
+ " [{x: 0.232, y: -1.05, z: 0.134, t: 8.56}],\n",
+ " [{x: 1.55, y: 0.503, z: 0.609, t: 7.54}],\n",
+ " [{x: -0.181, y: 0.579, z: 0.774, t: 8.93}, {x: -0.361, y: -0.517, ...}],\n",
+ " [{x: 0.746, y: -1.49, z: 0.096, t: 10.2}, ..., {x: -0.274, y: -0.0958, ...}],\n",
+ " [{x: 0.215, y: 1.29, z: -0.908, t: 11.8}],\n",
+ " [{x: -0.621, y: 0.148, z: 0.128, t: 10.6}],\n",
" [],\n",
+ " ...,\n",
" [],\n",
+ " [{x: 1.38, y: -0.677, z: 1.71, t: 11.9}],\n",
" [],\n",
- " [{x: -0.595, y: 1.38, z: -1.53, t: 10.3}, {x: -1.73, y: 0.588, ...}],\n",
- " [{x: 0.366, y: -1.18, z: 0.00993, t: 9.27}, ..., {x: 0.296, y: 1.42, ...}],\n",
- " [{x: 1.01, y: 0.548, z: 0.586, t: 11}, {x: 2.04, y: 0.921, z: ..., ...}],\n",
- " ...,\n",
- " [{x: -1.25, y: 0.28, z: 0.895, t: 10.5}, {x: -0.378, y: 0.766, ...}],\n",
- " [{x: -1.18, y: 0.972, z: 0.16, t: 9.78}],\n",
- " [{x: -0.375, y: 1.71, z: 1.64, t: 8.49}],\n",
- " [{x: 0.388, y: -0.147, z: -0.0319, t: 11.5}, ..., {x: 0.144, y: -0.615, ...}],\n",
- " [{x: -1.07, y: -0.77, z: -1.13, t: 10.3}, {x: 0.183, y: 2, z: ..., ...}],\n",
- " [{x: -0.0761, y: -1.87, z: 0.0424, t: 10.2}, {x: -1.21, y: -0.214, ...}],\n",
+ " [{x: 0.0776, y: -0.233, z: 0.738, t: 11.6}, {x: -0.242, y: 2.69, ...}],\n",
" [],\n",
- " [{x: -0.143, y: 0.775, z: 2.49, t: 11.1}, {x: -0.0686, y: 0.803, ...}],\n",
- " [{x: -0.359, y: -0.0502, z: -1.22, t: 9.41}, ..., {x: 0.565, y: -0.583, ...}]]\n",
- "-------------------------------------------------------------------------------\n",
+ " [{x: 2.12, y: -0.294, z: 0.0647, t: 10}],\n",
+ " [{x: 0.431, y: 0.33, z: -0.372, t: 9.36}],\n",
+ " [{x: -0.144, y: 0.304, z: -0.333, t: 9.21}, {x: -1.65, y: -0.12, ...}],\n",
+ " [{x: 2.4, y: 0.994, z: 1.88, t: 8.42}, {x: 0.418, y: -1.66, z: ..., ...}]]\n",
+ "------------------------------------------------------------------------------\n",
"type: 50 * var * Momentum4D[\n",
" x: float64,\n",
" y: float64,\n",
@@ -3464,7 +3474,7 @@
"]
"
],
"text/plain": [
- ""
+ ""
]
},
"execution_count": 110,
@@ -3516,16 +3526,16 @@
{
"data": {
"text/plain": [
- "array([31.00917422, 10.1059386 , 0. , 19.91593466, 0. ,\n",
- " 0. , 0. , 20.1812881 , 41.54347342, 22.19664008,\n",
- " 19.40152094, 20.94517569, 0. , 10.0249231 , 21.85616549,\n",
- " 19.87767413, 21.35421443, 29.26123961, 50.16621032, 0. ,\n",
- " 18.73543798, 0. , 30.34983157, 0. , 18.85920061,\n",
- " 28.12581142, 10.17168767, 19.98127001, 26.8863055 , 8.45740508,\n",
- " 8.75981725, 17.36536695, 0. , 8.83614441, 18.90126461,\n",
- " 22.31274926, 9.55031958, 27.7945086 , 0. , 18.81627509,\n",
- " 8.51613825, 19.0946717 , 9.65549481, 8.14024787, 31.48012119,\n",
- " 19.96012635, 20.26002769, 0. , 19.17155856, 39.78598547])"
+ "array([ 0. , 29.75052555, 20.62838909, 8.48660722, 7.33516837,\n",
+ " 18.70338324, 29.7146754 , 11.65299876, 10.59925 , 0. ,\n",
+ " 0. , 21.27961128, 0. , 29.59277853, 27.32368944,\n",
+ " 0. , 0. , 10.59714826, 0. , 34.36150967,\n",
+ " 27.76305288, 10.42185596, 8.59241845, 0. , 19.31564032,\n",
+ " 9.3728877 , 20.05084709, 9.29945943, 10.5533362 , 28.56379761,\n",
+ " 10.27649379, 41.35441725, 0. , 10.73854109, 0. ,\n",
+ " 19.35692625, 28.85293189, 32.14146007, 21.7278048 , 10.27831307,\n",
+ " 39.11007941, 0. , 11.63961915, 0. , 20.42034302,\n",
+ " 0. , 9.78956764, 9.33434012, 20.00141989, 18.47871584])"
]
},
"execution_count": 112,
@@ -3556,11 +3566,14 @@
"metadata": {},
"outputs": [],
"source": [
- "@ak.mixin_class(vector.backends.awkward.behavior)\n",
+ "behavior = vector.backends.awkward.behavior\n",
+ "\n",
+ "\n",
+ "@ak.mixin_class(behavior)\n",
"class TwoVector(vector.backends.awkward.MomentumAwkward2D):\n",
" pass\n",
"\n",
- "@ak.mixin_class(vector.backends.awkward.behavior)\n",
+ "@ak.mixin_class(behavior)\n",
"class ThreeVector(vector.backends.awkward.MomentumAwkward3D):\n",
" pass\n",
"\n",
@@ -3610,7 +3623,7 @@
" \"phi\": [[1.2, 1.4], [], [1.6], [3.4]],\n",
" },\n",
" with_name=\"TwoVector\",\n",
- " behavior=vector.backends.awkward.behavior,\n",
+ " behavior=behavior,\n",
")\n",
"vec"
]
@@ -3670,7 +3683,7 @@
"metadata": {},
"outputs": [],
"source": [
- "@ak.mixin_class(vector.backends.awkward.behavior)\n",
+ "@ak.mixin_class(behavior)\n",
"class LorentzVector(vector.backends.awkward.MomentumAwkward4D):\n",
" @ak.mixin_class_method(np.divide, {numbers.Number})\n",
" def divide(self, factor):\n",
@@ -3724,7 +3737,7 @@
" \"energy\": [[50, 51], [], [52], [60]],\n",
" },\n",
" with_name=\"LorentzVector\",\n",
- " behavior=vector.backends.awkward.behavior,\n",
+ " behavior=behavior,\n",
")\n",
"vec"
]
@@ -3853,8 +3866,8 @@
"for lhs, lhs_to in _binary_dispatch_cls.items():\n",
" for rhs, rhs_to in _binary_dispatch_cls.items():\n",
" out_to = min(lhs_to, rhs_to, key=_rank.index)\n",
- " vector.backends.awkward.behavior[(np.add, lhs, rhs)] = out_to.add\n",
- " vector.backends.awkward.behavior[(np.subtract, lhs, rhs)] = out_to.subtract"
+ " behavior[(np.add, lhs, rhs)] = out_to.add\n",
+ " behavior[(np.subtract, lhs, rhs)] = out_to.subtract"
]
},
{
@@ -3923,6 +3936,104 @@
"vec.to_2D() + vec.to_2D()"
]
},
+ {
+ "cell_type": "markdown",
+ "id": "326f3e22-d58e-4e82-9f63-99eab307ac52",
+ "metadata": {},
+ "source": [
+ "Finally, instead of manually registering the superclass ufuncs, one can use the utility `copy_behaviors` function to copy behavior items for a new subclass -"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 124,
+ "id": "48629f7e-306e-438d-bb15-a66f0acbdfc5",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "behavior.update(\n",
+ " ak._util.copy_behaviors(\n",
+ " \"Vector2D\", \"TwoVector\", behavior\n",
+ " )\n",
+ ")\n",
+ "behavior.update(\n",
+ " ak._util.copy_behaviors(\n",
+ " \"Vector3D\", \"ThreeVector\", behavior\n",
+ " )\n",
+ ")\n",
+ "behavior.update(\n",
+ " ak._util.copy_behaviors(\n",
+ " \"Momentum4D\", \"LorentzVector\", behavior\n",
+ " )\n",
+ ")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 125,
+ "id": "8874188b-778e-45af-8320-7f472f9794a2",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "[[{rho: 2, phi: 0.3, eta: 1.2, t: 100}, {rho: 4, phi: 0.4, eta: 1.4, ...}],\n",
+ " [],\n",
+ " [{rho: 6, phi: 0.5, eta: 1.6, t: 104}],\n",
+ " [{rho: 8, phi: 0.6, eta: 3.4, t: 120}]]\n",
+ "---------------------------------------------------------------------------\n",
+ "type: 4 * var * Momentum4D[\n",
+ " rho: float64,\n",
+ " phi: float64,\n",
+ " eta: float64,\n",
+ " t: int64\n",
+ "]
"
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "execution_count": 125,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "vec + vec"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 126,
+ "id": "051f94ac-b875-47f6-8b11-4725eeb6c2f3",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "[[{rho: 2, phi: 0.3}, {rho: 4, phi: 0.4}],\n",
+ " [],\n",
+ " [{rho: 6, phi: 0.5}],\n",
+ " [{rho: 8, phi: 0.6}]]\n",
+ "------------------------------------------\n",
+ "type: 4 * var * Vector2D[\n",
+ " rho: float64,\n",
+ " phi: float64\n",
+ "]
"
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "execution_count": 126,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "vec.to_2D() + vec.to_2D()"
+ ]
+ },
{
"cell_type": "markdown",
"id": "bb6c289a",
@@ -3930,6 +4041,7 @@
"source": [
"## Talks about vector\n",
"\n",
+ "- 3rd July 2024 - [A new SymPy backend for vector: uniting experimental and theoretical physicists](https://indi.to/pfTC6) - [PyHEP 2024 (virtual)](https://indico.cern.ch/event/1384010/)\n",
"- 9th October 2023 - [Whatβs new with Vector? First major release is out!](https://indi.to/35ym5) - [PyHEP 2023 (virtual)](https://indico.cern.ch/event/1252095/) [π₯](https://www.youtube.com/watch?v=JHEAb2R3xzE&list=PLKZ9c4ONm-VlAorAG8kR09ZqhMfHiH2LJ&index=10)\n",
"- 13th September 2022 - [Constructing HEP vectors and analyzing HEP data using Vector](https://indi.to/bPmMc) - [PyHEP 2022 (virtual)](https://indico.cern.ch/event/1150631/) [π₯](https://www.youtube.com/watch?v=4iveMzrbe7s&list=PLKZ9c4ONm-VkohKG-skzEG_gklMaSgaO7&index=15)\n",
"- 20th July 2022 - [Analysis Grand Challenge / HEP Scientific Python Ecosystem](https://indico.cern.ch/event/1151329/timetable/#3-analysis-grand-challenge-hep) - [DANCE/CoDaS@Snowmass 2022 computational and data science software training](https://indico.cern.ch/event/1151329/)\n",