@@ -47,12 +47,13 @@ def get_conversion_factor(from_u, to_u):
4747 assert from_u .dimensionality == to_u .dimensionality
4848 return from_u .magnitude / to_u .magnitude
4949
50- def protected_addition (f ):
50+ def scale_other_units (f ):
5151 @wraps (f )
5252 def g (self , other , * args ):
5353 if not isinstance (other , Quantity ):
5454 other = Quantity (other , copy = False )
55- getattr (self ._dimensionality , f .__name__ )(other ._dimensionality )
55+ if other ._dimensionality != self ._dimensionality :
56+ other = other .rescale (self .units )
5657 return f (self , other , * args )
5758 return g
5859
@@ -224,13 +225,33 @@ def __array_wrap__(self, obj, context=None):
224225 print 'https://bugs.launchpad.net/python-quantities'
225226 return result
226227
228+ @with_doc (np .ndarray .__add__ )
229+ @scale_other_units
230+ def __add__ (self , other ):
231+ return super (Quantity , self ).__add__ (other )
232+
233+ @with_doc (np .ndarray .__radd__ )
234+ @scale_other_units
235+ def __radd__ (self , other ):
236+ return super (Quantity , self ).__radd__ (other )
237+
227238 @with_doc (np .ndarray .__iadd__ )
228- @protected_addition
239+ @scale_other_units
229240 def __iadd__ (self , other ):
230241 return super (Quantity , self ).__iadd__ (other )
231242
243+ @with_doc (np .ndarray .__sub__ )
244+ @scale_other_units
245+ def __sub__ (self , other ):
246+ return super (Quantity , self ).__sub__ (other )
247+
248+ @with_doc (np .ndarray .__rsub__ )
249+ @scale_other_units
250+ def __rsub__ (self , other ):
251+ return super (Quantity , self ).__rsub__ (other )
252+
232253 @with_doc (np .ndarray .__isub__ )
233- @protected_addition
254+ @scale_other_units
234255 def __isub__ (self , other ):
235256 return super (Quantity , self ).__isub__ (other )
236257
0 commit comments