np.add(1*pq.kg, 1*pq.g) does not work · Issue #163 · python-quantities/python-quantities · GitHub
Skip to content

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

Open

Description

@twmr
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.

Activity

  1. twmr commented on Oct 28, 2019

    @twmr
    ContributorAuthor

    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

  2. apdavison commented on Nov 28, 2019

    @apdavison
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions