math_utils
The endf_parserpy.utils.math_utils module implements
functions for basic mathematical operations on scalars
and iterable objects (e.g. list). Operations
applied to two iterable objects are performed element-wise.
Operations between a scalar and an iterable object are also
performed element-wise.
The EndfFloat class
stores a float along with its string representation.
The following facilities are provided by the math_utils module:
- class endf_parserpy.utils.math_utils.EndfFloat(value, orig_str)[source]
float that keeps track of string representation.
Instances of this class represent
floatnumbers. Additionally, each instance stores a string that should be the source string from which the float value was obtained.Numeric comparisons between
EndfFloatinstances and data types convertible to float numbers via thefloat()function are possible. However, usingEndfFloatinstances directly in arithmetic expressions is not possible. They must be explicitly converted to an integer or float number before viafloat()orint()function, respectively.Creation of EndfFloat instance.
- Parameters:
value (object) – Any object that can be converted to a float via
float(value).orig_str – A string representation that corresponds to the float number given as
valueargument.
- endf_parserpy.utils.math_utils.math_isclose(x, y, rtol=1e-05, atol=1e-08)[source]
Checks whether two numbers are close.
Numbers represented as
EndfFloatare also supported.
- endf_parserpy.utils.math_utils.math_op(x, y, op, **kwargs)[source]
Performs a binary operation.
- Parameters:
x (Union[float, Iterable[float]]) – First number or iterable of numbers
y (Union[float, Iterable[float]]) – Second number or iterable of numbers
op (Callable[[float, float, **kwargs], Any]) – Function (e.g.
lambdafunction) that takes two floats and, optionally, keyword arguments.kwargs (Optional[dict[str, int]]) – Keyword arguments also passed to function provided as
opargument.
- Returns:
If either of the input arguments
xoryis an iterable, returns an iterable with results of the the element-wise application of the function. If bothxandyare scalar/non-iterable, the result of the function.- Return type:
Union[Any, Iterable[Any]]
- endf_parserpy.utils.math_utils.math_div(x, y, cast_int=False)[source]
Divide value(s) by other value(s)
- Parameters:
x (Union[float, Iterable[float]]) – First number or iterable of numbers
y (Union[float, Iterable[float]]) – Second number or iterable of numbers
cast_int (bool) – If both
xandyareint, and result does not correspond to integer, an exception is raised ifcast_int=True, otherwise the result returned as afloat.
- Returns:
Result of division
- Return type:
- endf_parserpy.utils.math_utils.math_mod(x, y, cast_int=False)[source]
Modulo of values
- Parameters:
x (Union[float, Iterable[float]]) – First number or iterable of numbers
y (Union[float, Iterable[float]]) – Second number or iterable of numbers
cast_int (bool) – If both
xandyareint, and result does not correspond to integer, an exception is raised ifcast_int=True, otherwise the result returned as afloat.
- Returns:
Result of modulo operation
- Return type: