Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

np.add(1*pq.kg, 1*pq.g) does not work #163

Open
twmr opened this issue Sep 10, 2019 · 2 comments
Open

np.add(1*pq.kg, 1*pq.g) does not work #163

twmr opened this issue Sep 10, 2019 · 2 comments

Comments

@twmr
Copy link
Contributor

twmr commented Sep 10, 2019

In [1]: import quantities as pq                                                                                                                              

In [2]: import numpy as np                                                                                                                                   

In [3]: np.add(1*pq.kg, 1*pq.g)                                                                                                                              
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
/opt/miniconda/envs/py3/lib/python3.7/site-packages/quantities/dimensionality.py in _d_check_uniform(q1, q2, out)
    256     try:
--> 257         assert q1._dimensionality == q2._dimensionality
    258         return q1.dimensionality

AssertionError: 

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-3-91c844f67315> in <module>
----> 1 np.add(1*pq.kg, 1*pq.g)

/opt/miniconda/envs/py3/lib/python3.7/site-packages/quantities/quantity.py in __array_prepare__(self, obj, context)
    245         #print self, obj, res, uf, objs
    246         try:
--> 247             res._dimensionality = p_dict[uf](*objs)
    248         except KeyError:
    249             raise ValueError(

/opt/miniconda/envs/py3/lib/python3.7/site-packages/quantities/dimensionality.py in _d_check_uniform(q1, q2, out)
    260         raise ValueError(
    261             'quantities must have identical units, got "%s" and "%s"' %
--> 262             (q1.units, q2.units)
    263         )
    264     except AttributeError:

Other quantity libraries like pint, astropy.units and unyt don't raise an exception when np.add is called with quantities with different (rescalable) units.

BTW quantities has the same problem when np.sub or np.equal is used.

@twmr
Copy link
Contributor Author

twmr commented Oct 28, 2019

The reason why I noticed this regression is that the benchmark tool, that is part of unyt uses np.add for tests. See https://github.com/yt-project/unyt/tree/master/benchmarks

@apdavison
Copy link
Contributor

I've briefly looked into this, it looks as though the __array_ufunc__ method (introduced in NumPy 1.13) would be a good approach for doing the necessary scaling, but I haven't had time to try this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants