From a52b92f3699fde06109b672a320f7485f6d88f1c Mon Sep 17 00:00:00 2001 From: Ben Zhang Date: Mon, 27 Jul 2020 01:17:20 -0700 Subject: [PATCH] Add radd, rsub and rmul --- tinynumpy/tinynumpy.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tinynumpy/tinynumpy.py b/tinynumpy/tinynumpy.py index 1428164..b1578b7 100644 --- a/tinynumpy/tinynumpy.py +++ b/tinynumpy/tinynumpy.py @@ -46,7 +46,7 @@ from math import sqrt from copy import copy, deepcopy -from collections.abc import Iterable +from collections import Iterable import operator import tinynumpy.tinylinalg as linalg @@ -764,10 +764,6 @@ def __eq__(self, other): out[:] = [i1==i2 for (i1, i2) in zip(self.flat, other.flat)] return out - - ########################################################################### - # Added some more basic functions to support in place and normal - # mathematical operations like additions, substractions ... def __add__(self, other): '''classic addition ''' @@ -781,6 +777,9 @@ def __add__(self, other): out[:] = [i+j for (i,j) in zip(self.flat, other.flat)] return out + def __radd__(self, other): + return self.__add__(other) + def __sub__(self, other): if (isinstance(other, int) or isinstance(other, float)) : out = empty(self.shape, self.dtype) @@ -792,6 +791,9 @@ def __sub__(self, other): out[:] = [i-j for (i,j) in zip(self.flat, other.flat)] return out + def __rsub__(self, other): + return self.__sub__(other) + def __mul__(self, other): '''multiply element-wise with array or float/scalar''' if (isinstance(other, int) or isinstance(other, float)) : @@ -804,6 +806,9 @@ def __mul__(self, other): out[:] = [i*j for (i,j) in zip(self.flat, other.flat)] return out + def __rmul__(self, other): + return self.__mul__(other) + def __div__(self, other): '''divide element-wise with array or float/scalar''' if (isinstance(other, int) or isinstance(other, float)) :