duq package
Subpackages
Submodules
duq.dimension module
duq.helpers module
Helper functions used in other modules.
- duq.helpers.generate_symbol_for_base_exp_series(base_array: ndarray, exp_array: ndarray, seperator: str = '') str
Take two arrays representing the bases and exponents of an expression, and create a string representation of the expression where the bases with an exponent of 0 are ignored, bases with an exponent of 1 are written without the exponent, and all other exponents are superscripted, and each base-exponent pair is separated from the others by a given separator.
- Parameters:
base_array (numpy.ndarray) – Array representing the bases.
exp_array (numpy.ndarray) – Array representing the exponents
seperator (str) – Separator to be printed in between base-exponent pairs.
- Returns:
str
String representation of the expression.
- duq.helpers.order_for_repr(arrays: Union[Sequence[Union[Sequence, ndarray]], ndarray], cut_idx: int) list
Re-order each sub-array (not in-place) in an array of arrays. Each subarray is reordered based on priority of dimensions/units for generating name and symbol representations. The priority is set as follows:
Derived dimensions/units before primary dimensions/units
More complex derived dimensions/units before simpler ones
Primary dimensions/units in the conventional order
All arrays containing names, symbols etc. are already ordered in a way that the primary dimensions/units are first (in the conventional order), followed by derived dimensions/units, going from simple ones to more complex ones. Thus, for reordering, only the index of the first derived dimension/unit is needed.
- Parameters:
arrays (Union[Sequence[Union[Sequence, np.ndarray]], np.ndarray]) – An array containing the arrays to be ordered.
cut_idx (int) – Index used to slice the array into two. This should be the index of the first derived dimension/unit in the array.
- Returns:
ordered_arrays – List of ordered arrays.
- Return type:
list
Examples
([[a,b,c,d,e,f]], 3) -> [[f,e,d,a,b,c]]
- duq.helpers.parse_base_with_exp_string(string: str) Tuple[ndarray, ndarray]
Parse a string representation of a collection of bases and exponents, seperated from each other by a ‘.’ symbol. Each base may be followed by a ^ symbol, separating it from its exponent. The exponent should either be an integer, or a fraction where the nominator and denominator are separated by a / symbol.
- Parameters:
string (str) – The string representation of the expression.
- Return type:
tuple[numpy.ndarray, numpy.ndarray]
Examples
“kg.m^2.s^-2” -> ([“kg”, “m”, “s”], [1, 2, -2]) “m^3/2” -> ([“m”], [1.5])
- duq.helpers.pretty_print_base_with_exp_series(base_array: Sequence, exp_array: Sequence, seperator: str = '') str
Create a string representation of a collection of bases and their corresponding exponents, where exponents are in superscript and the terms may be separated by a seperator character.
- Parameters:
base_array (Sequence) – Collection of bases.
exp_array (Sequence) – Collection of exponents
seperator (str) – A character or a string to separate base-exponent pairs from each other.
- Returns:
str
The string representation of the expression.
Examples
(base_array=[“L”, “M”, “T”], exp_array=[1, 2, -2]) -> “LM²T⁻²” (base_array=[“length”, “mass”, “time”], exp_array=[1, 2, -2], seperator=”.”) -> “length.mass².time⁻²”
- duq.helpers.raise_for_type(obj: object, obj_type: ~typing.Union[~typing.Type, ~typing.Tuple[~typing.Type]], msg: str, error_type: type = <class 'NotImplementedError'>) None
Check an object’s type and raise a specific error with a given message if the type does not match with the given expected type.
- Parameters:
obj (Object) – The object whose type is to be checked.
obj_type (Type) – The expected type of the object.
msg (str) – Error message to be shown when the type does not match.
error_type (Exception (optional; default: NotImplementedError)) – Type of error to be raised.
- Return type:
None
- duq.helpers.superscript_map_func(exp: Union[int, float]) str
Turn a number into its superscript string form.