BF-MATH - Basic Mathematics

../../../../../../_images/MLPro-BF-Math_class_diagram.drawio.png

Ver. 3.0.1 (2025-07-11)

This module provides basic mathematical classes.

class mlpro.bf.math.basics.Dimension(p_name_short, p_base_set='R', p_name_long='', p_name_latex='', p_unit='', p_unit_latex='', p_boundaries: list = [], p_description='', p_symmetrical: bool = False, p_logging=False, **p_kwargs)

Bases: EventManager, KWArgs

Objects of this type specify properties of a dimension of a set.

Parameters:

p_name_shortstr

Short name of dimension

p_base_set

Base set of dimension. See constants C_BASE_SET_*. Default = C_BASE_SET_R.

p_name_long :str

Long name of dimension (optional)

p_name_latexstr

LaTeX name of dimension (optional)

p_unitstr

Unit (optional)

p_unit_latexstr

LaTeX code of unit (optional)

p_boundarieslist

List with minimum and maximum value (optional)

p_descriptionstr

Description of dimension (optional)

p_symmetricalbool

Information about the symmetry of the dimension (optional, default is False)

p_logging

Log level (see constants of class Log). Default: Log.C_LOG_ALL

p_kwargsdict

Further optional keyword parameters.

C_TYPE = 'Dimension'
C_BASE_SET_R = 'R'
C_BASE_SET_N = 'N'
C_BASE_SET_Z = 'Z'
C_BASE_SET_DO = 'DO'
C_EVENT_BOUNDARIES = 'BOUNDARIES'
get_id()
get_name_short()
get_base_set()
get_name_long()
get_name_latex()
get_unit()
get_unit_latex()
get_boundaries()
set_boundaries(p_boundaries: list)

Sets new boundaries with respect to the symmmetry and raises event C_EVENT_BOUNDARIES.

Parameters:

p_boundaries (list) – New boundaries (lower and upper value)

get_description()
get_symmetrical() bool
get_kwargs() dict

Returns all keyword arguments provided during initialization as a dictionary. Alternatively, public attribute kwargs can be used directly.

copy()
class mlpro.bf.math.basics.Set

Bases: object

Objects of this type describe a (multivariate) set in a mathematical sense.

C_NUMERIC_BASE_SETS = ['N', 'Z', 'R']
add_dim(p_dim: Dimension, p_ignore_duplicates: bool = False)

Raises the dimensionality of the set by adding a new dimension.

Parameters:
  • p_dim (Dimension) – Dimension to be added.

  • p_ignore_duplicates (bool) – If True, duplicated short names of dimensions are accepted. Default = False.

is_numeric() bool

Returns True if the set consists of numeric dimensions only.

get_dim(p_id) Dimension

Returns the dimension specified by it’s unique id.

get_dim_by_name(p_name) Dimension
get_dims() list

” Returns all dimensions.

get_num_dim()

Returns the dimensionality of the set (=number of dimensions of the set).

get_dim_ids()

Returns the unique ids of the related dimensions.

spawn(p_id_list: list)

Spawns a new class with same type and a subset of dimensions specified by an index list.

Parameters:

adopted (p_id_list List of indices of dimensions to be)

Returns:

New object with subset of dimensions

copy(p_new_dim_ids: bool = True)
append(p_set, p_new_dim_ids: bool = True, p_ignore_duplicates: bool = False)
class mlpro.bf.math.basics.DataObject(p_data, *p_meta_data)

Bases: object

Container class for (big) data objects of any type with optional additional meta data.

get_data()
get_meta_data() tuple
class mlpro.bf.math.basics.Element(p_set: Set)

Bases: object

Element of a (multivariate) set.

Parameters:

p_set (Set) – Underlying set.

get_dim_ids() list
get_values() list | ndarray
set_values(p_values: list | ndarray)

Overwrites the values of all components of the element.

Parameters:

dimensions. (p_values Something iterable with same length as number of element)

get_value(p_dim_id)
set_value(p_dim_id, p_value)
copy()
class mlpro.bf.math.basics.ElementList

Bases: object

List of Element objects.

add_elem(p_id, p_elem: Element)

Adds an element object under it’s id in the internal element list.

Parameters:
  • element (p_id Unique id of the)

  • added (p_elem Element object to be)

get_elem_ids() list
get_elem(p_id) Element
class mlpro.bf.math.basics.BatchElement(p_set: Set)

Bases: Element

class mlpro.bf.math.basics.MSpace

