src.pydasa.serialization.parser#
Module for default global variables and comparison functions for use by all PyDASA and its Data Structs.
Functions#
|
Convert a LaTeX expression to a Python-compatible string. |
|
Pair the variable names in LaTeX format with their Python equivalents. |
|
Construct a mapping between LaTeX symbols and Python symbols. |
|
Compose the Pi-coefficient symbols from expression. |
|
Parse and replace power expressions with placeholders. |
|
Parse numeric constants from expression. |
|
Calulate the result of a dimensional operation. |
|
Assing power operation to the dimensional column. |
|
Confirm dimensionless coefficient expression. |
|
Parse and evaluate the dimensional expression. The function that processes a dimensional expression containing |
|
Arrange the numeric constant for display. |
Module Contents#
- src.pydasa.serialization.parser.latex_to_python(expr)#
Convert a LaTeX expression to a Python-compatible string.
- src.pydasa.serialization.parser.extract_latex_vars(expr)#
Pair the variable names in LaTeX format with their Python equivalents.
- src.pydasa.serialization.parser.create_latex_mapping(expr)#
Construct a mapping between LaTeX symbols and Python symbols.
- Parameters:
expr (str) – The LaTeX expression to parse.
- Returns:
- A tuple containing:
A dictionary mapping LaTeX symbols to Python symbols for internal substitution.
A dictionary mapping Python variable names to their corresponding sympy symbols for lambdify.
A dictionary mapping LaTeX variable names to their Python equivalents.
A dictionary mapping Python variable names to their LaTeX equivalents.
- Return type:
- src.pydasa.serialization.parser.extract_coeff_syms(expr, patt=PI_COEF_RE)#
Compose the Pi-coefficient symbols from expression.
- Parameters:
- Returns:
List of unique coefficient symbols (e.g., [’Pi_{0}’, ‘Pi_{1}’]).
- Return type:
List[str]
Example:
>>> extract_coeff_syms("\Pi_{0} * \Pi_{1}**(-1)") ['\Pi_{0}', '\Pi_{1}']
- src.pydasa.serialization.parser.extract_powered_coeffs(expr, pow_patt=PI_POW_RE)#
Parse and replace power expressions with placeholders.
Identifies patterns like Pi_{0}**(-1) and replaces them with temporary placeholders for easier parsing.
- Parameters:
- Returns:
Modified expression with placeholders
Dictionary mapping placeholders to (coefficient_symbol, power) tuples
- Return type:
- Raises:
ValueError – If power expression is invalid.
Example:
>>> expr = "\Pi_{0}**(-1) * \Pi_{1}**(2)" >>> new_expr, transforms = extract_powered_coeffs(expr) >>> # new_expr: "__PHLR_0__ * __PHLR_1__" >>> # transforms: {"__PHLR_0__": ("\Pi_{0}", -1), "__PHLR_1__": ("\Pi_{1}", 2)}
- src.pydasa.serialization.parser.extract_num_consts(expr)#
Parse numeric constants from expression.
- Parameters:
expr (str) – Mathematical expression containing numeric values.
- Returns:
List of numeric constants found in expression.
- Return type:
List[float]
Example:
>>> extract_num_consts("2 * \Pi_{0} + 0.5") [2.0, 0.5]
- src.pydasa.serialization.parser.compute_dims_opn(dim_col_a, dim_col_b, opn)#
Calulate the result of a dimensional operation.
- Applies dimensional analysis rules:
Multiplication (*): Add exponents
Division (/): Subtract exponents
Addition (+) / Subtraction (-): Result is dimensionless (all zeros)
- Parameters:
- Returns:
Resulting dimensional column.
- Return type:
List[int]
Example:
>>> compute_dims_opn([1, 2, 0], [0, 1, -1], "*") [1, 3, -1] # L^1 * M^2 * L^0 * M^1 * T^-1 = L^1 * M^3 * T^-1
- src.pydasa.serialization.parser.apply_pow_to_dims(dim_col, pwr)#
Assing power operation to the dimensional column.
- Parameters:
- Returns:
Dimensional column with power applied.
- Return type:
List[int]
Example:
>>> apply_pow_to_dims([1, 2, -1], -1) [-1, -2, 1] # (L^1 * M^2 * T^-1)^-1 = L^-1 * M^-2 * T^1
- src.pydasa.serialization.parser.validate_coeff_expr(expr, avail_coeffs, pattern=PI_COEF_RE)#
Confirm dimensionless coefficient expression.
- Checks that:
Expression contains valid coefficient references
All referenced coefficients exist
- Parameters:
- Raises:
ValueError – If expression is invalid or references missing coefficients.
- Return type:
None
- src.pydasa.serialization.parser.parse_dim_expr(expr, coeffs, dim_col_fn, coef_patt=PI_COEF_RE, pow_patt=PI_POW_RE, ops_patt=BASIC_OPS_RE)#
Parse and evaluate the dimensional expression. The function that processes a dimensional expression containing Pi coefficients, operations, and numeric constants.
- Parameters:
expr (str) – Mathematical expression to parse.
coeffs (Dict[str, Any]) – Available coefficients dictionary.
dim_col_fn (Callable[[Any], List[float | int]]) – Function to extract dimension column from coefficient. Signature: dim_col_fn(coef) -> List[float | int]
coef_patt (str) – Regex pattern to match coefficients.
pow_patt (str) – Regex pattern to match power expressions.
ops_patt (str) – Regex pattern to match operations.
- Returns:
Resulting dimensional column
Numeric constant multiplier (None if no constant or constant = 1.0)
- Return type:
- Raises:
ValueError – If expression parsing fails.
Example:
>>> def get_dims(c): return c._dim_col >>> result_dims, constant = parse_dim_expr( ... "2 * \Pi_{0}**(-1) * \Pi_{1}", ... coeffs, ... get_dims ... )
- src.pydasa.serialization.parser.format_numeric_constant(constant)#
Arrange the numeric constant for display.
- Parameters:
constant (Optional[float]) – Numeric constant to format.
- Returns:
Formatted constant string, empty if None or 1.0.
- Return type:
Example:
>>> format_numeric_constant(2.0) '2' >>> format_numeric_constant(0.5) '0.5' >>> format_numeric_constant(None) ''