Arithmetic operations for Parameters and DescriptorNumbers #54
Locked
damskii9992
announced in
ADRs
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
ParametersandDescriptorNumbersallow 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 with correctly propagated limits.Implementation
Currently, the following arithmetic operations are implemented for
ParameterandDescriptorNumber:The output is a
Parameterwith automatically calculated min/max if any of the elements of the arithmetic operation is a Parameter, except for in specific cases explained below, otherwise the output is aDescriptorNumber. In either case, the unit and variance are also automatically propagated.The operations work between any of
Parameter,DescriptorNumber, floats or integers, except for exponentiation which does not take parameters as exponents.Details for specific operations
Addition
The
min/maxlimits of the resultingParameteris the sum ofmin/maxof the two Parameters.When adding
DescriptorNumberor a float/integer to a Parameter, themin/maxare increased by the value of theDescriptorNumberor the float/integer instead.Floats/integers can only be added to a dimensionless
Parameter/DescriptorNumber, and these can only be added together if their units have the same physical dimension.In case the units of the operands are different yet have the same physical dimension, the 2nd operand is converted to the unit of the 1st operand, in line with other libraries such as Pint.
Subtraction
Subtraction works similarly to addition, with the exception that the limits of the resulting
Parameterfrom subtraction between twoParameters are handled differently.The resulting
minis now theminof the first operand, subtracted by themaxof the second operand, and vice versa for the resultingmax.Subtraction of -
np.infwithnp.infhere yields -np.infrather thanNaNbecause these are numerical limits.Multiplication
For multiplication of
Parameters the resultingmin/maxare the lowest/highest of the 4 combinations that themin/maxof the operands can be multiplied together.Here, multiplication of ±
np.infwith 0 yields 0, because these are numerical limits.Multiplication of
Parameters with otherParameters orDescriptorNumbers also multiplies their units together and simplify the resulting unit.Special case
In the instance a
Parameteris multiplied by a0or aDescriptorNumberwith a value of 0, the returned result is aDescriptorNumberwith a value of 0, rather than aParameter, because the result is 0 no matter which value theParametertakes within itsmin/maxinterval.This is not the case when multiplied by another
Parameterwith a value of 0, because thatParametercan itself take values other than 0.Division
For the division of two
Parameters, the resultingmin/maxdepends mostly on the denominator. If the denominatorParametersmin/maxcrosses 0, the denominator can be arbitrarily close to 0 from either side, and the resultingParameterthus has limits of ±np.inf.If the denominator has 0 as either its
minormax, it can be arbitrarily close to 0 from only one side, and thus the limits of the resultingParametercan either both be ±np.inf, (if the numeratorParametercan be both positive and negative), or one side can be ±np.infand the other side finite.If the denominator has finite limits not including 0, the resulting limits are determined like in the multiplication operation, by the min and max of the 4 possible combinations of divisions between
min/maxes of theParametersDivision by a float/integer or a
DescriptorNumber/Parameterwith a value of 0 raises a ZeroDivisionError, instead of yieldingnp.inf, in order to avoidNaNs in a fitting procedure.Special case
If a float/integer or a
DescriptorNumberwith a value of 0 is divided by aParameter, the result is aDescriptorNumberwith a value of 0, like in the multiplication operation, since the result does not depend on which value theParametertakes within its limits.Exponentiation
Parameters can only be raised to powers of floats/integers orDescriptorNumbers without a unit or a variance, since exponents have to be unit-less and it is not well-defined how to propagate a variance in the exponent.If the
Parameter/DescriptorNumberhas a unit, it can only be raised to integer powers, as non-integer power units are not well-defined.Fractional powers of negative-valued
Parameters/DescriptorNumbers raise a ValueError, since complex numbers are not supported and the result is otherwise aNaNvalue, which is to be avoided.For the same reason, if the
minof aParameterraised to a factional power is negative, the resultingminis0, as0is the smallest value for which the operation was allowed.If a
Parameteris raised to the power of0, the returned result is aDescriptorNumber, since the result does not depend on which value theParametertakes within its limits.If the exponent is a multiple of 2 and the
Parameters limits crosses 0, the resultingminvalue is set to 0 and themaxvalue is the largest of the absolute value ofminandmax, since exponents of multiples of 2 are always positive.Negation
When a
Parameteris negated, the resultingminbecomes minus themaxand vice versa for themaxAbsolute Value
If the limits of the
Parametercrosses 0, the resultingminbecomes 0 and themaxis the larger of the absolute values of theminandmax.link to the ADR suggestion
#27
Beta Was this translation helpful? Give feedback.
All reactions