Bases: Set

Objects of this type represent a metric space. The method distance implements the metric of the space.

distance(p_e1: Element, p_e2: Element)
class mlpro.bf.math.basics.ESpace

Bases: MSpace

Objects of this type represent an Euclidian space. The distance method implements the Euclidian norm.

distance(p_e1: Element, p_e2: Element)
class mlpro.bf.math.basics.Function(p_input_set: ~mlpro.bf.math.basics.Set = None, p_output_set: ~mlpro.bf.math.basics.Set = None, p_input_space=None, p_output_space=None, p_output_elem_cls: type = <class 'mlpro.bf.math.basics.Element'>, p_autocreate_elements: bool = True, **p_kwargs)

Bases: KWArgs, ScientificObject

Model class for an elementary bi-multivariate mathematical function that maps elements of a multivariate input space to elements of a multivariate output space.

Parameters:
  • p_input_set (Set = None) – Optional input set, needed for the mapping of objects of type Element.

  • p_output_set (Set = None) – Optional output set, needed for the mapping of objects of type Element.

  • p_output_elem_cls (type = Element) – Output element class (compatible to class Element)

  • p_autocreate_elements (bool = True) – If True, elements of the output space are created automatically during mapping of objects of type Element.

  • **p_kwargs – Further optional keyword arguments needed for particular custom implementations.

map(p_input: float | list | ndarray | Element, p_output: float | list | ndarray | Element = None, p_dim: int = None) float | list | ndarray | Element

Maps an input to an output by calling the custom methods _map_[Type]().

Parameters:
  • p_input (Data) – Input to be mapped.

  • p_output (Data = None) – Optional output object as the intended receiver of the mapping result.

  • p_dim (int = None) – An optional dimension index to which the mapping is restricted.

Returns:

Result of the mapping.

Return type:

Data

_map(p_input: Element, p_output: Element = None, p_dim: int = None) Element

Default custom method for own mappings of single objects of type Element.

Parameters:
  • p_input (Element) – Input to be mapped.

  • p_output (Element = None) – Optional output object as the intended receiver of the mapping result.

  • p_dim (int = None) – An optional dimension index to which the mapping is restricted.

Returns:

Result of the mapping.

Return type:

Element

_map_float(p_input: float, p_output: float = None, p_dim: int = None) Element

Custom method for own mappings of single floats.

Parameters:
  • p_input (Element) – Input to be mapped.

  • p_output (Element = None) – Optional output object as the intended receiver of the mapping result.

  • p_dim (int = None) – An optional dimension index to which the mapping is restricted.

Returns:

Result of the mapping.

Return type:

float

_map_list(p_input: list, p_output: list = None, p_dim: int = None) list

Custom method for own mass mappings of lists.

Parameters:
  • p_input (list) – Input to be mapped.

  • p_output (list = None) – Optional output object as the intended receiver of the mapping result.

  • p_dim (int = None) – An optional dimension index to which the mapping is restricted.

Returns:

Result of the mapping.

Return type:

list

_map_ndarray(p_input: ndarray, p_output: ndarray = None, p_dim: int = None) ndarray

Custom method for own mass mappings of Numpy arrays.

Parameters:
  • p_input (list) – Input to be mapped.

  • p_output (list = None) – Optional output object as the intended receiver of the mapping result.

  • p_dim (int = None) – An optional dimension index to which the mapping is restricted.

Returns:

Result of the mapping.

Return type:

np.ndarray

map_inverse(p_input: float | list | ndarray | Element, p_output: float | list | ndarray | Element = None, p_dim: int = None) float | list | ndarray | Element

Inverse mapping of an input to an output by calling the custom methods _map_inverse_[Type]().

Parameters:
  • p_input (Data) – Input to be mapped.

  • p_output (Data = None) – Optional output object as the intended receiver of the mapping result.

  • p_dim (int = None) – An optional dimension index to which the mapping is restricted.

Returns:

Result of the mapping.

Return type:

Data

_map_inverse(p_input: Element, p_output: Element = None, p_dim: int = None) Element

Default custom method for own inverse mappings of single objects of type Element.

Parameters:
  • p_input (Element) – Input to be mapped.

  • p_output (Element = None) – Optional output object as the intended receiver of the mapping result.

  • p_dim (int = None) – An optional dimension index to which the mapping is restricted.

Returns:

Result of the mapping.

Return type:

Element

