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
-
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 with correctly propagated limits.Implementation
Currently, the following arithmetic operations are implemented for
Parameter
andDescriptorNumber
:The output is a
Parameter
with 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
/max
limits of the resultingParameter
is the sum ofmin
/max
of the two Parameters.When adding
DescriptorNumber
or a float/integer to a Parameter, themin
/max
are increased by the value of theDescriptorNumber
or 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
Parameter
from subtraction between twoParameter
s are handled differently.The resulting
min
is now themin
of the first operand, subtracted by themax
of the second operand, and vice versa for the resultingmax
.Subtraction of -
np.inf
withnp.inf
here yields -np.inf
rather thanNaN
because these are numerical limits.Multiplication
For multiplication of
Parameter
s the resultingmin
/max
are the lowest/highest of the 4 combinations that themin
/max
of the operands can be multiplied together.Here, multiplication of ±
np.inf
with 0 yields 0, because these are numerical limits.Multiplication of
Parameter
s with otherParameter
s orDescriptorNumber
s also multiplies their units together and simplify the resulting unit.Special case
In the instance a
Parameter
is multiplied by a0
or aDescriptorNumber
with a value of 0, the returned result is aDescriptorNumber
with a value of 0, rather than aParameter
, because the result is 0 no matter which value theParameter
takes within itsmin
/max
interval.This is not the case when multiplied by another
Parameter
with a value of 0, because thatParameter
can itself take values other than 0.Division
For the division of two
Parameter
s, the resultingmin
/max
depends mostly on the denominator. If the denominatorParameter
smin
/max
crosses 0, the denominator can be arbitrarily close to 0 from either side, and the resultingParameter
thus has limits of ±np.inf
.If the denominator has 0 as either its
min
ormax
, it can be arbitrarily close to 0 from only one side, and thus the limits of the resultingParameter
can either both be ±np.inf
, (if the numeratorParameter
can be both positive and negative), or one side can be ±np.inf
and 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
/max
es of theParameter
sDivision by a float/integer or a
DescriptorNumber
/Parameter
with a value of 0 raises a ZeroDivisionError, instead of yieldingnp.inf
, in order to avoidNaN
s in a fitting procedure.Special case
If a float/integer or a
DescriptorNumber
with a value of 0 is divided by aParameter
, the result is aDescriptorNumber
with a value of 0, like in the multiplication operation, since the result does not depend on which value theParameter
takes within its limits.Exponentiation
Parameter
s can only be raised to powers of floats/integers orDescriptorNumber
s 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
/DescriptorNumber
has a unit, it can only be raised to integer powers, as non-integer power units are not well-defined.Fractional powers of negative-valued
Parameter
s/DescriptorNumber
s raise a ValueError, since complex numbers are not supported and the result is otherwise aNaN
value, which is to be avoided.For the same reason, if the
min
of aParameter
raised to a factional power is negative, the resultingmin
is0
, as0
is the smallest value for which the operation was allowed.If a
Parameter
is raised to the power of0
, the returned result is aDescriptorNumber
, since the result does not depend on which value theParameter
takes within its limits.If the exponent is a multiple of 2 and the
Parameter
s limits crosses 0, the resultingmin
value is set to 0 and themax
value is the largest of the absolute value ofmin
andmax
, since exponents of multiples of 2 are always positive.Negation
When a
Parameter
is negated, the resultingmin
becomes minus themax
and vice versa for themax
Absolute Value
If the limits of the
Parameter
crosses 0, the resultingmin
becomes 0 and themax
is the larger of the absolute values of themin
andmax
.link to the ADR suggestion
#27
Beta Was this translation helpful? Give feedback.
All reactions