src.pydasa.serialization.parser =============================== .. py:module:: src.pydasa.serialization.parser .. autoapi-nested-parse:: Module latex.py =========================================== Module for default global variables and comparison functions for use by all **PyDASA** and its Data Structs. Functions --------- .. autoapisummary:: src.pydasa.serialization.parser.latex_to_python src.pydasa.serialization.parser.extract_latex_vars src.pydasa.serialization.parser.create_latex_mapping src.pydasa.serialization.parser.extract_coeff_syms src.pydasa.serialization.parser.extract_powered_coeffs src.pydasa.serialization.parser.extract_num_consts src.pydasa.serialization.parser.compute_dims_opn src.pydasa.serialization.parser.apply_pow_to_dims src.pydasa.serialization.parser.validate_coeff_expr src.pydasa.serialization.parser.parse_dim_expr src.pydasa.serialization.parser.format_numeric_constant Module Contents --------------- .. py:function:: latex_to_python(expr) Convert a LaTeX expression to a Python-compatible string. :param expr: The LaTeX expression to convert. :type expr: str :returns: The Python-compatible string. :rtype: str .. py:function:: extract_latex_vars(expr) Pair the variable names in LaTeX format with their Python equivalents. :param expr: The LaTeX expression to parse. :type expr: str :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. :rtype: tuple [dict] .. py:function:: create_latex_mapping(expr) Construct a mapping between LaTeX symbols and Python SymPy's symbols preserving the original format and names. :param expr: The LaTeX expression to parse. :type expr: str :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. :rtype: tuple[dict] .. py:function:: extract_coeff_syms(expr, patt = PI_COEF_RE) Compose the Pi-coefficient symbols from expression. :param expr: Mathematical expression containing Pi coefficients. :type expr: str :param patt: Regex pattern to match coefficients. Defaults to Pi notation. :type patt: str :returns: List of unique coefficient symbols (e.g., ['\Pi_{0}', '\Pi_{1}']). :rtype: List[str] Example:: >>> extract_coeff_syms("\Pi_{0} * \Pi_{1}**(-1)") ['\Pi_{0}', '\Pi_{1}'] .. py:function:: 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. :param expr: Mathematical expression with potential power operations. :type expr: str :param pow_patt: Regex pattern to match power expressions. :type pow_patt: str :returns: - Modified expression with placeholders - Dictionary mapping placeholders to (coefficient_symbol, power) tuples :rtype: 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)} .. py:function:: extract_num_consts(expr) Parse numeric constants from expression. :param expr: Mathematical expression containing numeric values. :type expr: str :returns: List of numeric constants found in expression. :rtype: List[float] Example:: >>> extract_num_consts("2 * \Pi_{0} + 0.5") [2.0, 0.5] .. py:function:: 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) :param dim_col_a: First dimensional column. :type dim_col_a: Sequence[int | float] :param dim_col_b: Second dimensional column. :type dim_col_b: Sequence[int | float] :param opn: Operation to perform ('*', '/', '+', '-'). :type opn: str :returns: Resulting dimensional column. :rtype: 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 .. py:function:: apply_pow_to_dims(dim_col, pwr) Assing power operation to the dimensional column. :param dim_col: Dimensional column. :type dim_col: Sequence[int | float] :param pwr: Power to apply. :type pwr: float :returns: Dimensional column with power applied. :rtype: 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 .. py:function:: 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 :param expr: Expression to validate. :type expr: str :param avail_coeffs: Available coefficients. :type avail_coeffs: Dict[str, Any] :param pattern: Regex pattern to match coefficients. :type pattern: str :raises ValueError: If expression is invalid or references missing coefficients. .. py:function:: 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. :param expr: Mathematical expression to parse. :type expr: str :param coeffs: Available coefficients dictionary. :type coeffs: Dict[str, Any] :param dim_col_fn: Function to extract dimension column from coefficient. Signature: dim_col_fn(coef) -> List[float | int] :type dim_col_fn: Callable[[Any], List[float | int]] :param coef_patt: Regex pattern to match coefficients. :type coef_patt: str :param pow_patt: Regex pattern to match power expressions. :type pow_patt: str :param ops_patt: Regex pattern to match operations. :type ops_patt: str :returns: - Resulting dimensional column - Numeric constant multiplier (None if no constant or constant = 1.0) :rtype: 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 ... ) .. py:function:: format_numeric_constant(constant) Arrange the numeric constant for display. :param constant: Numeric constant to format. :type constant: Optional[float] :returns: Formatted constant string, empty if None or 1.0. :rtype: str Example:: >>> format_numeric_constant(2.0) '2' >>> format_numeric_constant(0.5) '0.5' >>> format_numeric_constant(None) ''