예제 #1
0
 def expand_term(self, term):
     """distributes a coefficient to all of the terms within a group within
     parentheses."""
     if self.phrase == '0':
         return Function('0', variables = self.variables, constants\
         = self.constants, asimp = self.asimp)
     if True:
         coefficient = Function(\
         eqn_helper.get_coefficient(term), variables = self.variables,\
         constants = self.constants, asimp = self.asimp)\
         .simplify_parentheses()
         core = Function(eqn_helper.get_core(term),\
         variables = self.variables, asimp = self.asimp,\
         constants = self.constants).simplify_parentheses()
         #most of the expansion really happens within the Function's
         #multiplication method
         core *= coefficient
         return core
     else:
         return term
예제 #2
0
 def combine_like_terms(self):
     """Takes terms with the same cores and combines their coefficients."""
     result = []
     terms = self.terms.copy()
     #False if no substitution has been made in the current loop
     subbed = False
     #makes everything a function so that it's easier to work with
     for i in range(len(terms)):
         terms[i] = Function(terms[i], variables = self.variables,\
         constants = self.constants, asimp = self.asimp)
     #trims away the first term each time it checks for like terms
     while len(terms) > 0:
         subbed = False
         for j in range(len(result)):
             #checks that the cores of the terms are the same
             if len(terms) > 0 and eqn_helper.get_core(result[j].phrase)\
             == eqn_helper.get_core(terms[0].phrase):
                 try:
                     if eqn_helper.get_core(result[j].phrase) != '':
                         result[j] = Function(\
                         str(float(eqn_helper.get_coefficient(terms[0].phrase))\
                         + float(eqn_helper.get_coefficient(result[j].phrase)))\
                         + '*' + eqn_helper.get_core(result[j].phrase),\
                         asimp = self.asimp, constants = self.constants)
                     else:
                         result[j] = Function(\
                         str(float(eqn_helper.get_coefficient(terms[0].phrase))\
                         + float(eqn_helper.get_coefficient(result[j].phrase))), \
                         asimp = self.asimp, constants = self.constants)
                     del terms[0]
                     subbed = True
                 except ValueError:
                     if eqn_helper.get_core(result[j].phrase) != '':
                         result[j] = Function(\
                         '('+str(Function(eqn_helper.get_coefficient(terms[0].phrase))\
                         + Function(eqn_helper.get_coefficient(result[j].phrase)))\
                         + ')*' + eqn_helper.get_core(result[j].phrase), \
                         asimp = self.asimp, constants = self.constants)
                         del terms[0]
                         subbed = True
                 except TypeError:
                     result [j] = Function(\
                     str(complex(terms[0].evaluate())\
                     + complex(result[j].evaluate())), asimp = False,\
                     constants = self.constants)
                     del terms[0]
                     subbed = True
         if not subbed:
             result.append(terms[0])
             del terms[0]
             subbed = False
     phrase = ''
     for i in result:
         try:
             if float(i.phrase) != 0:
                 phrase = phrase + i.get_phrase() + '+'
         except ValueError:
             phrase = phrase + i.get_phrase() + '+'
     i = 0
     while i < len(result)-1:
         if Function(result[i].get_phrase()).get_variables() \
         != Function(result[i+1].get_phrase()).get_variables():
             return Function('(' + phrase[:len(phrase)-1] + ')',\
             variables = self.variables, asimp = self.asimp,
             constants = self.constants)
         i += 1
     return Function(phrase[:len(phrase)-1], variables = self.variables,
                            asimp = self.asimp, constants = self.constants)