def __init__(self, phrase, var_values = None, variables = None,\ cpx = True, constants = {'e':e, 'pi':pi, 'I':1j, \ 'π':pi, 'inf':'inf', 'oo':'inf'}, asimp = True): """makes a Function given a string phrase and isolates its variables""" self.complex = cpx self.phrase = phrase self.phrase = eqn_helper.make_friendly(\ self.phrase, constants.keys(), self.complex) if constants != None: for name in constants: self.phrase = eqn_helper.replace_constants(\ self.phrase, constants[name], name) self.phrase = eqn_helper.make_friendly(\ self.phrase, constants.keys(), self.complex) else: self.constants = dict('') self.constants = constants #autosimplify self.asimp = asimp self.var_names = eqn_helper.get_variables(self.phrase, self.complex) if variables == None: #default list of variables for testing purposes if var_values == None: self.var_values = [i+1 for i in range(len(self.var_names))] else: self.var_values = var_values #the order of variable assignment and dictionary creation allows #the variables in a Function to simply be assigned left to right self.variables = dict(zip(self.var_names, self.var_values)) else: self.set_variables(variables) self.terms = self.init_terms() self.degrees = self.init_degrees()
def set_complex(self, cpx): """sets True or False that the function uses complex variables""" self.complex = cpx self.phrase = eqn_helper.make_friendly(self.phrase, cpx) self.var_names = eqn_helper.get_variables(self.phrase, self.complex) if len(self.var_values) != len(self.var_names): self.var_values = [i+1 for i in range(len(self.var_names))] self.variables = dict(zip(self.var_names, self.var_values))
def __init__(self, phrase, cpx = False, constants = {'e':e, 'pi':pi,\ 'I':1j, 'π':pi, 'inf':'inf', 'oo':'inf'}): """makes a Function given a string phrase and isolates its variables""" self.complex = cpx self.phrase = phrase self.phrase = make_friendly(\ self.phrase, constants.keys(), self.complex) if constants != None: for name in constants: self.phrase = replace_constants(\ self.phrase, constants[name], name) self.phrase = make_friendly(\ self.phrase, constants.keys(), self.complex) else: self.constants = dict('') self.constants = constants self.var_names = get_variables(self.phrase, self.complex) self.imd = RawEvaluator(self.phrase)
def evaluate(self, variables = None, phrase = None): """takes a Function defined by the items in a phrase and evaluates it given the values in the values array""" phrase = self.phrase #sets variables to their previously assigned values if type(variables) == type([0]): for i in range(len(variables)): if i < len(self.var_names): phrase = replace_variables(\ phrase, variables[i], self.var_names[i]) else: break elif variables != None: for name in variables: phrase = replace_variables(\ phrase, variables[name], name) phrase = self.eval_functions(phrase) #this try-catch setup is obviously naïve, but it's a useful way to keep #the program running while trying to evaluate many Functions for making #a graph or something of the like try: try: return_term = evaluate_numbers(phrase) except ValueError: print('Kevin\'s term evaluator failed on:', phrase) return_term = eval(phrase) return return_term except ZeroDivisionError: print('Zero Division Error.') return 0 except SyntaxError: print('Syntax Error.') return_term = eval(make_friendly(phrase)) return return_term except OverflowError: print('Overflow Error. Value too large or too small.') return 0
def __init__(self, phrase): self.phrase = re.sub('(?<!\*\*)(?<!e)\-', '-1*', phrase) self.phrase = eqn_helper.make_friendly(self.phrase) self.operators = self.init_operators() self.secondary_terms = [] self.terms = self.init_terms()
def __init__(self, phrase): self.phrase = re.sub('(?<!\*\*)(?<!e)\-', '-1*', phrase) #fix this to better handle exponential terms self.phrase = eqn_helper.make_friendly(self.phrase) self.operators = self.init_operators() self.terms = self.init_terms()