def simplify_constant(self): terms_counter_analyzer = TermsCounterAnalyzer(self._term_list) terms = terms_counter_analyzer.get_constants() if 0 == len(terms): return self._remove_terms(terms) self.add_term(ConstantBuilder().value( self._get_added_terms_value(terms)).build())
def apply(self, name, value): terms_counter_analyzer = TermsCounterAnalyzer(self._term_list) terms = terms_counter_analyzer.get_variables_with_name(name) if len(terms) == 0: raise LookupError self._remove_terms(terms) for term in terms: term.multiply(value) self.add_term(ConstantBuilder().value(term.value).build())
def get_value_variable(self, name): terms_counter_analyzer = TermsCounterAnalyzer(self._term_list) terms = terms_counter_analyzer.get_variables_with_name(name) if 1 == len(terms): return terms[0].value elif 1 < len(terms): raise NotSimplified else: return 0
def get_value_constant(self): terms_counter_analyzer = TermsCounterAnalyzer(self._term_list) constants = terms_counter_analyzer.get_constants() if 1 == len(constants): return constants[0].value elif 1 < len(constants): raise NotSimplified else: return 0
def multiply(self, value): for term in self._term_list: term.multiply(value) if 0 == value: terms_counter_analyzer = TermsCounterAnalyzer(self._term_list) terms = terms_counter_analyzer.get_variables() self._remove_terms(terms) if 0 == len(self._term_list): self._term_list.append(ConstantBuilder().value(0).build())
def apply_expression(self, name, expression): terms_counter_analyzer = TermsCounterAnalyzer(self._term_list) terms = terms_counter_analyzer.get_variables_with_name(name) if len(terms) == 0: raise LookupError self._remove_terms(terms) for term in terms: temp_expression = expression.clon() temp_expression.multiply(term.value) self.add_expression(temp_expression)
def simplify_variable(self, name): terms_counter_analyzer = TermsCounterAnalyzer(self._term_list) terms = terms_counter_analyzer.get_variables_with_name(name) if 0 == len(terms): return self._remove_terms(terms) variable_value = self._get_added_terms_value(terms) if 0 == variable_value: self.add_term(ConstantBuilder().value(variable_value).build()) else: self.add_term(Variable(name, variable_value))
def get_name_set(self): terms_counter_analyzer = TermsCounterAnalyzer(self._term_list) return terms_counter_analyzer.get_variables_names_set()