def substract(a, b): """Returns the expression resultant of substracting terms a and b.""" a = Expression(a, no_vars_intended=True) b = Expression(b, no_vars_intended=True) if isanumber(a.expression) or isanumber(b.expression): raise NonAlgebraicOperationError if len(a.terms) > 1 or len(b.terms) > 1: raise InvalidOperationError(a, b) if TermOperations.getpower( a.expression) is not TermOperations.getpower( b.expression) or a.variables != b.variables: result = a.expression + '-' + b.expression return Expression.beautify(result) a_coefficient = a.get_number(0, frac_to_number=True) b_coefficient = b.get_number(0, frac_to_number=True) result = str(num(a_coefficient) - num(b_coefficient)) result = if_assign(result == '1', "", result) result = if_assign(result == '-1', "-", result) result += "".join(a.variables) + '**' + str( TermOperations.getpower(a.expression)) return Expression.beautify(result)
def add(a, b, non_algebraic=False): """Returns the expression resultant of adding terms a and b.""" a = Expression(a, no_vars_intended=True) b = Expression(b, no_vars_intended=True) if isanumber(a.expression) or isanumber(b.expression): if non_algebraic is True: return num(num(a.expression) + num(b.expression)) raise NonAlgebraicOperationError if len(a.terms) > 1 or len(b.terms) > 1: raise InvalidOperationError(a, b) if TermOperations.getpower( a.expression) is not TermOperations.getpower( b.expression) or a.variables != b.variables: operator = if_assign(b.expression.startswith('-'), '', '+') return Expression.beautify(a.expression + operator + b.expression) a_coefficient = a.get_number(0) b_coefficient = b.get_number(0) result = str(num(a_coefficient) + num(b_coefficient)) result = if_assign(result == '1', "", result) result = if_assign(result == '-1', "-", result) result += "".join(a.variables) + '**' + str( TermOperations.getpower(a.expression)) if result.endswith('**1'): result = result.replace('**1', '') return Expression.beautify(result)
def getpower(a): """Returns exponent of term a.""" if TermOperations.ispowered(a): return num(a[a.find('**') + 2:]) elif isanumber(a): return 0 return 1
def solve(self, debug=False): """Returns the solution of the equation as integer or float, depending on what the solution is.""" rhs_no_var_terms = [ num(term) for term in Expression( self.rhs, no_vars_intended=True).get_terms() if isanumber(term) ] lhs_no_var_terms = [ num(term) for term in Expression( self.lhs, no_vars_intended=True).get_terms() if isanumber(term) ] rhs_var_terms = [ term for term in Expression(self.rhs, no_vars_intended=True).get_terms() if any(var in term for var in self.variables) ] lhs_var_terms = [ term for term in Expression(self.lhs, no_vars_intended=True).get_terms() if any(var in term for var in self.variables) ] polynomial = '+'.join(lhs_var_terms) + '-' + '+'.join(rhs_var_terms) polynomial = if_assign(polynomial.endswith('-'), polynomial[:-1], polynomial) lhs = Polynomial(polynomial) coefficient = self.get_number(0, lhs.polynomial) result = sum(rhs_no_var_terms) - sum(lhs_no_var_terms) if debug: print(lhs, '=', result, '-->', str(result) + '/(' + coefficient + ')') if coefficient is '0': if result == 0: return "All real numbers are solutions." return "No solutions." return num(str((eval(str(result) + '/(' + coefficient + ')'))))
def get_number(self, index, expression=None, catch_variable=False, catch_term=False, frac_to_number=False): """Given an expression (str) returns the number that starts at index. It may or may not catch full terms and variables. If no expression is given then self.expression is imparted by default.""" expression = self.expression if expression is None else expression separators = copy.copy(SEPARATORS) starts_with_minus = False # Profilactic measure if catch_term is True and catch_variable is False: catch_variable = True if catch_variable is False: separators.extend(self.variables) if catch_term is True: separators.remove('*') expression = expression[index:] if expression.startswith('-'): starts_with_minus = True expression = expression[1:] if any(x in separators for x in expression): # If there actually are any separators on the expression... separator = next((x for x in expression if x in separators)) pos = expression.find(separator) if expression[0:pos] is "" and isanumber(expression[0:pos + 1]): number = expression[0:pos + 1] else: number = expression[0:pos] else: number = expression if frac_to_number: number = frac_to_num(number) if expression[0].isalpha() and number == '': return '1' if starts_with_minus is False else '-1' return number if starts_with_minus is False else '-' + number
def get_non_coefficient_term(self): return next((term for term in self.terms if isanumber(term)), "0")