def solve(self): """Solves the ideal gas equation for the unknown value. Returns ------- The value (with the default units) for the corresponding unknown. """ left, right = None, None # Solve for the side without any unknowns first. if self.P and self.V: left = self.conv_P * self.V elif self.n and self.T: right = self.n * Ratm * self.T if right: # Then, solve for the single unknown on the left side. if self.unknown == "P": # Pressure calculations. setattr(self, 'P', operator.__truediv__(right, self.V)) return self.P if self.unknown == "V": # Volume calculations. setattr(self, 'V', operator.__truediv__(right, self.conv_P)) return self.V if left: # Otherwise, solve for the single unknown on the right side. if self.unknown == "n": # Moles calculations. setattr( self, 'n', operator.__truediv__(left, operator.__mul__(Ratm, self.conv_T))) return self.n if self.unknown == "T": # Temperature calculations. setattr( self, 'T', operator.__truediv__(left, operator.__mul__(Ratm, self.n))) return self.T
def cross_correlation_2d(img, kernel): # 1. padding image - about O(N^2). size0 = img.shape[0] size1 = img.shape[1] k_size = kernel.shape[0] padded_img = image_padding_2d(img, k_size // 2) # 2. apply cross correlation using iteration - O(N^2 * K^2). filtered_img = np.zeros((size0, size1)) y_range = range(size1) elapsed_ = list(range(0, size0, size0 // 20)) for x in range(size0): for y in y_range: # at each (x, y) point, cross correlation using kernel is calculated. _sum = 0 for i in range(x, x + k_size): for j in range(y, y + k_size): _sum = operator.__add__( _sum, operator.__mul__(padded_img[i][j], kernel[i - x][j - y])) filtered_img[x][y] = _sum #print('%.04f'%(filtered_img[x][y]), end=' ') #print() if x in elapsed_: print('.', end='') #print(filtered_img) #filtered_img = filtered_img.astype('uint8') return filtered_img
def molarity(compound, setting=None, moles=None, volume=None): """ Calculations involving the molarity of a compound. Returns a value based on the setting. The compound must be the Compound class. The moles/volume setting will be gathered from the compound itself if defined. **Volume is assumed to be in milliliters. Setting --> Molarity: Returns the molarity of the compound from moles and volume. Setting --> Moles: Returns the moles of the compound from molarity and volume. Setting --> Volume: Returns the volume of the compound from moles and volume. """ # Initialize settings: if setting not in ["molarity", "moles", "volume"]: raise ValueError("You must choose a setting: molarity, moles volume.") if not isinstance(compound, Compound): raise AttributeError( "You must include a Compound class as the main argument") if compound.volume and not volume: volume = compound.volume if not compound.volume and not volume and setting in ["molarity", "moles"]: raise AttributeError( "You must define volume either through the Compound class or through the method." ) if compound.mole_amount and not moles: moles = compound.mole_amount if not compound.mole_amount and not moles and setting in [ "molarity", "volume" ]: raise AttributeError( "You must define the mole amount either through the Compound class or through the method." ) if not compound.molarity and setting in ["moles", "volume"]: raise AttributeError( "You must define the molarity of the solution if you want to calculate molarity." ) # Calculations if setting == "molarity": return operator.__truediv__(moles, volume) if setting == "moles": return operator.__mul__(volume, compound.molarity) if setting == "volume": return operator.__truediv__(moles, compound.molarity) else: return None
def test_basic(self): # 5 + 4 * 2 standard_result = a + b * c # (+ 5 (* 4 2)) operator_result = operator.__add__( a, operator.__mul__( b, c ) ) node_result = outer.__call__() # evaluate via __call__ self.assertEqual(node_result, operator_result) self.assertEqual(node_result, standard_result)
def __init__(self, compound, charge = None, state = "aq", **kwargs): super().__init__(compound, bypass = True) if charge: # Charge can also not be provided. if charge not in range(-5, 6): raise ValueError(f"The value for charge as initiated, {charge}, is too high or too low.") self.charge = charge if state not in ["aq", "s", "l", "g"]: raise ValueError("The state of the compound must be either aqueous, solid, liquid, or gaseous.") self.state = state if 'molarity' in kwargs and 'volume' in kwargs: self.molarity = kwargs['molarity'] self.volume = kwargs['volume'] self.mole_amount = operator.__mul__(self.molarity, self.volume) if 'molarity' in kwargs and 'moles' in kwargs: self.molarity = kwargs['molarity'] self.mole_amount = kwargs['mole_amount'] self.volume = operator.__truediv__(self.mole_amount, self.molarity) if 'moles' in kwargs and 'volume' in kwargs: self.mole_amount = kwargs['moles'] self.volume = kwargs['volume'] self.molarity = operator.__truediv__(self.mole_amount, self.volume)
def __mul__(self, other): if isinstance(other, Number) or len(other) == 1: return Vector(map(lambda x: operator.__mul__(x, other), self)) else: return Vector(map(operator.__mul__, self, other))
def test_sanity(): ''' Performs a set of simple sanity checks on most operators. ''' #__abs__ AreEqual(operator.__abs__(0), 0) AreEqual(operator.__abs__(1), 1) AreEqual(operator.__abs__(-1), 1) AreEqual(operator.__abs__(0.0), 0.0) AreEqual(operator.__abs__(1.1), 1.1) AreEqual(operator.__abs__(-1.1), 1.1) AreEqual(operator.__abs__(0L), 0L) AreEqual(operator.__abs__(1L), 1L) AreEqual(operator.__abs__(-1L), 1L) #__neg__ AreEqual(operator.__neg__(0), 0) AreEqual(operator.__neg__(1), -1) AreEqual(operator.__neg__(-1), 1) AreEqual(operator.__neg__(0.0), 0.0) AreEqual(operator.__neg__(1.1), -1.1) AreEqual(operator.__neg__(-1.1), 1.1) AreEqual(operator.__neg__(0L), 0L) AreEqual(operator.__neg__(1L), -1L) AreEqual(operator.__neg__(-1L), 1L) #__pos__ AreEqual(operator.__pos__(0), 0) AreEqual(operator.__pos__(1), 1) AreEqual(operator.__pos__(-1), -1) AreEqual(operator.__pos__(0.0), 0.0) AreEqual(operator.__pos__(1.1), 1.1) AreEqual(operator.__pos__(-1.1), -1.1) AreEqual(operator.__pos__(0L), 0L) AreEqual(operator.__pos__(1L), 1L) AreEqual(operator.__pos__(-1L), -1L) #__add__ AreEqual(operator.__add__(0, 0), 0) AreEqual(operator.__add__(1, 2), 3) AreEqual(operator.__add__(-1, 2), 1) AreEqual(operator.__add__(0.0, 0.0), 0.0) AreEqual(operator.__add__(1.1, 2.1), 3.2) AreEqual(operator.__add__(-1.1, 2.1), 1.0) AreEqual(operator.__add__(0L, 0L), 0L) AreEqual(operator.__add__(1L, 2L), 3L) AreEqual(operator.__add__(-1L, 2L), 1L) #__sub__ AreEqual(operator.__sub__(0, 0), 0) AreEqual(operator.__sub__(1, 2), -1) AreEqual(operator.__sub__(-1, 2), -3) AreEqual(operator.__sub__(0.0, 0.0), 0.0) AreEqual(operator.__sub__(1.1, 2.1), -1.0) AreEqual(operator.__sub__(-1.1, 2.1), -3.2) AreEqual(operator.__sub__(0L, 0L), 0L) AreEqual(operator.__sub__(1L, 2L), -1L) AreEqual(operator.__sub__(-1L, 2L), -3L) #__mul__ AreEqual(operator.__mul__(0, 0), 0) AreEqual(operator.__mul__(1, 2), 2) AreEqual(operator.__mul__(-1, 2), -2) AreEqual(operator.__mul__(0.0, 0.0), 0.0) AreEqual(operator.__mul__(2.0, 3.0), 6.0) AreEqual(operator.__mul__(-2.0, 3.0), -6.0) AreEqual(operator.__mul__(0L, 0L), 0L) AreEqual(operator.__mul__(1L, 2L), 2L) AreEqual(operator.__mul__(-1L, 2L), -2L) #__div__ AreEqual(operator.__div__(0, 1), 0) AreEqual(operator.__div__(4, 2), 2) AreEqual(operator.__div__(-1, 2), -1) AreEqual(operator.__div__(0.0, 1.0), 0.0) AreEqual(operator.__div__(4.0, 2.0), 2.0) AreEqual(operator.__div__(-4.0, 2.0), -2.0) AreEqual(operator.__div__(0L, 1L), 0L) AreEqual(operator.__div__(4L, 2L), 2L) AreEqual(operator.__div__(-4L, 2L), -2L) #__floordiv__ AreEqual(operator.__floordiv__(0, 1), 0) AreEqual(operator.__floordiv__(4, 2), 2) AreEqual(operator.__floordiv__(-1, 2), -1) AreEqual(operator.__floordiv__(0.0, 1.0), 0.0) AreEqual(operator.__floordiv__(4.0, 2.0), 2.0) AreEqual(operator.__floordiv__(-4.0, 2.0), -2.0) AreEqual(operator.__floordiv__(0L, 1L), 0L) AreEqual(operator.__floordiv__(4L, 2L), 2L) AreEqual(operator.__floordiv__(-4L, 2L), -2L) #__truediv__ AreEqual(operator.__truediv__(0, 1), 0) AreEqual(operator.__truediv__(4, 2), 2) AreEqual(operator.__truediv__(-1, 2), -0.5) AreEqual(operator.__truediv__(0.0, 1.0), 0.0) AreEqual(operator.__truediv__(4.0, 2.0), 2.0) AreEqual(operator.__truediv__(-1.0, 2.0), -0.5) AreEqual(operator.__truediv__(0L, 1L), 0L) AreEqual(operator.__truediv__(4L, 2L), 2L) AreEqual(operator.__truediv__(-4L, 2L), -2L) #__mod__ AreEqual(operator.__mod__(0, 1), 0) AreEqual(operator.__mod__(4, 2), 0) AreEqual(operator.__mod__(-1, 2), 1) AreEqual(operator.__mod__(0.0, 1.0), 0.0) AreEqual(operator.__mod__(4.0, 2.0), 0.0) AreEqual(operator.__mod__(-1.0, 2.0), 1.0) AreEqual(operator.__mod__(0L, 1L), 0L) AreEqual(operator.__mod__(4L, 2L), 0L) AreEqual(operator.__mod__(-4L, 2L), 0L) #__inv__ AreEqual(operator.__inv__(0), -1) AreEqual(operator.__inv__(1), -2) AreEqual(operator.__inv__(-1), 0) AreEqual(operator.__inv__(0L), -1L) AreEqual(operator.__inv__(1L), -2L) AreEqual(operator.__inv__(-1L), 0L) #__invert__ AreEqual(operator.__invert__(0), -1) AreEqual(operator.__invert__(1), -2) AreEqual(operator.__invert__(-1), 0) AreEqual(operator.__invert__(0L), -1L) AreEqual(operator.__invert__(1L), -2L) AreEqual(operator.__invert__(-1L), 0L) #__lshift__ AreEqual(operator.__lshift__(0, 1), 0) AreEqual(operator.__lshift__(1, 1), 2) AreEqual(operator.__lshift__(-1, 1), -2) AreEqual(operator.__lshift__(0L, 1), 0L) AreEqual(operator.__lshift__(1L, 1), 2L) AreEqual(operator.__lshift__(-1L, 1), -2L) #__rshift__ AreEqual(operator.__rshift__(1, 1), 0) AreEqual(operator.__rshift__(2, 1), 1) AreEqual(operator.__rshift__(-1, 1), -1) AreEqual(operator.__rshift__(1L, 1), 0L) AreEqual(operator.__rshift__(2L, 1), 1L) AreEqual(operator.__rshift__(-1L, 1), -1L) #__not__ AreEqual(operator.__not__(0), 1) AreEqual(operator.__not__(1), 0) AreEqual(operator.__not__(-1), 0) AreEqual(operator.__not__(0L), 1) AreEqual(operator.__not__(1L), 0) AreEqual(operator.__not__(-1L), 0) #__and__ AreEqual(operator.__and__(0, 0), 0) AreEqual(operator.__and__(1, 1), 1) AreEqual(operator.__and__(0, 1), 0) AreEqual(operator.__and__(1, 0), 0) #__xor__ AreEqual(operator.__xor__(0, 0), 0) AreEqual(operator.__xor__(1, 1), 0) AreEqual(operator.__xor__(0, 1), 1) AreEqual(operator.__xor__(1, 0), 1) #__or__ AreEqual(operator.__or__(0, 0), 0) AreEqual(operator.__or__(1, 1), 1) AreEqual(operator.__or__(0, 1), 1) AreEqual(operator.__or__(1, 0), 1) #__concat__ AreEqual(operator.__concat__([0], [1]), [0,1]) AreEqual(operator.__concat__([2], [1]), [2,1]) AreEqual(operator.__concat__([-1], [1]), [-1,1]) #__contains__ Assert(operator.__contains__("abc", "c")) Assert(not operator.__contains__("abc", "d")) Assert(operator.__contains__("abc", "")) Assert(not operator.__contains__("", "c")) Assert(operator.__contains__([1,2,3], 1)) Assert(not operator.__contains__([1,2,3], 4)) #__getitem__ AreEqual(operator.__getitem__("abc", 2), "c") AssertError(IndexError, operator.__getitem__, "abc", 3) AreEqual(operator.__getitem__([1,2,3], 2), 3) AssertError(IndexError, operator.__getitem__, [1,2,3], 3) #__setitem__ AssertError(TypeError, operator.__setitem__, "abc", 2, "d") t_list = [1,2,3] operator.__setitem__(t_list, 2, 4) AreEqual(t_list, [1,2,4]) AssertError(IndexError, operator.__setitem__, [1,2,3], 4, 9) #__delitem__ #UNIMPLEMENTED #AssertError(TypeError, operator.__delitem__, "abc", 2) t_list = [1,2,3] operator.__delitem__(t_list, 2) AreEqual(t_list, [1,2]) AssertError(IndexError, operator.__delitem__, [1,2,3], 4) #__repeat__ AreEqual(operator.__repeat__("abc", 2), "abcabc") AreEqual(operator.__repeat__("", 2), "") AreEqual(operator.__repeat__([1,2,3], 2), [1,2,3,1,2,3]) #__getslice__ AreEqual(operator.__getslice__("abc", 1, 2), "b") AreEqual(operator.__getslice__("abc", 0, 3), "abc") AreEqual(operator.__getslice__("", 0, 0), "") AreEqual(operator.__getslice__([1,2,3], 1, 2), [2]) AreEqual(operator.__getslice__([1,2,3], 0, 3), [1,2,3]) AreEqual(operator.__getslice__([], 0, 0), []) #__delslice__ t_list = [1,2,3] operator.__delslice__(t_list, 1, 2) AreEqual(t_list, [1,3]) t_list = [1,2,3] operator.__delslice__(t_list, 0, 3) AreEqual(t_list, []) t_list = [1,2,3] operator.__delslice__(t_list, 0, 0) AreEqual(t_list, [1,2,3]) #__setslice__ t_list = [1,2,3] operator.__setslice__(t_list, 1, 2, [9]) AreEqual(t_list, [1,9,3]) t_list = [1,2,3] operator.__setslice__(t_list, 0, 3, [9, 8]) AreEqual(t_list, [9, 8]) t_list = [1,2,3] operator.__setslice__(t_list, 0, 0, [9]) AreEqual(t_list, [9,1, 2,3])
def eval(self, env): return operator.__mul__(self.left.eval(env), self.right.eval(env))
def compute_corner_response(img): ## a) apply Sobel filters # Sobel filtering function is implemented in filtering_by_yoseob.py # sobel_img_x, sobel_img_y is derivatives along x and y direction respectively. sobel_img_x, sobel_img_y = my_sobel_filtering(img) # padded with 0 value to operate corner response sobel_img_x = image_padding_2d(sobel_img_x, 2, 1) # (img, padd_width, type=1) sobel_img_y = image_padding_2d(sobel_img_y, 2, 1) # type=1 means just padd with zeros. ## b) compute second moment matrix M uni_window = np.ones((5, 5)) patch_img_x = np.zeros((5, 5)) patch_img_y = np.zeros((5, 5)) size0 = img.shape[0] size1 = img.shape[1] ## c) variables for computing corner responses R = np.zeros((size0, size1)) _k = 0.04 _max_val = 0 elapsed_ = list(range(0, size0, size0 // 20)) for x in range(size0): for y in range(size1): # i. subtract mean of each image patch _sum_x = 0. _sum_y = 0. for i in range(x, x + 5): for j in range(y, y + 5): patch_img_x[i - x][j - y] = sobel_img_x[i][j] _sum_x = operator.__add__(_sum_x, sobel_img_x[i][j]) patch_img_y[i - x][j - y] = sobel_img_y[i][j] _sum_y = operator.__add__(_sum_y, sobel_img_y[i][j]) _avg_x = operator.__truediv__(_sum_x, 25) _avg_y = operator.__truediv__(_sum_y, 25) sum_of_ix_ix = 0. sum_of_ix_iy = 0. sum_of_iy_iy = 0. for i in range(5): for j in range(5): patch_img_x[i][j] = operator.__sub__( patch_img_x[i][j], _avg_x) patch_img_y[i][j] = operator.__sub__( patch_img_y[i][j], _avg_y) sum_of_ix_ix = operator.__add__( sum_of_ix_ix, operator.__mul__(patch_img_x[i][j], patch_img_x[i][j])) sum_of_ix_iy = operator.__add__( sum_of_ix_iy, operator.__mul__(patch_img_x[i][j], patch_img_y[i][j])) sum_of_iy_iy = operator.__add__( sum_of_iy_iy, operator.__mul__(patch_img_y[i][j], patch_img_y[i][j])) # ii. get second moment matrix # since we use uniform window, just calculated the summation of ix_ix, ix_iy, iy_iy respectively (above). M = np.array([[sum_of_ix_ix, sum_of_ix_iy], [sum_of_ix_iy, sum_of_iy_iy]]) eigenvalues, _ = np.linalg.eig(M) #print(eigen_values) e1 = eigenvalues[0] e2 = eigenvalues[1] R[x][y] = e1 * e2 - _k * ((e1 + e2)**2) ## d) normalize responses # i. negative values to 0, otherwise normalize to range [0, 1] if R[x][y] < 0: R[x][y] = 0 if R[x][y] > _max_val: _max_val = R[x][y] if x in elapsed_: print('.', end='') #print("_max_val:", _max_val) #normalizer = 1. #if _max_val != 0: # normalizer = 1 / _max_val #normalizer = 1 / 255 #normalizer = 1 / np.linalg.norm(R) for x in range(size0): for y in range(size1): R[x][y] = operator.__truediv__(R[x][y], _max_val) return R
def __mul__(self, other): with self._lock: return operator.__mul__(self.__wrapped__, other)
def __mul__(self, value): return operator.__mul__(self._tensor, value)
def __mul__(x, y): if isinstance(x, callproxy): x = x.p_obj if isinstance(y, callproxy): y = y.p_obj return operator.__mul__(x, y)
def eval(self,env): return operator.__mul__(self.left.eval(env),self.right.eval(env))
def test_sanity(self): """Performs a set of simple sanity checks on most operators.""" #__abs__ self.assertEqual(operator.__abs__(0), 0) self.assertEqual(operator.__abs__(1), 1) self.assertEqual(operator.__abs__(-1), 1) self.assertEqual(operator.__abs__(0.0), 0.0) self.assertEqual(operator.__abs__(1.1), 1.1) self.assertEqual(operator.__abs__(-1.1), 1.1) self.assertEqual(operator.__abs__(long(0)), long(0)) self.assertEqual(operator.__abs__(long(1)), long(1)) self.assertEqual(operator.__abs__(-long(1)), long(1)) #__neg__ self.assertEqual(operator.__neg__(0), 0) self.assertEqual(operator.__neg__(1), -1) self.assertEqual(operator.__neg__(-1), 1) self.assertEqual(operator.__neg__(0.0), 0.0) self.assertEqual(operator.__neg__(1.1), -1.1) self.assertEqual(operator.__neg__(-1.1), 1.1) self.assertEqual(operator.__neg__(long(0)), long(0)) self.assertEqual(operator.__neg__(long(1)), -long(1)) self.assertEqual(operator.__neg__(-long(1)), long(1)) #__pos__ self.assertEqual(operator.__pos__(0), 0) self.assertEqual(operator.__pos__(1), 1) self.assertEqual(operator.__pos__(-1), -1) self.assertEqual(operator.__pos__(0.0), 0.0) self.assertEqual(operator.__pos__(1.1), 1.1) self.assertEqual(operator.__pos__(-1.1), -1.1) self.assertEqual(operator.__pos__(long(0)), long(0)) self.assertEqual(operator.__pos__(long(1)), long(1)) self.assertEqual(operator.__pos__(-long(1)), -long(1)) #__add__ self.assertEqual(operator.__add__(0, 0), 0) self.assertEqual(operator.__add__(1, 2), 3) self.assertEqual(operator.__add__(-1, 2), 1) self.assertEqual(operator.__add__(0.0, 0.0), 0.0) self.assertEqual(operator.__add__(1.1, 2.1), 3.2) self.assertEqual(operator.__add__(-1.1, 2.1), 1.0) self.assertEqual(operator.__add__(long(0), long(0)), long(0)) self.assertEqual(operator.__add__(long(1), long(2)), long(3)) self.assertEqual(operator.__add__(-long(1), long(2)), long(1)) #__sub__ self.assertEqual(operator.__sub__(0, 0), 0) self.assertEqual(operator.__sub__(1, 2), -1) self.assertEqual(operator.__sub__(-1, 2), -3) self.assertEqual(operator.__sub__(0.0, 0.0), 0.0) self.assertEqual(operator.__sub__(1.1, 2.1), -1.0) self.assertEqual(operator.__sub__(-1.1, 2.1), -3.2) self.assertEqual(operator.__sub__(long(0), long(0)), long(0)) self.assertEqual(operator.__sub__(long(1), long(2)), -long(1)) self.assertEqual(operator.__sub__(-long(1), long(2)), -long(3)) #__mul__ self.assertEqual(operator.__mul__(0, 0), 0) self.assertEqual(operator.__mul__(1, 2), 2) self.assertEqual(operator.__mul__(-1, 2), -2) self.assertEqual(operator.__mul__(0.0, 0.0), 0.0) self.assertEqual(operator.__mul__(2.0, 3.0), 6.0) self.assertEqual(operator.__mul__(-2.0, 3.0), -6.0) self.assertEqual(operator.__mul__(long(0), long(0)), long(0)) self.assertEqual(operator.__mul__(long(1), long(2)), long(2)) self.assertEqual(operator.__mul__(-long(1), long(2)), -long(2)) #__div__ self.assertEqual(operator.__div__(0, 1), 0) self.assertEqual(operator.__div__(4, 2), 2) self.assertEqual(operator.__div__(-1, 2), -1) self.assertEqual(operator.__div__(0.0, 1.0), 0.0) self.assertEqual(operator.__div__(4.0, 2.0), 2.0) self.assertEqual(operator.__div__(-4.0, 2.0), -2.0) self.assertEqual(operator.__div__(long(0), long(1)), long(0)) self.assertEqual(operator.__div__(long(4), long(2)), long(2)) self.assertEqual(operator.__div__(-long(4), long(2)), -long(2)) #__floordiv__ self.assertEqual(operator.__floordiv__(0, 1), 0) self.assertEqual(operator.__floordiv__(4, 2), 2) self.assertEqual(operator.__floordiv__(-1, 2), -1) self.assertEqual(operator.__floordiv__(0.0, 1.0), 0.0) self.assertEqual(operator.__floordiv__(4.0, 2.0), 2.0) self.assertEqual(operator.__floordiv__(-4.0, 2.0), -2.0) self.assertEqual(operator.__floordiv__(long(0), long(1)), long(0)) self.assertEqual(operator.__floordiv__(long(4), long(2)), long(2)) self.assertEqual(operator.__floordiv__(-long(4), long(2)), -long(2)) #__truediv__ self.assertEqual(operator.__truediv__(0, 1), 0) self.assertEqual(operator.__truediv__(4, 2), 2) self.assertEqual(operator.__truediv__(-1, 2), -0.5) self.assertEqual(operator.__truediv__(0.0, 1.0), 0.0) self.assertEqual(operator.__truediv__(4.0, 2.0), 2.0) self.assertEqual(operator.__truediv__(-1.0, 2.0), -0.5) self.assertEqual(operator.__truediv__(long(0), long(1)), long(0)) self.assertEqual(operator.__truediv__(long(4), long(2)), long(2)) self.assertEqual(operator.__truediv__(-long(4), long(2)), -long(2)) #__mod__ self.assertEqual(operator.__mod__(0, 1), 0) self.assertEqual(operator.__mod__(4, 2), 0) self.assertEqual(operator.__mod__(-1, 2), 1) self.assertEqual(operator.__mod__(0.0, 1.0), 0.0) self.assertEqual(operator.__mod__(4.0, 2.0), 0.0) self.assertEqual(operator.__mod__(-1.0, 2.0), 1.0) self.assertEqual(operator.__mod__(long(0), long(1)), long(0)) self.assertEqual(operator.__mod__(long(4), long(2)), long(0)) self.assertEqual(operator.__mod__(-long(4), long(2)), long(0)) #__inv__ self.assertEqual(operator.__inv__(0), -1) self.assertEqual(operator.__inv__(1), -2) self.assertEqual(operator.__inv__(-1), 0) self.assertEqual(operator.__inv__(long(0)), -long(1)) self.assertEqual(operator.__inv__(long(1)), -long(2)) self.assertEqual(operator.__inv__(-long(1)), long(0)) #__invert__ self.assertEqual(operator.__invert__(0), -1) self.assertEqual(operator.__invert__(1), -2) self.assertEqual(operator.__invert__(-1), 0) self.assertEqual(operator.__invert__(long(0)), -long(1)) self.assertEqual(operator.__invert__(long(1)), -long(2)) self.assertEqual(operator.__invert__(-long(1)), long(0)) #__lshift__ self.assertEqual(operator.__lshift__(0, 1), 0) self.assertEqual(operator.__lshift__(1, 1), 2) self.assertEqual(operator.__lshift__(-1, 1), -2) self.assertEqual(operator.__lshift__(long(0), 1), long(0)) self.assertEqual(operator.__lshift__(long(1), 1), long(2)) self.assertEqual(operator.__lshift__(-long(1), 1), -long(2)) #__rshift__ self.assertEqual(operator.__rshift__(1, 1), 0) self.assertEqual(operator.__rshift__(2, 1), 1) self.assertEqual(operator.__rshift__(-1, 1), -1) self.assertEqual(operator.__rshift__(long(1), 1), long(0)) self.assertEqual(operator.__rshift__(long(2), 1), long(1)) self.assertEqual(operator.__rshift__(-long(1), 1), -long(1)) #__not__ self.assertEqual(operator.__not__(0), 1) self.assertEqual(operator.__not__(1), 0) self.assertEqual(operator.__not__(-1), 0) self.assertEqual(operator.__not__(long(0)), 1) self.assertEqual(operator.__not__(long(1)), 0) self.assertEqual(operator.__not__(-long(1)), 0) #__and__ self.assertEqual(operator.__and__(0, 0), 0) self.assertEqual(operator.__and__(1, 1), 1) self.assertEqual(operator.__and__(0, 1), 0) self.assertEqual(operator.__and__(1, 0), 0) #__xor__ self.assertEqual(operator.__xor__(0, 0), 0) self.assertEqual(operator.__xor__(1, 1), 0) self.assertEqual(operator.__xor__(0, 1), 1) self.assertEqual(operator.__xor__(1, 0), 1) #__or__ self.assertEqual(operator.__or__(0, 0), 0) self.assertEqual(operator.__or__(1, 1), 1) self.assertEqual(operator.__or__(0, 1), 1) self.assertEqual(operator.__or__(1, 0), 1) #__concat__ self.assertEqual(operator.__concat__([0], [1]), [0,1]) self.assertEqual(operator.__concat__([2], [1]), [2,1]) self.assertEqual(operator.__concat__([-1], [1]), [-1,1]) #__contains__ self.assertTrue(operator.__contains__("abc", "c")) self.assertTrue(not operator.__contains__("abc", "d")) self.assertTrue(operator.__contains__("abc", "")) self.assertTrue(not operator.__contains__("", "c")) self.assertTrue(operator.__contains__([1,2,3], 1)) self.assertTrue(not operator.__contains__([1,2,3], 4)) #__getitem__ self.assertEqual(operator.__getitem__("abc", 2), "c") self.assertRaises(IndexError, operator.__getitem__, "abc", 3) self.assertEqual(operator.__getitem__([1,2,3], 2), 3) self.assertRaises(IndexError, operator.__getitem__, [1,2,3], 3) #__setitem__ self.assertRaises(TypeError, operator.__setitem__, "abc", 2, "d") t_list = [1,2,3] operator.__setitem__(t_list, 2, 4) self.assertEqual(t_list, [1,2,4]) self.assertRaises(IndexError, operator.__setitem__, [1,2,3], 4, 9) #__delitem__ #UNIMPLEMENTED #self.assertRaises(TypeError, operator.__delitem__, "abc", 2) t_list = [1,2,3] operator.__delitem__(t_list, 2) self.assertEqual(t_list, [1,2]) self.assertRaises(IndexError, operator.__delitem__, [1,2,3], 4) #__repeat__ self.assertEqual(operator.__repeat__("abc", 2), "abcabc") self.assertEqual(operator.__repeat__("", 2), "") self.assertEqual(operator.__repeat__([1,2,3], 2), [1,2,3,1,2,3]) #__getslice__ self.assertEqual(operator.__getslice__("abc", 1, 2), "b") self.assertEqual(operator.__getslice__("abc", 0, 3), "abc") self.assertEqual(operator.__getslice__("", 0, 0), "") self.assertEqual(operator.__getslice__([1,2,3], 1, 2), [2]) self.assertEqual(operator.__getslice__([1,2,3], 0, 3), [1,2,3]) self.assertEqual(operator.__getslice__([], 0, 0), []) #__delslice__ t_list = [1,2,3] operator.__delslice__(t_list, 1, 2) self.assertEqual(t_list, [1,3]) t_list = [1,2,3] operator.__delslice__(t_list, 0, 3) self.assertEqual(t_list, []) t_list = [1,2,3] operator.__delslice__(t_list, 0, 0) self.assertEqual(t_list, [1,2,3]) #__setslice__ t_list = [1,2,3] operator.__setslice__(t_list, 1, 2, [9]) self.assertEqual(t_list, [1,9,3]) t_list = [1,2,3] operator.__setslice__(t_list, 0, 3, [9, 8]) self.assertEqual(t_list, [9, 8]) t_list = [1,2,3] operator.__setslice__(t_list, 0, 0, [9]) self.assertEqual(t_list, [9,1, 2,3])
def test_sanity(self): """Performs a set of simple sanity checks on most operators.""" #__abs__ self.assertEqual(operator.__abs__(0), 0) self.assertEqual(operator.__abs__(1), 1) self.assertEqual(operator.__abs__(-1), 1) self.assertEqual(operator.__abs__(0.0), 0.0) self.assertEqual(operator.__abs__(1.1), 1.1) self.assertEqual(operator.__abs__(-1.1), 1.1) self.assertEqual(operator.__abs__(big(0)), big(0)) self.assertEqual(operator.__abs__(big(1)), big(1)) self.assertEqual(operator.__abs__(-big(1)), big(1)) #__neg__ self.assertEqual(operator.__neg__(0), 0) self.assertEqual(operator.__neg__(1), -1) self.assertEqual(operator.__neg__(-1), 1) self.assertEqual(operator.__neg__(0.0), 0.0) self.assertEqual(operator.__neg__(1.1), -1.1) self.assertEqual(operator.__neg__(-1.1), 1.1) self.assertEqual(operator.__neg__(big(0)), big(0)) self.assertEqual(operator.__neg__(big(1)), -big(1)) self.assertEqual(operator.__neg__(-big(1)), big(1)) #__pos__ self.assertEqual(operator.__pos__(0), 0) self.assertEqual(operator.__pos__(1), 1) self.assertEqual(operator.__pos__(-1), -1) self.assertEqual(operator.__pos__(0.0), 0.0) self.assertEqual(operator.__pos__(1.1), 1.1) self.assertEqual(operator.__pos__(-1.1), -1.1) self.assertEqual(operator.__pos__(big(0)), big(0)) self.assertEqual(operator.__pos__(big(1)), big(1)) self.assertEqual(operator.__pos__(-big(1)), -big(1)) #__add__ self.assertEqual(operator.__add__(0, 0), 0) self.assertEqual(operator.__add__(1, 2), 3) self.assertEqual(operator.__add__(-1, 2), 1) self.assertEqual(operator.__add__(0.0, 0.0), 0.0) self.assertEqual(operator.__add__(1.1, 2.1), 3.2) self.assertEqual(operator.__add__(-1.1, 2.1), 1.0) self.assertEqual(operator.__add__(big(0), big(0)), big(0)) self.assertEqual(operator.__add__(big(1), big(2)), big(3)) self.assertEqual(operator.__add__(-big(1), big(2)), big(1)) #__sub__ self.assertEqual(operator.__sub__(0, 0), 0) self.assertEqual(operator.__sub__(1, 2), -1) self.assertEqual(operator.__sub__(-1, 2), -3) self.assertEqual(operator.__sub__(0.0, 0.0), 0.0) self.assertEqual(operator.__sub__(1.1, 2.1), -1.0) self.assertEqual(operator.__sub__(-1.1, 2.1), -3.2) self.assertEqual(operator.__sub__(big(0), big(0)), big(0)) self.assertEqual(operator.__sub__(big(1), big(2)), -big(1)) self.assertEqual(operator.__sub__(-big(1), big(2)), -big(3)) #__mul__ self.assertEqual(operator.__mul__(0, 0), 0) self.assertEqual(operator.__mul__(1, 2), 2) self.assertEqual(operator.__mul__(-1, 2), -2) self.assertEqual(operator.__mul__(0.0, 0.0), 0.0) self.assertEqual(operator.__mul__(2.0, 3.0), 6.0) self.assertEqual(operator.__mul__(-2.0, 3.0), -6.0) self.assertEqual(operator.__mul__(big(0), big(0)), big(0)) self.assertEqual(operator.__mul__(big(1), big(2)), big(2)) self.assertEqual(operator.__mul__(-big(1), big(2)), -big(2)) #__div__ self.assertEqual(operator.__div__(0, 1), 0) self.assertEqual(operator.__div__(4, 2), 2) self.assertEqual(operator.__div__(-1, 2), -1) self.assertEqual(operator.__div__(0.0, 1.0), 0.0) self.assertEqual(operator.__div__(4.0, 2.0), 2.0) self.assertEqual(operator.__div__(-4.0, 2.0), -2.0) self.assertEqual(operator.__div__(big(0), big(1)), big(0)) self.assertEqual(operator.__div__(big(4), big(2)), big(2)) self.assertEqual(operator.__div__(-big(4), big(2)), -big(2)) #__floordiv__ self.assertEqual(operator.__floordiv__(0, 1), 0) self.assertEqual(operator.__floordiv__(4, 2), 2) self.assertEqual(operator.__floordiv__(-1, 2), -1) self.assertEqual(operator.__floordiv__(0.0, 1.0), 0.0) self.assertEqual(operator.__floordiv__(4.0, 2.0), 2.0) self.assertEqual(operator.__floordiv__(-4.0, 2.0), -2.0) self.assertEqual(operator.__floordiv__(big(0), big(1)), big(0)) self.assertEqual(operator.__floordiv__(big(4), big(2)), big(2)) self.assertEqual(operator.__floordiv__(-big(4), big(2)), -big(2)) #__truediv__ self.assertEqual(operator.__truediv__(0, 1), 0) self.assertEqual(operator.__truediv__(4, 2), 2) self.assertEqual(operator.__truediv__(-1, 2), -0.5) self.assertEqual(operator.__truediv__(0.0, 1.0), 0.0) self.assertEqual(operator.__truediv__(4.0, 2.0), 2.0) self.assertEqual(operator.__truediv__(-1.0, 2.0), -0.5) self.assertEqual(operator.__truediv__(big(0), big(1)), big(0)) self.assertEqual(operator.__truediv__(big(4), big(2)), big(2)) self.assertEqual(operator.__truediv__(-big(4), big(2)), -big(2)) #__mod__ self.assertEqual(operator.__mod__(0, 1), 0) self.assertEqual(operator.__mod__(4, 2), 0) self.assertEqual(operator.__mod__(-1, 2), 1) self.assertEqual(operator.__mod__(0.0, 1.0), 0.0) self.assertEqual(operator.__mod__(4.0, 2.0), 0.0) self.assertEqual(operator.__mod__(-1.0, 2.0), 1.0) self.assertEqual(operator.__mod__(big(0), big(1)), big(0)) self.assertEqual(operator.__mod__(big(4), big(2)), big(0)) self.assertEqual(operator.__mod__(-big(4), big(2)), big(0)) #__inv__ self.assertEqual(operator.__inv__(0), -1) self.assertEqual(operator.__inv__(1), -2) self.assertEqual(operator.__inv__(-1), 0) self.assertEqual(operator.__inv__(big(0)), -big(1)) self.assertEqual(operator.__inv__(big(1)), -big(2)) self.assertEqual(operator.__inv__(-big(1)), big(0)) #__invert__ self.assertEqual(operator.__invert__(0), -1) self.assertEqual(operator.__invert__(1), -2) self.assertEqual(operator.__invert__(-1), 0) self.assertEqual(operator.__invert__(big(0)), -big(1)) self.assertEqual(operator.__invert__(big(1)), -big(2)) self.assertEqual(operator.__invert__(-big(1)), big(0)) #__lshift__ self.assertEqual(operator.__lshift__(0, 1), 0) self.assertEqual(operator.__lshift__(1, 1), 2) self.assertEqual(operator.__lshift__(-1, 1), -2) self.assertEqual(operator.__lshift__(big(0), 1), big(0)) self.assertEqual(operator.__lshift__(big(1), 1), big(2)) self.assertEqual(operator.__lshift__(-big(1), 1), -big(2)) #__rshift__ self.assertEqual(operator.__rshift__(1, 1), 0) self.assertEqual(operator.__rshift__(2, 1), 1) self.assertEqual(operator.__rshift__(-1, 1), -1) self.assertEqual(operator.__rshift__(big(1), 1), big(0)) self.assertEqual(operator.__rshift__(big(2), 1), big(1)) self.assertEqual(operator.__rshift__(-big(1), 1), -big(1)) #__not__ self.assertEqual(operator.__not__(0), 1) self.assertEqual(operator.__not__(1), 0) self.assertEqual(operator.__not__(-1), 0) self.assertEqual(operator.__not__(big(0)), 1) self.assertEqual(operator.__not__(big(1)), 0) self.assertEqual(operator.__not__(-big(1)), 0) #__and__ self.assertEqual(operator.__and__(0, 0), 0) self.assertEqual(operator.__and__(1, 1), 1) self.assertEqual(operator.__and__(0, 1), 0) self.assertEqual(operator.__and__(1, 0), 0) #__xor__ self.assertEqual(operator.__xor__(0, 0), 0) self.assertEqual(operator.__xor__(1, 1), 0) self.assertEqual(operator.__xor__(0, 1), 1) self.assertEqual(operator.__xor__(1, 0), 1) #__or__ self.assertEqual(operator.__or__(0, 0), 0) self.assertEqual(operator.__or__(1, 1), 1) self.assertEqual(operator.__or__(0, 1), 1) self.assertEqual(operator.__or__(1, 0), 1) #__concat__ self.assertEqual(operator.__concat__([0], [1]), [0, 1]) self.assertEqual(operator.__concat__([2], [1]), [2, 1]) self.assertEqual(operator.__concat__([-1], [1]), [-1, 1]) #__contains__ self.assertTrue(operator.__contains__("abc", "c")) self.assertTrue(not operator.__contains__("abc", "d")) self.assertTrue(operator.__contains__("abc", "")) self.assertTrue(not operator.__contains__("", "c")) self.assertTrue(operator.__contains__([1, 2, 3], 1)) self.assertTrue(not operator.__contains__([1, 2, 3], 4)) #__getitem__ self.assertEqual(operator.__getitem__("abc", 2), "c") self.assertRaises(IndexError, operator.__getitem__, "abc", 3) self.assertEqual(operator.__getitem__([1, 2, 3], 2), 3) self.assertRaises(IndexError, operator.__getitem__, [1, 2, 3], 3) #__setitem__ self.assertRaises(TypeError, operator.__setitem__, "abc", 2, "d") t_list = [1, 2, 3] operator.__setitem__(t_list, 2, 4) self.assertEqual(t_list, [1, 2, 4]) self.assertRaises(IndexError, operator.__setitem__, [1, 2, 3], 4, 9) #__delitem__ #UNIMPLEMENTED #self.assertRaises(TypeError, operator.__delitem__, "abc", 2) t_list = [1, 2, 3] operator.__delitem__(t_list, 2) self.assertEqual(t_list, [1, 2]) self.assertRaises(IndexError, operator.__delitem__, [1, 2, 3], 4) #__repeat__ self.assertEqual(operator.__repeat__("abc", 2), "abcabc") self.assertEqual(operator.__repeat__("", 2), "") self.assertEqual(operator.__repeat__([1, 2, 3], 2), [1, 2, 3, 1, 2, 3]) #__getslice__ self.assertEqual(operator.__getslice__("abc", 1, 2), "b") self.assertEqual(operator.__getslice__("abc", 0, 3), "abc") self.assertEqual(operator.__getslice__("", 0, 0), "") self.assertEqual(operator.__getslice__([1, 2, 3], 1, 2), [2]) self.assertEqual(operator.__getslice__([1, 2, 3], 0, 3), [1, 2, 3]) self.assertEqual(operator.__getslice__([], 0, 0), []) #__delslice__ t_list = [1, 2, 3] operator.__delslice__(t_list, 1, 2) self.assertEqual(t_list, [1, 3]) t_list = [1, 2, 3] operator.__delslice__(t_list, 0, 3) self.assertEqual(t_list, []) t_list = [1, 2, 3] operator.__delslice__(t_list, 0, 0) self.assertEqual(t_list, [1, 2, 3]) #__setslice__ t_list = [1, 2, 3] operator.__setslice__(t_list, 1, 2, [9]) self.assertEqual(t_list, [1, 9, 3]) t_list = [1, 2, 3] operator.__setslice__(t_list, 0, 3, [9, 8]) self.assertEqual(t_list, [9, 8]) t_list = [1, 2, 3] operator.__setslice__(t_list, 0, 0, [9]) self.assertEqual(t_list, [9, 1, 2, 3])
def __rmul__(self, s): return __mul__(self, s)
def __rmul__(self, other): """other * self""" return __mul__(other, get_wrapped_object(self))
def __mul__(self, other): """self * other""" return __mul__(get_wrapped_object(self), get_wrapped_object(other))
def update_event(self, inp=-1): self.set_output_val(0, operator.__mul__(self.input(0), self.input(1)))
def cross_correlation_1d(img, kernel): # 1. check direction of kernel and flatten dir = 0 # dir (direction) is 0 when kernel is horizontal, 1 when vertical. if len(kernel.shape) != 1: # if the shape is not an 1-d ndarray, if kernel.shape[1] == 1: # vertical kernel dir = 1 kernel = kernel.flatten() # flatten to 1-d ndarray size0 = img.shape[0] size1 = img.shape[1] k_size = kernel.shape[0] diff = k_size // 2 # 2. padding original image (img) if dir == 0: padded_size = size1 + k_size - 1 padded_img = np.zeros((size0, padded_size)) j_range0 = range(diff) j_range1 = range(size1) j_range2 = range(size1 + diff, size1 + 2 * diff) for i in range(size0): for j in j_range0: padded_img[i][j] = img[i][0] for j in j_range1: padded_img[i][j + diff] = img[i][j] for j in j_range2: padded_img[i][j] = img[i][size1 - 1] else: # dir == 1 (vertical cases) padded_size = size0 + k_size - 1 padded_img = np.zeros((padded_size, size1)) j_range0 = range(diff) j_range1 = range(size0) j_range2 = range(size0 + diff, size0 + 2 * diff) for j in range(size1): for i in j_range0: padded_img[i][j] = img[0][j] for i in j_range1: padded_img[i + diff][j] = img[i][j] for i in j_range2: padded_img[i][j] = img[size0 - 1][j] #print(padded_img) #print() # 3. apply cross correlation using iteration filtered_img = np.zeros((size0, size1)) for x in range(size0): for y in range(size1): if dir == 0: # horizontal kernel _sum = 0 for j in range(k_size): _sum = operator.__add__( _sum, operator.__mul__(padded_img[x][y + j], kernel[j])) filtered_img[x][y] = _sum elif dir == 1: # vertical kernel _sum = 0 for i in range(k_size): _sum = operator.__add__( _sum, operator.__mul__(padded_img[x + i][y], kernel[i])) filtered_img[x][y] = _sum #print('%.04f'%filtered_img[i][j], end=' ') #print() #print(filtered_img) return filtered_img
def __rmul__(self, other: tp.Any) -> tp.Any: operator = lambda rhs, lhs: operator_mod.__mul__(lhs, rhs) operator.__name__ = 'r' + operator_mod.__mul__.__name__ return self._ufunc_binary_operator(operator=operator, other=other)
# reversed lt = spice(lambda x, y: operator.lt(y, x), name='lt') __lt__ = spice(lambda x, y: operator.__lt__(y, x), name='__lt__') # reversed matmul = spice(lambda x, y: operator.matmul(y, x), name='matmul') __matmul__ = spice(lambda x, y: operator.__matmul__(y, x), name='__matmul__') # reversed mod = spice(lambda x, y: operator.mod(y, x), name='mod') __mod__ = spice(lambda x, y: operator.__mod__(y, x), name='__mod__') mul = spice(lambda x, y: operator.mul(x, y), name='mul', doc=operator.mul.__doc__) __mul__ = spice(lambda x, y: operator.__mul__(x, y), name='__mul__', doc=operator.mul.__doc__) ne = spice(lambda x, y: operator.ne(x, y), name='ne', doc=operator.ne.__doc__) __ne__ = spice(lambda x, y: operator.__ne__(x, y), name='__ne__', doc=operator.ne.__doc__) or_ = spice(lambda x, y: operator.or_(x, y), name='or_', doc=operator.or_.__doc__) __or__ = spice(lambda x, y: operator.__or__(x, y), name='__or__', doc=operator.or_.__doc__)