Files
Youtube_Math/algebraic_steps.py

197 lines
6.3 KiB
Python

#All Rights Reserved John Salguero
#Steps that are generated
from sympy import *
from sympy.parsing.sympy_parser import (
parse_expr,
standard_transformations,
implicit_multiplication_application
)
transformations = standard_transformations + (implicit_multiplication_application,)
def move_all_to_one_side(equation):
step = {}
current = equation
step["before"] = current
left, right = current.split("=")
x = symbols('x')
left_expr = parse_expr(left, transformations=transformations, evaluate=False)
right_expr = parse_expr(right, transformations=transformations, evaluate=False)
new_expr = left_expr - right_expr
step["after"] = f"{sstr(new_expr)} = 0"
step["step"] = f"Subtract both sides by {sstr(right_expr)}"
step["rule"] = "Subtraction Property of Equality"
return step
def add_both_sides(equation, value):
step = {}
current = equation
step["before"] = current
left, right = current.split("=")
x = symbols('x')
left_expr = parse_expr(left, transformations=transformations, evaluate=False)
right_expr = parse_expr(right, transformations=transformations, evaluate=False)
new_left_expr = left_expr + value
new_right_expr = right_expr + value
step["after"] = f"{sstr(new_left_expr)} = {sstr(new_right_expr)}"
step["step"] = f"Add both sides by {sstr(value)}"
step["rule"] = "Addition Property of Equality"
return step
def subtract_both_sides(equation, value):
step = {}
current = equation
step["before"] = current
left, right = current.split("=")
x = symbols('x')
left_expr = parse_expr(left, transformations=transformations, evaluate=False)
right_expr = parse_expr(right, transformations=transformations, evaluate=False)
new_left_expr = left_expr - value
new_right_expr = right_expr - value
step["after"] = f"{sstr(new_left_expr)} = {sstr(new_right_expr)}"
step["step"] = f"Subtract both sides by {sstr(value)}"
step["rule"] = "Subtraction Property of Equality"
return step
def divide_both_sides(equation, value):
step = {}
current = equation
step["before"] = current
left, right = current.split("=")
x = symbols('x')
left_expr = parse_expr(left, transformations=transformations, evaluate=False)
right_expr = parse_expr(right, transformations=transformations, evaluate=False)
new_left_expr = clean(left_expr / value)
new_right_expr = clean(right_expr / value)
step["after"] = f"{sstr(new_left_expr)} = {sstr(new_right_expr)}"
step["step"] = f"Divide both sides by {sstr(value)}"
step["rule"] = "Division Property of Equality"
return step
def multiply_both_sides(equation, value):
step = {}
current = equation
step["before"] = current
left, right = current.split("=")
x = symbols('x')
left_expr = parse_expr(left, transformations=transformations, evaluate=False)
right_expr = parse_expr(right, transformations=transformations, evaluate=False)
new_left_expr = left_expr * value
new_right_expr = right_expr * value
step["after"] = f"{sstr(new_left_expr)} = {sstr(new_right_expr)}"
step["step"] = f"Multiply both sides by {sstr(value)}"
step["rule"] = "Multiplication Property of Equality"
return step
def factor_collect(equation):
step = {}
current = equation
step["before"] = current
left, right = current.split("=")
x = symbols('x')
left_expr = parse_expr(left, transformations=transformations, evaluate=False)
right_expr = parse_expr(right, transformations=transformations, evaluate=False)
new_left_expr = factor(left_expr)
new_right_expr = factor(right_expr)
step["after"] = f"{sstr(new_left_expr)} = {sstr(new_right_expr)}"
step["step"] = f"Collect the factors"
step["rule"] = "Factoring by grouping"
return step
def factor_form_collection(equation, factor):
step = {}
current = equation
step["before"] = current
left, right = current.split("=")
x = symbols('x')
left_expr = parse_expr(left, transformations=transformations, evaluate=False)
right_expr = parse_expr(right, transformations=transformations, evaluate=False)
new_left_expr = collect(left_expr, factor)
new_right_expr = collect(right_expr, factor)
step["after"] = f"{sstr(new_left_expr)} = {sstr(new_right_expr)}"
step["step"] = f"Collect the factors using factor {sstr(factor)}"
step["rule"] = "Factor by grouping"
return step
def combine_like_terms(equation):
step = {}
current = equation
step["before"] = current
left, right = current.split("=")
x = symbols('x')
left_expr = parse_expr(left, transformations=transformations, evaluate=False)
right_expr = parse_expr(right, transformations=transformations, evaluate=False)
## Combine Left Terms
left_terms = left_expr.as_ordered_terms()
# group by base
left_groups = {}
for t in left_terms:
coeff, rest = t.as_coeff_Mul()
left_groups.setdefault(rest, 0)
left_groups[rest] += coeff
# rebuild manually
new_left_terms = []
for base, coeff in left_groups.items():
if coeff != 0:
new_left_terms.append(coeff * base)
new_left_expr = sum(new_left_terms)
## Comnine Right Terms
right_terms = right_expr.as_ordered_terms()
# group by base
right_groups = {}
for t in right_terms:
coeff, rest = t.as_coeff_Mul()
right_groups.setdefault(rest, 0)
right_groups[rest] += coeff
# rebuild manually
new_right_terms = []
for base, coeff in right_groups.items():
if coeff != 0:
new_right_terms.append(coeff * base)
new_right_expr = sum(new_right_terms)
step["after"] = f"{sstr(new_left_expr)} = {sstr(new_right_expr)}"
step["step"] = "Collect Like Terms"
step["rule"] = "Combine the like terms"
return step
def clean(expr):
# remove explicit 1 multipliers
expr = expr.replace(
lambda e: isinstance(e, Mul),
lambda e: Mul(*[arg for arg in e.args if arg != 1])
)
return expr