def _simplify(self, expression): multiplies, others = filter_split(lambda x: isinstance(x, Multiply), expression.get_expressions()) sets = [set(x.get_expressions()) for x in multiplies] c = Counter() for s in sets: c.update(s) term, count = c.most_common(1)[0] if count < 2: return expression in_terms = [] for mult in multiplies: if term in mult.get_expressions(): l = list(mult.get_expressions()) l.remove(term) if len(l) == 1: in_terms.append(l[0]) else: in_terms.append(Multiply(*l)) else: others.append(mult) if len(others) == 0: return term * Add(*in_terms) return Add(term * Add(*in_terms), *others)
def _simplify(self, expression): values, other = filter_split(lambda x: isinstance(x, Value), expression.get_expressions()) value = reduce(lambda x, y: x + y, map(lambda x: x.get_numeric_value(), values)) if len(other) == 0: return Value(value) if value == 0: if len(other) == 1: return other[0] return Add(*other) return Add(value, *other)
def _simplify(self, expression): expr = expression.get_expression() var = expression.get_var() exprs = expr.get_expressions() terms = [] for i, e in enumerate(exprs): rest = exprs[:i] + exprs[i + 1:] terms.append(Multiply(Derivative(e, var), *rest)) return Add(*terms)
def _simplify(self, expression): exprs = list(expression.get_expressions()) add = next(e for e in exprs if isinstance(e, Add)) exprs.remove(add) terms = [] for term in add.get_expressions(): if len(exprs) == 1: terms.append(Multiply(exprs[0], term)) else: terms.append(Multiply(Multiply(*exprs), term)) return Add(*terms)
def _simplify(self, expression): exprs = expression.get_expressions() counts = Counter(exprs) exprs = [] for term in counts: freq = counts[term] if freq == 1: exprs.append(term) else: exprs.append(Value(freq) * term) if len(exprs) == 1: return exprs[0] return Add(*exprs)
def runTest(self): self.assertSimplify(Add(x, 3, 5), Add(x, 8)) self.assertSimplify(Add(x - 1, 3, 5), Add(x - 1, 8))
def runTest(self): self.assertSimplify((x + y) + z, Add(z, y, x))
def _simplify(self, expression): adds, other = filter_split(lambda x: isinstance(x, Add), expression.get_expressions()) adds = list(chain(*map(lambda x: x.get_expressions(), adds))) return Add(*adds, *other)
def _simplify(self, expression): expr = expression.get_expression() var = expression.get_var() exprs = expr.get_expressions() return Add(*map(lambda x: Derivative(x, var), exprs))