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

unintuitive behaviour interacting with numpy scalars #900

Closed
rlhycd opened this issue Nov 13, 2019 · 5 comments
Closed

unintuitive behaviour interacting with numpy scalars #900

rlhycd opened this issue Nov 13, 2019 · 5 comments
Labels
numpy Numpy related bug/enhancement

Comments

@rlhycd
Copy link

rlhycd commented Nov 13, 2019

import pint, numpy
si = pint.UnitRegistry()

a = numpy.ones(1)
b = 10. * si.gram / si.kilogram
print(a[0]+b, "vs.", float(a[0])+b)
11.0 gram / kilogram vs. 1.01 dimensionless
@jthielen
Copy link
Contributor

I think this is a bigger issue in general with NumPy's deferring to pint for operator precedence not working properly. The following code (with arrays) demonstrates the same issue:

import pint, numpy
si = pint.UnitRegistry()

a = numpy.array([0, 1, 2])
b = 10. * si.gram / si.kilogram
print(a + b, "vs.", b + a)
[10.0 11.0 12.0] gram / kilogram vs. [0.01 1.01 2.01] dimensionless

It looks like some more subtle debugging of why the bare NumPy array/scalar is not treated as dimensionless in the first example is need.

@Tigraan
Copy link

Tigraan commented Nov 20, 2019

I guess "unintuitive behaviour" is the British way to say "buggy"...

Even more generally, the problem seems to be that (numpy array) + (pint array) is a valid operation regardless of the dimensions which is surely a bug:

import pint, numpy
si = pint.UnitRegistry()

numpy.asarray([1.0, 2.0]) + 10.* si.gram  # [11.0, 12.0] gram
1.0 + 10.0 * si.gram  # pint.errors.DimensionalityError: Cannot convert from 'gram' to 'dimensionless'

One could argue that the first behaviour (add to the magnitude) is proper (I do not think so), but surely one cannot argue that floats and ndarrays should behave differently.

Looking at the definition in pint (

def _add_sub(self, other, op):
) the addition operation seems kosher. My best guess is that numpy accepts operations of the form (array) + (some other object) with restrictions lax enough that pint arrays qualify, but I have not checked.

jthielen added a commit to jthielen/pint that referenced this issue Dec 2, 2019
@jthielen
Copy link
Contributor

This should be resolved with #905 (see test added below):

def test_addition_with_scalar(self):
a = np.array([0, 1, 2])
b = 10. * self.ureg('gram/kilogram')
self.assertQuantityAlmostEqual(a + b, self.Q_([0.01, 1.01, 2.01], self.ureg.dimensionless))
self.assertQuantityAlmostEqual(b + a, self.Q_([0.01, 1.01, 2.01], self.ureg.dimensionless))

@Michaeldz36 and @Tigraan would you be able to confirm if this is resolved for you on the current master branch?

@hgrecco hgrecco added the numpy Numpy related bug/enhancement label Dec 11, 2019
@jthielen
Copy link
Contributor

Unless there are complaints, I think this can be closed now @hgrecco? There was a test added in #905 for this issue (see above comment).

@hgrecco
Copy link
Owner

hgrecco commented Dec 30, 2019

Closing. @Michaeldz36 and @Tigraan , feel free to write back if needed.

@hgrecco hgrecco closed this as completed Dec 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
numpy Numpy related bug/enhancement
Projects
None yet
Development

No branches or pull requests

4 participants