Just the tricky solution left

This commit is contained in:
2026-04-30 09:39:02 -04:00
parent 88160d086c
commit c31cb1c699
4 changed files with 14 additions and 13 deletions

View File

@@ -410,7 +410,7 @@ def distribute_step(equation):
left_expr = parse_expr(left, transformations=transformations, evaluate=False)
right_expr = parse_expr(right, transformations=transformations, evaluate=False)
print(f"calling distribute_once with expression: {sstr(left_expr)}")
#print(f"calling distribute_once with expression: {sstr(left_expr)}")
new_left_expr, distributed = distribute_once(left_expr)
if distributed != None:
@@ -423,10 +423,15 @@ def distribute_step(equation):
return steps
def build_ordered_add(args):
expr = args[0]
for a in args[1:]:
expr = Add(expr, a, evaluate=False)
return expr
flat_args = []
for arg in args:
if arg.is_Add:
flat_args.extend(arg.args)
else:
flat_args.append(arg)
return Add(*flat_args, evaluate=False)
def distribute_once(expr):
expr = flatten_mul(expr)
@@ -437,14 +442,14 @@ def distribute_once(expr):
# ------------------------------------------------------------
if expr.is_Mul:
print(f"expr: {sstr(expr)}")
#print(f"expr: {sstr(expr)}")
add_part = None
other_parts = []
# extract Add factor + everything else
for arg in expr.args:
print(f"arg: {sstr(arg)}")
#print(f"arg: {sstr(arg)}")
if arg.is_Add and add_part is None:
add_part = arg
@@ -455,7 +460,7 @@ def distribute_once(expr):
# DISTRIBUTION RULE
# --------------------------------------------------------
if add_part is not None:
print(f"expr used: {sstr(expr)}, add used: {sstr(add_part)}")
#print(f"expr used: {sstr(expr)}, add used: {sstr(add_part)}")
distributed_value = Mul(*other_parts)
@@ -472,7 +477,7 @@ def distribute_once(expr):
# STEP 2: PRIORITY-BASED RECURSION (IMPORTANT FIX)
# ------------------------------------------------------------
if expr.args:
print(f"step2 args:{expr.args}")
#print(f"step2 args:{expr.args}")
# PASS 1: ONLY distributable Mul(Add(...))
for i, arg in enumerate(expr.args):
if arg.is_Mul and arg.has(Add):