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

View File

@@ -208,14 +208,10 @@ def generate_binomial ():
#a(x + b) + c(x + d) = e #a(x + b) + c(x + d) = e
ans = random.choice([i for i in range(-15, 16)]) ans = random.choice([i for i in range(-15, 16)])
a = random.choice([i for i in range(-5, 6) if i != 0]) a = random.choice([i for i in range(-5, 6) if i != 0])
a = 4
b = random.choice([i for i in range(-5, 6)]) b = random.choice([i for i in range(-5, 6)])
b = 4
c = random.choice([i for i in range(-5, 6) if i != 0 and i != -a]) c = random.choice([i for i in range(-5, 6) if i != 0 and i != -a])
c = 1
d = random.choice([i for i in range(-5, 6)]) d = random.choice([i for i in range(-5, 6)])
d = 5
e = a * (ans + b) + c * (ans + d) e = a * (ans + b) + c * (ans + d)