From df24aa12ade902c5ad2534792ebb2c7f91319da5 Mon Sep 17 00:00:00 2001 From: Saransh Chopra Date: Mon, 22 Jul 2024 11:28:44 +0200 Subject: [PATCH] docs: update intro notebook + readme --- README.md | 36 ++- docs/index.rst | 12 +- docs/usage/intro.ipynb | 482 +++++++++++++++++++++++++---------------- 3 files changed, 327 insertions(+), 203 deletions(-) diff --git a/README.md b/README.md index 039229d1..8a926afd 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ [![codecov percentage][codecov-badge]][codecov-link] [![GitHub Discussion][github-discussions-badge]][github-discussions-link] [![Gitter][gitter-badge]][gitter-link] -[![Code style: black][black-badge]][black-link] [![PyPI platforms][pypi-platforms]][pypi-link] [![PyPI version][pypi-version]][pypi-link] @@ -29,7 +28,7 @@ Main features of Vector: - [SymPy](https://www.sympy.org/en/index.html) vectors - NumPy arrays of vectors (as a [structured array](https://numpy.org/doc/stable/user/basics.rec.html) subclass) - [Awkward Arrays](https://awkward-array.org/) of vectors - - potential for more: CuPy, TensorFlow, Torch, JAX... + - potential for more: CuPy, TensorFlow, Torch... - Awkward backend also implemented in [Numba](https://numba.pydata.org/) for JIT-compiled calculations on vectors. - [JAX](https://awkward-array.org/doc/main/user-guide/how-to-specialize-differentiate-jax.html) and [Dask](https://dask-awkward.readthedocs.io/en/stable/) support through Awkward Arrays. - Distinction between geometrical vectors, which have a minimum of attribute and method names, and vectors representing momentum, which have synonyms like `pt` = `rho`, `energy` = `t`, `mass` = `tau`. @@ -768,12 +767,15 @@ At the moment, it is possible to sub-class vector awkward mixins to extend the v For instance, the `MomentumAwkward` classes can be extended in the following way: ```py -@ak.mixin_class(vector.backends.awkward.behavior) +behavior = vector.backends.awkward.behavior + + +@ak.mixin_class(behavior) class TwoVector(vector.backends.awkward.MomentumAwkward2D): pass -@ak.mixin_class(vector.backends.awkward.behavior) +@ak.mixin_class(behavior) class ThreeVector(vector.backends.awkward.MomentumAwkward3D): pass @@ -794,7 +796,7 @@ vec = ak.zip( "phi": [[1.2, 1.4], [], [1.6], [3.4]], }, with_name="TwoVector", - behavior=vector.backends.awkward.behavior, + behavior=behavior, ) vec @@ -809,7 +811,7 @@ vec.add(vec) Similarly, other vector methods can be used by the new methods internally. ```py -@ak.mixin_class(vector.backends.awkward.behavior) +@ak.mixin_class(behavior) class LorentzVector(vector.backends.awkward.MomentumAwkward4D): @ak.mixin_class_method(np.divide, {numbers.Number}) def divide(self, factor): @@ -831,7 +833,7 @@ vec = ak.zip( "energy": [[50, 51], [], [52], [60]], }, with_name="LorentzVector", - behavior=vector.backends.awkward.behavior, + behavior=behavior, ) vec / 2 @@ -852,8 +854,19 @@ _rank = [TwoVector, ThreeVector, LorentzVector] for lhs, lhs_to in _binary_dispatch_cls.items(): for rhs, rhs_to in _binary_dispatch_cls.items(): out_to = min(lhs_to, rhs_to, key=_rank.index) - vector.backends.awkward.behavior[(np.add, lhs, rhs)] = out_to.add - vector.backends.awkward.behavior[(np.subtract, lhs, rhs)] = out_to.subtract + behavior[(np.add, lhs, rhs)] = out_to.add + behavior[(np.subtract, lhs, rhs)] = out_to.subtract + +vec + vec +vec.to_2D() + vec.to_2D() +``` + +Finally, instead of manually registering the superclass ufuncs, one can use the utility `copy_behaviors` function to copy behavior items for a new subclass - + +```py +behavior.update(ak._util.copy_behaviors("Vector2D", "TwoVector", behavior)) +behavior.update(ak._util.copy_behaviors("Vector3D", "ThreeVector", behavior)) +behavior.update(ak._util.copy_behaviors("Momentum4D", "LorentzVector", behavior)) vec + vec vec.to_2D() + vec.to_2D() @@ -861,6 +874,7 @@ vec.to_2D() + vec.to_2D() ## Talks about vector +- 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/) - 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) - 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) - 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/) @@ -912,8 +926,6 @@ Support for this work was provided by the National Science Foundation cooperativ [actions-badge]: https://github.com/scikit-hep/vector/actions/workflows/ci.yml/badge.svg [actions-link]: https://github.com/scikit-hep/vector/actions -[black-badge]: https://img.shields.io/badge/code%20style-black-000000.svg -[black-link]: https://github.com/psf/black [codecov-badge]: https://codecov.io/gh/scikit-hep/vector/branch/main/graph/badge.svg?token=YBv60ueORQ [codecov-link]: https://codecov.io/gh/scikit-hep/vector [conda-version]: https://img.shields.io/conda/vn/conda-forge/vector.svg @@ -924,7 +936,7 @@ Support for this work was provided by the National Science Foundation cooperativ [gitter-link]: https://gitter.im/Scikit-HEP/vector?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge [license-badge]: https://img.shields.io/badge/License-BSD_3--Clause-blue.svg [license-link]: https://opensource.org/licenses/BSD-3-Clause -[pre-commit-badge]: https://results.pre-commit.ci/badge/github/scikit-hep/vector/develop.svg +[pre-commit-badge]: https://results.pre-commit.ci/badge/github/scikit-hep/vector/main.svg [pre-commit-link]: https://results.pre-commit.ci/repo/github/scikit-hep/vector [pypi-link]: https://pypi.org/project/vector/ [pypi-platforms]: https://img.shields.io/pypi/pyversions/vector diff --git a/docs/index.rst b/docs/index.rst index 526032bc..2d3995e5 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -5,7 +5,7 @@ .. image:: _images/vector-logo.png -|Action status| |Documentation Status| |pre-commit.ci status| |coverage| |GitHub Discussion| |Gitter| |Code style: black| +|Action status| |Documentation Status| |pre-commit.ci status| |coverage| |GitHub Discussion| |Gitter| |PyPI platforms| |PyPI version| |Conda latest releasetatus| |DOI| |License| |Scikit-HEP| @@ -21,10 +21,12 @@ Main features of Vector: * Uses names and conventions set by `ROOT `_'s `TLorentzVector `_ and `Math::LorentzVector `_, as well as `scikit-hep/math `_, `uproot-methods TLorentzVector `_, `henryiii/hepvector `_, and `coffea.nanoevents.methods.vector `_. * Implemented on a variety of backends: * pure Python objects + * `SymPy `_ vectors * NumPy arrays of vectors (as a `structured array `_ subclass) * `Awkward Arrays `_ of vectors - * potential for more: CuPy, TensorFlow, Torch, JAX... -* NumPy/Awkward backends also implemented in `Numba `_ for JIT-compiled calculations on vectors. + * potential for more: CuPy, TensorFlow, Torch... +* Awkward and Object backends also implemented in `Numba `_ for JIT-compiled calculations on vectors. +* `JAX `_ and `Dask `_ support through Awkward Arrays. * Distinction between geometrical vectors, which have a minimum of attribute and method names, and vectors representing momentum, which have synonyms like ``pt`` = ``rho``, ``energy`` = ``t``, ``mass`` = ``tau``. Installation @@ -115,14 +117,12 @@ The API reference details the functionality of each ``class`` and ``function`` p :target: https://github.com/scikit-hep/vector/actions .. |Documentation Status| image:: https://readthedocs.org/projects/vector/badge/?version=latest :target: https://vector.readthedocs.io/en/latest/?badge=latest -.. |pre-commit.ci status| image:: https://results.pre-commit.ci/badge/github/scikit-hep/vector/develop.svg +.. |pre-commit.ci status| image:: https://results.pre-commit.ci/badge/github/scikit-hep/vector/main.svg :target: https://results.pre-commit.ci/repo/github/scikit-hep/vector .. |GitHub Discussion| image:: https://img.shields.io/static/v1?label=Discussions&message=Ask&color=blue&logo=github :target: https://github.com/scikit-hep/vector/discussions .. |Gitter| image:: https://badges.gitter.im/Scikit-HEP/vector.svg :target: https://gitter.im/Scikit-HEP/vector?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge -.. |Code style: black| image:: https://img.shields.io/badge/code%20style-black-000000.svg - :target: https://github.com/psf/black .. |PyPI platforms| image:: https://img.shields.io/pypi/pyversions/vector :target: https://pypi.org/project/vector/ .. |PyPI version| image:: https://badge.fury.io/py/vector.svg diff --git a/docs/usage/intro.ipynb b/docs/usage/intro.ipynb index ea5ab47e..54164e0e 100644 --- a/docs/usage/intro.ipynb +++ b/docs/usage/intro.ipynb @@ -727,7 +727,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "IPython console for SymPy 1.12 (Python 3.11.5-64-bit) (ground types: python)\n", + "IPython console for SymPy 1.13.0 (Python 3.11.5-64-bit) (ground types: python)\n", "\n", "These commands were executed:\n", ">>> from sympy import *\n", @@ -736,7 +736,7 @@ ">>> f, g, h = symbols('f g h', cls=Function)\n", ">>> init_printing()\n", "\n", - "Documentation can be found at https://docs.sympy.org/1.12/\n", + "Documentation can be found at https://docs.sympy.org/1.13.0/\n", "\n" ] } @@ -928,15 +928,25 @@ "$\\displaystyle \\frac{t}{\\sqrt{1 - \\frac{x^{2}}{t^{2}} - \\frac{y^{2}}{t^{2}} - \\frac{z^{2}}{t^{2}}}} + \\frac{x^{2}}{t \\sqrt{1 - \\frac{x^{2}}{t^{2}} - \\frac{y^{2}}{t^{2}} - \\frac{z^{2}}{t^{2}}}} + \\frac{y^{2}}{t \\sqrt{1 - \\frac{x^{2}}{t^{2}} - \\frac{y^{2}}{t^{2}} - \\frac{z^{2}}{t^{2}}}} + \\frac{z^{2}}{t \\sqrt{1 - \\frac{x^{2}}{t^{2}} - \\frac{y^{2}}{t^{2}} - \\frac{z^{2}}{t^{2}}}}$" ], "text/plain": [ - " 2 2 2 \n", - " t x y z \n", - "──────────────────────── + ────────────────────────── + ────────────────────────── + ──────────────────────────\n", - " __________________ __________________ __________________ __________________\n", - " β•± 2 2 2 β•± 2 2 2 β•± 2 2 2 β•± 2 2 2 \n", - " β•± x y z β•± x y z β•± x y z β•± x y z \n", - " β•± 1 - ── - ── - ── tβ‹… β•± 1 - ── - ── - ── tβ‹… β•± 1 - ── - ── - ── tβ‹… β•± 1 - ── - ── - ── \n", - " β•± 2 2 2 β•± 2 2 2 β•± 2 2 2 β•± 2 2 2 \n", - "β•²β•± t t t β•²β•± t t t β•²β•± t t t β•²β•± t t t " + " 2 2 β†ͺ\n", + " t x y β†ͺ\n", + "──────────────────────── + ────────────────────────── + ────────────────────── β†ͺ\n", + " __________________ __________________ ______________ β†ͺ\n", + " β•± 2 2 2 β•± 2 2 2 β•± 2 2 β†ͺ\n", + " β•± x y z β•± x y z β•± x y β†ͺ\n", + " β•± 1 - ── - ── - ── tβ‹… β•± 1 - ── - ── - ── tβ‹… β•± 1 - ── - ── - β†ͺ\n", + " β•± 2 2 2 β•± 2 2 2 β•± 2 2 β†ͺ\n", + "β•²β•± t t t β•²β•± t t t β•²β•± t t β†ͺ\n", + "\n", + "β†ͺ 2 \n", + "β†ͺ z \n", + "β†ͺ ──── + ──────────────────────────\n", + "β†ͺ ____ __________________\n", + "β†ͺ 2 β•± 2 2 2 \n", + "β†ͺ z β•± x y z \n", + "β†ͺ ── tβ‹… β•± 1 - ── - ── - ── \n", + "β†ͺ 2 β•± 2 2 2 \n", + "β†ͺ t β•²β•± t t t " ] }, "execution_count": 35, @@ -965,8 +975,8 @@ " β•± 2 2 2 2 \n", " β•± t - x - y - z βŽ› 2 2 2 2⎞\n", "tβ‹… β•± ───────────────── β‹…βŽt + x + y + z ⎠\n", - " β•± 2 \n", - " β•²β•± t \n", + " β•± 2 \n", + " β•²β•± t \n", "───────────────────────────────────────────────\n", " 2 2 2 2 \n", " t - x - y - z " @@ -1982,60 +1992,60 @@ { "data": { "text/plain": [ - "MomentumNumpy3D([[[(-0.57563366, -1.47699474, -0.47994495),\n", - " ( 0.89352352, -0.54653254, -1.19265982)],\n", - " [( 0.65238098, -1.16036404, -0.77114054),\n", - " ( 0.98179895, 0.83873052, 0.20782947)],\n", - " [( 0.28760738, 1.94304859, 1.41478171),\n", - " ( 0.49411618, -1.04273322, -0.71518308)],\n", - " [(-1.36682398, -0.94597456, -0.73709658),\n", - " ( 2.03310802, -0.30766412, 1.98470129)],\n", - " [( 1.49521531, -0.87638969, -0.23913866),\n", - " (-0.46980167, 0.62148637, 1.99592602)]],\n", + "MomentumNumpy3D([[[( 0.13260391, 0.2601781 , 1.04694552),\n", + " (-0.26319529, -0.77320792, -0.99130649)],\n", + " [( 0.01667648, 0.29866905, 0.18466902),\n", + " ( 1.34383037, -0.10881989, -0.49760439)],\n", + " [( 2.03530493, 0.11366079, -0.82173533),\n", + " (-0.31129007, -1.84168638, -0.16826771)],\n", + " [( 1.58952081, 0.67672986, 0.39419798),\n", + " (-0.34309328, 1.22834158, -0.45125405)],\n", + " [(-0.39677479, 0.89203262, 0.62427518),\n", + " (-0.51305815, -1.06523853, 0.51476415)]],\n", "\n", - " [[(-1.05207609, -1.44829405, -1.10489952),\n", - " ( 0.8946932 , -1.00756739, -0.16826565)],\n", - " [(-0.02981107, -0.42955782, -0.14474439),\n", - " (-0.20049079, -2.35649518, -0.05785158)],\n", - " [(-2.16564064, -0.08356786, 1.13762491),\n", - " (-0.66967829, -1.38382422, 0.09770476)],\n", - " [( 1.37105958, 0.133506 , 1.01661919),\n", - " (-0.33576833, 0.2765389 , -0.44101496)],\n", - " [( 0.28632014, 2.21312791, 0.01082426),\n", - " ( 0.43130588, 0.33686237, -0.78208239)]],\n", + " [[( 0.62534261, 2.19809954, -1.74075339),\n", + " (-0.5823244 , -0.29312602, -0.43676964)],\n", + " [(-3.15626856, 0.11091443, 1.59055725),\n", + " (-1.51497505, 0.73573103, 0.11611239)],\n", + " [(-1.10585758, -1.3744337 , -0.45652228),\n", + " ( 0.74772884, 0.33616902, 0.16559539)],\n", + " [( 1.68097067, -1.74999875, -0.09875288),\n", + " (-1.4415149 , 0.19554285, -0.41443723)],\n", + " [(-0.88453411, -0.43962161, -1.144978 ),\n", + " (-0.70646718, 1.0279956 , 0.15112694)]],\n", "\n", - " [[( 2.5167644 , -0.9666802 , -0.589714 ),\n", - " ( 0.00670535, 0.61154179, -0.5801598 )],\n", - " [( 0.85916569, -1.20686795, -1.09087556),\n", - " (-0.84848278, 0.74349409, -2.25708373)],\n", - " [(-1.06212568, 2.06836331, -0.81753563),\n", - " ( 0.93006167, -0.6716967 , -0.22985325)],\n", - " [(-0.46597827, -1.53912246, 1.2223433 ),\n", - " (-0.27870187, 0.63837853, 1.46091153)],\n", - " [(-0.15849965, 1.28535392, 0.35814527),\n", - " (-0.96411035, -0.48557259, -0.46646355)]],\n", + " [[(-0.167228 , -1.14624761, 0.37325173),\n", + " ( 0.03890062, -0.25074582, 0.75658396)],\n", + " [( 1.24646773, 0.84040834, 0.70366236),\n", + " (-0.93898028, -1.63633977, -0.82303599)],\n", + " [( 0.59744465, -0.56592375, 1.73794445),\n", + " ( 0.09641971, -0.58255055, 1.62074543)],\n", + " [(-0.28663052, 0.3644517 , 1.246621 ),\n", + " (-0.04809795, -0.50406828, 1.47166047)],\n", + " [(-0.34240645, 0.47174417, 0.87089676),\n", + " ( 1.66574864, 0.18921741, 0.39070562)]],\n", "\n", - " [[(-1.55428144, -1.50118239, -0.23267312),\n", - " (-0.95503501, 0.45042773, 1.12925202)],\n", - " [(-0.2755001 , -0.36794289, -0.10719488),\n", - " ( 1.22160482, -0.64129903, -1.50724479)],\n", - " [(-1.79014056, -0.19384452, -1.51068608),\n", - " (-0.59254713, -2.89712863, -1.16307961)],\n", - " [( 1.58128305, -0.4399382 , -1.71098032),\n", - " ( 0.0637261 , 0.0733358 , 1.93891281)],\n", - " [( 0.85877879, -0.40087665, 0.93195061),\n", - " ( 1.54372595, 1.45510561, 0.32922019)]],\n", + " [[( 0.00878644, -1.62536013, -0.42163486),\n", + " ( 0.46505462, 0.02231124, 1.01025727)],\n", + " [( 0.40799765, 1.03055286, -1.67387635),\n", + " (-0.6960781 , 1.34294937, -0.05646045)],\n", + " [( 0.7966022 , 0.8646252 , -0.37478019),\n", + " (-1.59930401, 0.34685381, -0.89294171)],\n", + " [( 0.59402505, 0.98172123, 0.78361029),\n", + " ( 0.8967682 , 0.75803473, -1.61971293)],\n", + " [(-0.00343387, 0.91582888, 1.00745774),\n", + " ( 0.38013935, 0.37748105, 0.61506312)]],\n", "\n", - " [[( 0.33832385, 0.56659414, -0.08377594),\n", - " ( 1.56367367, -0.48733358, 1.22979576)],\n", - " [( 0.35008414, -0.65620365, 0.71939126),\n", - " ( 0.41979301, 0.02219164, 1.11754321)],\n", - " [(-0.31562956, 0.75488482, 1.19409198),\n", - " (-0.09000928, -0.71370357, -0.71353752)],\n", - " [(-0.63108594, -0.71423969, 0.46006698),\n", - " (-0.50036183, -0.11251065, 0.16754473)],\n", - " [( 1.01445154, -0.4988761 , -0.02549549),\n", - " (-1.10230045, -0.23411548, -0.43315263)]]],\n", + " [[( 0.86241151, -0.79418385, -1.46392439),\n", + " ( 0.12601623, -1.75780638, -0.53983752)],\n", + " [(-0.82900978, -1.19255499, -1.09695779),\n", + " ( 0.31537637, -0.78298955, 0.13288536)],\n", + " [( 1.51532109, -1.17606172, -1.21292931),\n", + " (-0.23002288, -0.20392714, -0.13550909)],\n", + " [(-1.41126192, 1.00526992, 1.16169235),\n", + " (-3.41913823, 0.75890305, -2.06607514)],\n", + " [(-1.09784888, 0.53826587, -1.09363904),\n", + " ( 0.47498744, 0.27155865, 0.87275119)]]],\n", " dtype=[('x', '[[{x: -0.969, y: 0.833, z: -0.453}, ..., {x: 0.697, y: -1.54, z: 0.0639}],\n", - " [{x: 0.545, y: 1.39, z: 0.0666}, {x: 0.165, y: 0.887, z: 1.15}],\n", - " [{x: -0.233, y: -0.107, z: 0.298}],\n", - " [{x: 0.83, y: -1.1, z: -0.703}],\n", - " [{x: 0.559, y: 0.26, z: -0.0842}],\n", - " [{x: 1.03, y: -1.42, z: -0.671}, {x: -0.697, y: 1.04, z: -1.14}],\n", - " [{x: 1.71, y: 0.305, z: 2.26}, {x: -1.07, y: 0.411, z: -0.627}],\n", - " [{x: -0.816, y: 0.229, z: 0.0102}],\n", - " [{x: 0.239, y: 0.425, z: -1.58}, {...}, {x: 0.998, y: -1.11, z: -1.12}],\n", - " [{x: -0.592, y: -0.829, z: 1.32}, {x: -0.535, y: 1.83, z: 0.709}],\n", + "
[[{x: 0.0952, y: 2.35, z: -0.225}, {x: 1.12, y: -0.0102, z: 0.648}],\n",
+       " [{x: 0.626, y: -0.231, z: -1.15}, {...}, {x: 0.232, y: 0.773, z: 0.321}],\n",
+       " [{x: 0.153, y: 0.854, z: 1.62}, {...}, ..., {x: -1.03, y: -0.0817, z: 1.32}],\n",
+       " [{x: -0.0757, y: -0.717, z: 0.0196}],\n",
+       " [{x: -0.0664, y: -0.5, z: -0.732}, ..., {x: -0.682, y: 0.555, z: -1.15}],\n",
+       " [{x: 0.231, y: -0.166, z: -0.19}],\n",
+       " [{x: -0.26, y: 0.0152, z: -0.469}, {x: -0.807, y: 1.39, z: 0.51}],\n",
+       " [{x: -0.982, y: 2.04, z: -0.788}],\n",
+       " [{x: -0.601, y: -1.39, z: -0.962}],\n",
+       " [{x: 0.365, y: -0.684, z: 0.384}],\n",
        " ...,\n",
+       " [{x: 1.09, y: -0.864, z: 0.556}, {x: 0.166, y: -0.0708, z: 0.728}],\n",
+       " [{x: 1.47, y: 1.26, z: -2.5}, {x: 1.17, y: 0.418, z: 0.31}],\n",
        " [],\n",
-       " [{x: -1.61, y: 2.06, z: -0.878}],\n",
-       " [{x: 0.626, y: 0.814, z: 0.775}],\n",
-       " [{x: 0.53, y: -1.5, z: -0.0198}],\n",
-       " [{x: -1.27, y: -0.196, z: 0.651}, {...}, {x: 1.87, y: 0.0655, z: -1.39}],\n",
-       " [{x: -0.336, y: -0.503, z: 1.27}, {x: -0.951, y: 0.363, z: 1.13}],\n",
-       " [],\n",
-       " [],\n",
-       " [{x: -1.1, y: -0.0365, z: 0.033}, {...}, {x: -0.9, y: 1.06, z: -0.0743}]]\n",
-       "--------------------------------------------------------------------------\n",
+       " [{x: 0.681, y: 0.755, z: 0.631}],\n",
+       " [{x: -0.795, y: -0.883, z: -0.861}, {x: 0.418, y: 1.04, z: 0.172}],\n",
+       " [{x: -0.176, y: 0.133, z: -0.34}],\n",
+       " [{x: -0.307, y: 0.661, z: 0.976}, {x: 0.911, y: -1.2, z: 1.2}],\n",
+       " [{x: 1.76, y: 0.317, z: -0.544}, {x: -0.691, y: -0.761, z: -0.992}],\n",
+       " [{x: -0.71, y: 0.624, z: -0.0189}, {...}, ..., {x: -1.11, y: 0.629, z: 1.26}]]\n",
+       "-------------------------------------------------------------------------------\n",
        "type: 50 * var * Momentum3D[\n",
        "    x: float64,\n",
        "    y: float64,\n",
@@ -2193,7 +2203,7 @@
        "]
" ], "text/plain": [ - "" + "" ] }, "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",