_map_inverse_float(p_input: float, p_output: float = None, p_dim: int = None) Element

Custom method for own inverse mappings of single floats.

Parameters:
  • p_input (Element) – Input to be mapped.

  • p_output (Element = None) – Optional output object as the intended receiver of the mapping result.

  • p_dim (int = None) – An optional dimension index to which the mapping is restricted.

Returns:

Result of the mapping.

Return type:

float

_map_inverse_list(p_input: list, p_output: list = None, p_dim: int = None) list

Custom method for own mass inverse mappings of lists.

Parameters:
  • p_input (list) – Input to be mapped.

  • p_output (list = None) – Optional output object as the intended receiver of the mapping result.

  • p_dim (int = None) – An optional dimension index to which the mapping is restricted.

Returns:

Result of the mapping.

Return type:

list

_map_inverse_ndarray(p_input: ndarray, p_output: ndarray = None, p_dim: int = None) ndarray

Custom method for own mass inverse mappings of Numpy arrays.

Parameters:
  • p_input (list) – Input to be mapped.

  • p_output (list = None) – Optional output object as the intended receiver of the mapping result.

  • p_dim (int = None) – An optional dimension index to which the mapping is restricted.

Returns:

Result of the mapping.

Return type:

np.ndarray

class mlpro.bf.math.basics.Scaler(p_input_set: ~mlpro.bf.math.basics.Set = None, p_output_set: ~mlpro.bf.math.basics.Set = None, p_input_space=None, p_output_space=None, p_output_elem_cls: type = <class 'mlpro.bf.math.basics.Element'>, p_autocreate_elements: bool = True, **p_kwargs)

Bases: Function

Template for scaler algorithms scaling, unscaling, rescaling data. This class introduces a parameter handling based on the three attributes _param, _param_old, and _param_new. All custom methods inherited from the class Function shall apply the parameters stored in _param, which is a reference to eigther _param_old or _param_new.

Parameters:

details. (See class Function for further)

_param

Internal reference to the active set of function parameters applied to the next scaler action.

_param_old

Previous parameter set.

_param_new

Current parameter set.

scale(p_data: float | list | ndarray | Element, p_dim: int = None, p_param=None) float | list | ndarray | Element

Scales the specified data.

Parameters:
  • p_data (Data) – Data to be scaled.

  • p_dim (int = None) – Optional index of the dimension to be scaled.

  • None (p_param =) – Optional parameter set to be applied to the scaling operation. If None the set stored in self._param_new is used.

Returns:

The scaled data.

Return type:

Data

unscale(p_data: float | list | ndarray | Element, p_dim: int = None, p_param=None) float | list | ndarray | Element

Unscales the specified data.

Parameters:
  • p_data (Data) – Data to be unscaled.

  • p_dim (int = None) – Optional index of the dimension to be unscaled.

  • None (p_param =) – Optional parameter set to be applied to the unscaling operation. If None the set stored in self._param_new is used.

Returns:

The unscaled data.

Return type:

Data

rescale(p_data: float | list | ndarray | Element, p_dim: int = None, p_param_old=None, p_param_new=None) float | list | ndarray | Element

Rescales the specified data by unscaling them with previous parameters stored in _param_old and scaling them with the current parameters in _param_new.

Parameters:
  • p_data (Data) – Data to be rescaled.

  • p_dim (int = None) – Optional index of the dimension to be rescaled.

  • None (p_param_new =) – Optional parameter set to be applied to the unscaling operation. If None the set stored in self._param_old is used.

  • None – Optional parameter set to be applied to the scaling operation. If None the set stored in self._param_new is used.

Returns:

The rescaled data.

Return type:

Data

_set_parameters(p_param)

Private service method to activate the parameter set suitable for the next scaler operation.

Parameters:

p_param – Parameter set to be activated.

update_parameters(**p_kwargs) bool

Method to update the parameters of the scaler. It calls the custom method _update_parameters(), which specifies the actual parameters needed by the particular algorithm.

Parameters:

p_data (Data) – Data needed to update the parameters of the scaler.

Returns:

True, if the parameters were changed. False otherwise.

Return type:

bool

_update_parameters(**p_kwargs) bool

Custom method to update the parameters of the scaler based on data specific to the particular algorithm. Please set the internal attribute p_param_new with new values and backup the previous content in p_param_old before.

Parameters:

p_data (Data) – Data needed to update the parameters of the scaler.

Returns:

True, if the parameters were changed. False otherwise.

Return type:

bool