andreacortis / SemanticQuantities

MIT License
1 stars 0 forks source link

equality between quantities with uncertainties #4

Open andreacortis opened 3 years ago

andreacortis commented 3 years ago

So, to comment on the code review by Matt (@mlr07), what does it mean for two quantities to be equal?

Clearly, if the quantities do not have any uncertainties to them, then we can compare magnitudes up to tolerance. From the help function of numpy allclose

Signature: np.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)
Docstring:
Returns True if two arrays are element-wise equal within a tolerance.

The tolerance values are positive, typically very small numbers.  The
relative difference (`rtol` * abs(`b`)) and the absolute difference
`atol` are added together to compare against the absolute difference
between `a` and `b`.
[...]

But what if the quantities have some uncertainties to them? say we three define mass quantities

m1 = Mass(name='m', magnitude=ufloat(6.00, 0.1), units='kg')
m2 = Mass(name='m', magnitude=ufloat(6.01, 0.1), units='kg')
m3 = Mass(name='m', magnitude=ufloat(6.05, 0.01), units='kg')

can we assert that m1==m2 ? Or can we assert that m2==m3 because the distribution for m3 is contained within the distribution for m2?

In other words, we would need to agree on equality for quantities, (maybe there is a definition in literature?) and only then overload the __eq__ method. This is why I commented out that line as use sparingly!!

rckwzrd commented 3 years ago

this makes a lot more sense.

how would you go about overloading __eq__? overwrite the method in Base

i have not seen anything like this in the literature (i might be reading the wrong textbooks). could a check be implemented to first compare the uncertainties between two quantities... and then proceed with the assertion for quantity equality only if the uncertainties are equivalent?

something like (look before you leap):

if mass_a.uncertainty == mass_b.uncertainty:
    if mass_a.quantity == mass_b.quantity:
       print("equal")
    else:
       print("not equal")
else:
   print("uncertainties don't align... equality not possible")
andreacortis commented 3 years ago

let's keep on looking for definitions in literature. Maybe we need to think if the eq as overloading an approximate equality?