From 2d384745109319f92c2bbfccdebb312f980dd2c2 Mon Sep 17 00:00:00 2001 From: maxgalli <massimiliano.galli.95@gmail.com> Date: Fri, 17 May 2019 15:32:50 +0200 Subject: [PATCH] [Exp PyROOT] Add pythonization for operators of TComplex We add __radd__, __rsub__, __rmul__ and __rtruediv__/__rdiv__ --- .../pyroot_experimental/PyROOT/CMakeLists.txt | 1 + .../python/ROOT/pythonization/_tcomplex.py | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_tcomplex.py diff --git a/bindings/pyroot_experimental/PyROOT/CMakeLists.txt b/bindings/pyroot_experimental/PyROOT/CMakeLists.txt index 39e648c73c4..bfabdaf4568 100644 --- a/bindings/pyroot_experimental/PyROOT/CMakeLists.txt +++ b/bindings/pyroot_experimental/PyROOT/CMakeLists.txt @@ -16,6 +16,7 @@ set(py_sources ROOT/pythonization/_tarray.py ROOT/pythonization/_tclonesarray.py ROOT/pythonization/_tcollection.py + ROOT/pythonization/_tcomplex.py ROOT/pythonization/_tdirectory.py ROOT/pythonization/_tdirectoryfile.py ROOT/pythonization/_tfile.py diff --git a/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_tcomplex.py b/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_tcomplex.py new file mode 100644 index 00000000000..97d91a95515 --- /dev/null +++ b/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_tcomplex.py @@ -0,0 +1,71 @@ +# Author: Massimiliano Galli CERN 05/2019 + +################################################################################ +# Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. # +# All rights reserved. # +# # +# For the licensing terms see $ROOTSYS/LICENSE. # +# For the list of contributors see $ROOTSYS/README/CREDITS. # +################################################################################ + +from ROOT import pythonization +import cppyy + +def _rsub(self, other): + # Parameters: + # - self: complex number + # - other: other (in general not complex) number + return -self+other + +def _perform_division(self, other): + # Parameters: + # - self: complex number + # - other: int, float of long (Py2) number + TComplex = cppyy.gbl.TComplex + other_complex = TComplex.TComplex(other,0) + return other_complex/self + +def _rdiv(self, other): + # Parameters: + # - self: complex number + # - other: other term + if isinstance(other, (int, long, float)): + return _perform_division(self, other) + else: + return NotImplemented + +def _rtruediv(self, other): + # Parameters: + # - self: complex number + # - other: other term + if isinstance(other, (int, float)): + return _perform_division(self, other) + else: + return NotImplemented + +@pythonization() +def pythonize_tcomplex(klass, name): + # Parameters: + # klass: class to be pythonized + # name: string containing the name of the class + if name == 'TComplex': + + # implements __radd__ as equal to __add__ + klass.__radd__ = klass.__add__ + + # implements __rsub__ by assigning the function previously defined + klass.__rsub__ = _rsub + + # implements __rmul__ as equal to __mul__ + klass.__rmul__ = klass.__mul__ + + # implements __rtruediv__ by assigning the function previously defined + # necessary for Python3 + klass.__rtruediv__ = _rtruediv + + # implements __rtruediv__ by assigning the function previously defined + # necessary for Python2 + klass.__rdiv__ = _rdiv + + return True + -- GitLab