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

In place numpy array operations causing issues with dimensionality of quantities #1938

Open
domyam97 opened this issue Feb 16, 2024 · 1 comment

Comments

@domyam97
Copy link

In the PlainQuantity class the method dimensionality() only updates the dimensionality of the object when it is previously None. This is causing issues when working with numpy ndarray quantitites and in-place operations.

For example:

import numpy as np
from pint import UnitRegistry

ureg = UnitRegistry()
quantity = ureg.Quantity

# create a numpy array based quantity
x = quantity(np.array([[20.0]],"m")
print(x.dimensionality) # returns {'[length]':1.0}

# divide by 1m to get a dimenstionless quantity using in-place operation
x /= quantity(1,"m")
print(x.dimensionality) # still returns {'[length]':1.0}

Becuase the array was modified in place, a new object was not created and therefore the dimensionality was not reset to None. Subsequent calls to dimensionality do not reflect the changes caused by the division.

@domyam97
Copy link
Author

domyam97 commented Feb 16, 2024

Is there a reason the check that the _dimensionality parameter is None exists? It seems unnecessary and causes issues like this.

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

1 participant