From c8abb89e71c98c2b2ec3c7360a6c26debf114f21 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Sun, 13 Aug 2017 15:20:08 +0800 Subject: sky.py: Overload common arithmetic operators Signed-off-by: Aaron LI --- fg21sim/sky.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/fg21sim/sky.py b/fg21sim/sky.py index 53586c0..e019b2a 100644 --- a/fg21sim/sky.py +++ b/fg21sim/sky.py @@ -4,6 +4,11 @@ """ Generic simulation sky supporting both sky patch and HEALPix all-sky maps. + +References +---------- +* Python - 3. Data Model + https://docs.python.org/3/reference/datamodel.html#special-method-names """ import logging @@ -72,6 +77,59 @@ class SkyBase: self.clobber_ = clobber self.checksum_ = checksum + def __add__(self, other): + """Binary arithmetic operation: ``+``.""" + if isinstance(other, self.__class__): + return self.data + other.data + elif isinstance(other, (int, float, np.ndarray)): + return self.data + other + else: + raise NotImplemented + + def __sub__(self, other): + """Binary arithmetic operation: ``-``.""" + if isinstance(other, self.__class__): + return self.data - other.data + elif isinstance(other, (int, float, np.ndarray)): + return self.data - other + else: + raise NotImplemented + + def __mul__(self, other): + """Binary arithmetic operation: ``*``.""" + if isinstance(other, self.__class__): + return self.data * other.data + elif isinstance(other, (int, float, np.ndarray)): + return self.data * other + else: + raise NotImplemented + + def __truediv__(self, other): + """Binary arithmetic operation: ``/``.""" + if isinstance(other, self.__class__): + return self.data / other.data + elif isinstance(other, (int, float, np.ndarray)): + return self.data / other + else: + raise NotImplemented + + def __pow__(self, other): + """Binary arithmetic operation: ``**``.""" + if isinstance(other, self.__class__): + return self.data ** other.data + elif isinstance(other, (int, float, np.ndarray)): + return self.data ** other + else: + raise NotImplemented + + def __neg__(self): + """Unary arithmetic operation: ``-``.""" + return -self.data + + def __abs__(self): + """Unary arithmetic operation: ``abs()``.""" + return np.abs(self.data) + @property def shape(self): """ -- cgit v1.2.2