src.pydasa.serialization.parser#

Module for default global variables and comparison functions for use by all PyDASA and its Data Structs.

Functions#

latex_to_python(expr)

Convert a LaTeX expression to a Python-compatible string.

extract_latex_vars(expr)

Pair the variable names in LaTeX format with their Python equivalents.

create_latex_mapping(expr)

Construct a mapping between LaTeX symbols and Python SymPy's symbols preserving the original format and names.

extract_coeff_syms(expr[, patt])

Compose the Pi-coefficient symbols from expression.

extract_powered_coeffs(expr[, pow_patt])

Parse and replace power expressions with placeholders.

extract_num_consts(expr)

Parse numeric constants from expression.

compute_dims_opn(dim_col_a, dim_col_b, opn)

Calulate the result of a dimensional operation.

apply_pow_to_dims(dim_col, pwr)

Assing power operation to the dimensional column.

validate_coeff_expr(expr, avail_coeffs[, pattern])

Confirm dimensionless coefficient expression.

parse_dim_expr(expr, coeffs, dim_col_fn[, coef_patt, ...])

Parse and evaluate the dimensional expression. The function that processes a dimensional expression containing

format_numeric_constant(constant)

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.

Parameters:

expr (str) – The LaTeX expression to convert.

Returns:

The Python-compatible string.

Return type:

str

src.pydasa.serialization.parser.extract_latex_vars(expr)#

Pair the variable names in LaTeX format with their Python equivalents.

Parameters:

expr (str) – The LaTeX expression to parse.

Returns:

A tuple containing two dictionaries:
  • The first dictionary maps LaTeX variable names to their Python equivalents.

  • The second dictionary maps Python variable names to their LaTeX equivalents.

Return type:

tuple [dict]

src.pydasa.serialization.parser.create_latex_mapping(expr)#

Construct a mapping between LaTeX symbols and Python SymPy’s symbols preserving the original format and names.

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:

tuple[dict]

src.pydasa.serialization.parser.extract_coeff_syms(expr, patt=PI_COEF_RE)#

Compose the Pi-coefficient symbols from expression.

Parameters:
  • expr (str) – Mathematical expression containing Pi coefficients.

  • patt (str) – Regex pattern to match coefficients. Defaults to Pi notation.

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:
  • expr (str) – Mathematical expression with potential power operations.

  • pow_patt (str) – Regex pattern to match power expressions.

Returns:

  • Modified expression with placeholders

  • Dictionary mapping placeholders to (coefficient_symbol, power) tuples

Return type:

Tuple[str, Dict[str, Tuple[str, float]]]

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:
  • dim_col_a (Sequence[int | float]) – First dimensional column.

  • dim_col_b (Sequence[int | float]) – Second dimensional column.

  • opn (str) – Operation to perform (‘*’, ‘/’, ‘+’, ‘-‘).

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:
  • dim_col (Sequence[int | float]) – Dimensional column.

  • pwr (float) – Power to apply.

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:
  1. Expression contains valid coefficient references

  2. All referenced coefficients exist

Parameters:
  • expr (str) – Expression to validate.

  • avail_coeffs (Dict[str, Any]) – Available coefficients.

  • pattern (str) – Regex pattern to match coefficients.

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:

Tuple[List[int], Optional[float]]

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:

str

Example:

>>> format_numeric_constant(2.0)
'2'
>>> format_numeric_constant(0.5)
'0.5'
>>> format_numeric_constant(None)
''