ADR Suggestion - Arithmetic operations for Parameters and DescriptorNumbers #27
Closed
damskii9992
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
General
Adding capability to do arithmetic operations on
Parameters
andDescriptorNumbers
allow users to use fitted parameters to do quick and easy calculations with uncertainties and units to calculate items of interest, as well as quickly and easily create new parameters/descriptors from previous ones.Current implementation
This is not currently implemented
Proposed implementation
Add the following magic methods to
Parameter
andDescriptorNumber
:The output should be a
Parameter
with automatically calculated min/max if any of the elements of the arithmetic operation is a Parameter, otherwise the output should be aDescriptorNumber
. In either case the unit and variance is also automatically propagated.The operations should work between
Parameter
andDescriptorNumber
as well as with floats and integers.For addition/subtraction with scalars, the variance is unchanged, unless the scalar is a
DescriptorNumber
with a variance, in which case the variances are added, and the min/max of Parameters are shifted accordingly.For multiplication/division with scalars, the variance is also multiplied/divided together with the min/max.
Implementation details
To allow the arithmetic operations between
Parameter
andDescriptorNumber
, as well as with scalars, in both directions, i.e.a - b = c
andb - a = d
, the reverse magic methods also needs to be implemented:These methods are called on the 2nd operand when the 1st operand does not know how to do the operation (which the interpreter knows when
NotImplemented
is returned), or when the 2nd operand is a subclass of the 1st operand, such as in the case ofDescriptorNumber
andParameter
.When the unit of the elements of the addition/subtraction operations differ, yet have the same physical dimension, the 2nd element is converted to the unit of the 1st element before the operation, (i.e.
2m + 2cm = 2.02m
and2cm + 2m = 202cm
).This behavior differs from pure Scipp behavior, which does not allow addition/subtraction between different units of the same dimension, but mimics behavior of other unit libraries such as pint. This point is also discussed in Scipp: scipp/scipp#3456
For multiplication/division, the derived units from Scipp with numerical prefactors: (e.g.
2m * 1dm = 2 0.1m^2
) is automatically converted to their base unit without the prefactor, i.e.2m * 1dm = 0.2 m^2
. There is also an open dicussion on this issue on Scipp: scipp/scipp#3500Beta Was this translation helpful? Give feedback.
All reactions