Skip to content
Snippets Groups Projects

Added simplify_by_equality

Merged Frederik Hennig requested to merge da15siwa/pystencils:simplify_equality into master
2 files
+ 11
5
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -469,9 +469,12 @@ def simplify_by_equality(expr, a, b, c):
@@ -469,9 +469,12 @@ def simplify_by_equality(expr, a, b, c):
if not isinstance(a, sp.Symbol) or not isinstance(b, sp.Symbol):
if not isinstance(a, sp.Symbol) or not isinstance(b, sp.Symbol):
raise ValueError("a and b must be symbols.")
raise ValueError("a and b must be symbols.")
 
c = sp.sympify(c)
 
 
if not (isinstance(c, sp.Symbol) or is_constant(c)):
 
raise ValueError("c must be either a symbol or a constant!")
 
expr = sp.sympify(expr)
expr = sp.sympify(expr)
if not {a, b, c} <= expr.atoms(sp.Symbol):
return expr
expr_expanded = sp.expand(expr)
expr_expanded = sp.expand(expr)
a_coeff = expr_expanded.coeff(a, 1)
a_coeff = expr_expanded.coeff(a, 1)
@@ -481,11 +484,9 @@ def simplify_by_equality(expr, a, b, c):
@@ -481,11 +484,9 @@ def simplify_by_equality(expr, a, b, c):
if isinstance(c, sp.Symbol):
if isinstance(c, sp.Symbol):
c_coeff = expr_expanded.coeff(c, 1)
c_coeff = expr_expanded.coeff(c, 1)
rest = expr_expanded - (c * c_coeff).expand()
rest = expr_expanded - (c * c_coeff).expand()
elif is_constant(c):
else:
c_coeff = expr_expanded / c
c_coeff = expr_expanded / c
rest = 0
rest = 0
else:
raise ValueError("c must be either a symbol or a constant!")
a_summands = summands(a_coeff)
a_summands = summands(a_coeff)
b_summands = summands(b_coeff)
b_summands = summands(b_coeff)
Loading