← Back to team overview

python-quantities-developers team mailing list archive

[Bug 390689] Re: Adding different units raises a ValueError

 

** Branch linked: lp:~dsdale24/python-quantities/mixed-quantities

-- 
Adding different units raises a ValueError
https://bugs.launchpad.net/bugs/390689
You received this bug notification because you are a member of Python
Quantities Development Team, which is subscribed to python-quantities.

Status in Physical Quantities for Python: Confirmed

Bug description:
Adding values of different (but compatible) units raises a ValueError. Below is a simple example to reproduce the error.

>>> import quantities as pq
>>> a = 10 * pq.m
>>> b = 1 * pq.mm
>>> a + b

My guess is that this is a known issue, in which case, I'd suggest that a Known Issues page be added to the website.

I dug through the code and found that the __array_wrap__ method gets called *after* numpy has already added the values together. To properly handle addition, you'd need to alter the values before numpy got its hands on the data. Darren, I guess this is one of the reasons you've been lobbying for __gfunc_pre__ and __gfunc_post__:

http://www.nabble.com/suggestion-for-generalizing-numpy-functions-tt22413628.html#a22413628
http://www.nabble.com/Plans-for-Numpy-1.4.0-and-scipy-0.8.0-tt24132765.html

In any case, I thought it'd be a good idea to document this issue. The obvious work around is to just alter the units of one of the values before addition. In other words.

>>> b.units = pq.m
>>> a + b

Good luck getting __gfunc_pre__ (or __array_prepare__) into numpy 1.4.