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

Uncertainties + pint #86

Open
SengerM opened this issue Oct 5, 2018 · 5 comments
Open

Uncertainties + pint #86

SengerM opened this issue Oct 5, 2018 · 5 comments

Comments

@SengerM
Copy link

SengerM commented Oct 5, 2018

I am trying to combine uncertainties and pint to avoid tedious calculations. When operations consist only in +,-,* and / everything works perfectly. The problem arises when I try to use other functions of the uncertainties package such as cosine(), sqrt(), etc. A MWE of what I am trying to do is as follows

import uncertainties as un
from uncertainties.umath import *
import pint
ureg = pint.UnitRegistry()
a = un.ufloat(10,1)*ureg.meter
b = un.ufloat(2,0.1)*ureg.meter
print(a/b) # This works nice
print(sqrt(un.ufloat(3,1))) # This works nice
print(sqrt(a/b)) # This fails

As far as I know the problem is that sqrt() is waiting for a ufloat object and I am passing a "pint type object". Is there any way to implement this?

@lebigot
Copy link
Collaborator

lebigot commented Oct 6, 2018

Indeed, Pint objects should be handled by Pint instead of the uncertainties module: units have to be handled somewhere.

Now, how would you do the same if your Pint quantities didn't have uncertainties?

@SengerM
Copy link
Author

SengerM commented Oct 6, 2018

For Pint quantities without uncertainties it can be done using Numpy:

import pint
import numpy
ureg = pint.UnitRegistry()
a = 10*ureg.meter
b = 20*ureg.meter
print(numpy.sqrt(a/b))

@lebigot
Copy link
Collaborator

lebigot commented Oct 6, 2018

And so what happens if you do the same with uncertainties?

@SengerM
Copy link
Author

SengerM commented Oct 6, 2018

This

import pint
import numpy
import uncertainties as un
import uncertainties.unumpy as unp
ureg = pint.UnitRegistry()
a = un.ufloat(10,1)*ureg.meter
b = un.ufloat(20,1)*ureg.meter
print(a)
print(numpy.sqrt(a/b))

gives

10.0+/-1.0 meter
Traceback (most recent call last):
  File "test.py", line 9, in <module>
	print(numpy.sqrt(a/b))
AttributeError: 'AffineScalarFunc' object has no attribute 'sqrt'

@lebigot
Copy link
Collaborator

lebigot commented Oct 7, 2018

Thanks. I'll investigate some more the interaction between NumPy and non-basic types, then. My guess is that uncertainties could interface more with NumPy (in addition to what is already available in uncertainties.unumpy).

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

No branches or pull requests

2 participants