|
| 1 | +""" |
| 2 | +Evaluation routines for Distribute[] |
| 3 | +""" |
| 4 | + |
| 5 | +from mathics.core.expression import Expression |
| 6 | +from mathics.core.symbols import Symbol |
| 7 | + |
| 8 | + |
| 9 | +def eval_Distribute(expr, operator_symbol, evaluation): |
| 10 | + """ |
| 11 | + Recursively distribute operator_symbol over the expression. |
| 12 | + Returns None if no distribution was performed. |
| 13 | + """ |
| 14 | + if not isinstance(expr, Expression): |
| 15 | + return None |
| 16 | + |
| 17 | + head = expr.get_head() |
| 18 | + elements = expr.elements |
| 19 | + |
| 20 | + # Find the first element containing the operator_symbol. |
| 21 | + operator_position = None |
| 22 | + for i, elem in enumerate(elements): |
| 23 | + if contains_operator_symbol(elem, operator_symbol): |
| 24 | + operator_position = i |
| 25 | + break |
| 26 | + |
| 27 | + if operator_position is None: |
| 28 | + # No element contains operator_symbol, but check if head itself needs distribution. |
| 29 | + return None |
| 30 | + |
| 31 | + # Get the element at the target position |
| 32 | + target_elem = elements[operator_position] |
| 33 | + |
| 34 | + # If the element is the operator symbol (e.g., Plus), distribute over it. |
| 35 | + if is_operator_symbol(target_elem, operator_symbol): |
| 36 | + # Get all components of the operator symbol |
| 37 | + target_components = target_elem.elements |
| 38 | + |
| 39 | + # Create new expressions by replacing the operator position with each component. |
| 40 | + result_parts = [] |
| 41 | + for component in target_components: |
| 42 | + # Replace the operator position with this component. |
| 43 | + new_elements = list(elements) |
| 44 | + new_elements[operator_position] = component |
| 45 | + new_expr = Expression(head, *new_elements) |
| 46 | + |
| 47 | + # Recursively distribute in the new Expression. |
| 48 | + recursive_result = eval_Distribute(new_expr, operator_symbol, evaluation) |
| 49 | + if recursive_result is not None: |
| 50 | + result_parts.append(recursive_result) |
| 51 | + else: |
| 52 | + result_parts.append(new_expr) |
| 53 | + |
| 54 | + # Return the combination using the operator symbol. |
| 55 | + return Expression(operator_symbol, *result_parts) |
| 56 | + |
| 57 | + # If the element contains but is not the operator symbol, recurse into it. |
| 58 | + else: |
| 59 | + recursive_result = eval_Distribute(target_elem, operator_symbol, evaluation) |
| 60 | + if recursive_result is not None: |
| 61 | + new_elements = list(elements) |
| 62 | + new_elements[operator_position] = recursive_result |
| 63 | + new_expr = Expression(head, *new_elements) |
| 64 | + |
| 65 | + # Try to distribute the modified Expression again |
| 66 | + second_result = eval_Distribute(new_expr, operator_symbol, evaluation) |
| 67 | + if second_result is not None: |
| 68 | + return second_result |
| 69 | + return new_expr |
| 70 | + |
| 71 | + return None |
| 72 | + |
| 73 | + |
| 74 | +def is_operator_symbol(expr, operator_symbol): |
| 75 | + """ |
| 76 | + Check if expr's head is exactly the operator_symbol. |
| 77 | + """ |
| 78 | + if not isinstance(expr, Expression): |
| 79 | + return False |
| 80 | + |
| 81 | + expr_head = expr.get_head() |
| 82 | + |
| 83 | + if isinstance(operator_symbol, Symbol): |
| 84 | + return ( |
| 85 | + isinstance(expr_head, Symbol) |
| 86 | + and expr_head.get_name() == operator_symbol.get_name() |
| 87 | + ) |
| 88 | + |
| 89 | + return expr_head == operator_symbol |
| 90 | + |
| 91 | + |
| 92 | +def contains_operator_symbol(expr, operator_symbol): |
| 93 | + """ |
| 94 | + Check if expr contains operator_symbol anywhere. |
| 95 | + """ |
| 96 | + if not isinstance(expr, Expression): |
| 97 | + return False |
| 98 | + |
| 99 | + # Check if this expression's head is the target |
| 100 | + if is_operator_symbol(expr, operator_symbol): |
| 101 | + return True |
| 102 | + |
| 103 | + # Recursively check sub-expressions |
| 104 | + for elem in expr.elements: |
| 105 | + if contains_operator_symbol(elem, operator_symbol): |
| 106 | + return True |
| 107 | + |
| 108 | + return False |
0 commit comments