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 float numbers. Additionally, each instance stores a string that should be the source string from which the float value was obtained.

Numeric comparisons between EndfFloat instances and data types convertible to float numbers via the float() function are possible. However, using EndfFloat instances directly in arithmetic expressions is not possible. They must be explicitly converted to an integer or float number before via float() or int() 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 value argument.

get_original_string()[source]

Return the string representation of the float value.

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 EndfFloat are also supported.

Parameters:
  • x (Union[float, EndfFloat]) – First number

  • y (Union[float, EndfFloat]) – Second number

  • rtol (float) – Relative tolerance for comparison

  • atol (float) – Absolute tolerance for comparison

Returns:

True if numbers are close, False otherwise.

Return type:

bool

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. lambda function) that takes two floats and, optionally, keyword arguments.

  • kwargs (Optional[dict[str, int]]) – Keyword arguments also passed to function provided as op argument.

Returns:

If either of the input arguments x or y is an iterable, returns an iterable with results of the the element-wise application of the function. If both x and y are scalar/non-iterable, the result of the function.

Return type:

Union[Any, Iterable[Any]]

endf_parserpy.utils.math_utils.math_mul(x, y)[source]

Multiply 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

Returns:

Multiplication result

Return type:

Union[float, Iterable[float]]

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 x and y are int, and result does not correspond to integer, an exception is raised if cast_int=True, otherwise the result returned as a float.

Returns:

Result of division

Return type:

Union[float, Iterable[float]]

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 x and y are int, and result does not correspond to integer, an exception is raised if cast_int=True, otherwise the result returned as a float.

Returns:

Result of modulo operation

Return type:

Union[float, Iterable[float]]

endf_parserpy.utils.math_utils.math_add(x, y)[source]

Add values

Parameters:
  • x (Union[float, Iterable[float]]) – First number or iterable of numbers

  • y (Union[float, Iterable[float]]) – Second number or iterable of numbers

Returns:

Result of addition

Return type:

Union[float, Iterable[float]]

endf_parserpy.utils.math_utils.math_sub(x, y)[source]

Subtract values

Parameters:
  • x (Union[float, Iterable[float]]) – First number or iterable of numbers

  • y (Union[float, Iterable[float]]) – Second number or iterable of numbers

Returns:

Result of subtraction

Return type:

Union[float, Iterable[float]]

endf_parserpy.utils.math_utils.math_allclose(x, y, rtol=1e-05, atol=1e-08)[source]

Check whether all values close.

Parameters:
  • x (Union[float, Iterable[float]]) – First number or iterable of numbers

  • y (Union[float, Iterable[float]]) – Second number or iterable of numbers

  • rtol (float) – Relative tolerance for comparison

  • atol (float) – Absolute tolerance for comparison

Returns:

True if value(s) in x close to value(s) in y, False otherwise

Return type:

bool