def express_as(self, form): """Expresses the equation in the form passed as an argument and returns an instance of that form's class as the new expression. Valid forms are , Standard and . Keyword arguments: form (str): the form the equation will be converted to""" slope = str(self.slope) if not isinstance(form, LinearForms): raise InvalidFormError(form) if form is self.form: raise RedundantConversionError(self.form, form) if form is LinearForms.PointSlope: points = self.get_point(2) x_point, y_point = str(points[0]), str(points[1]) rewritten = "y-" + y_point + "=" + slope + "(x-" + x_point + ")" rewritten = Expression.beautify(rewritten) return PointSlope(rewritten) if form == LinearForms.SlopeIntercept: operator = "+" if self.y_intercept > 0 else "" rewritten = "y=" + slope + "x" + operator + str(self.y_intercept) rewritten = Expression.beautify(rewritten) return SlopeIntercept(rewritten) raise InvalidFormError(form)
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 sort(self, for_variable): """Sorts a Standard Form linear equation to be solved for a given variable. Keyword arguments: variable (str): the variable this equation will be sorted to solve for.""" # Required and convenient variables definition. eqtn = self.equation c_pos, a, b = eqtn.find( "=") + 1, self.x_coefficient, self.y_coefficient c = self.get_number(c_pos, eqtn) den = if_assign(for_variable == 'y', a, b) mult = if_assign(for_variable == 'y', b, a) if eqtn[eqtn.index(for_variable) - len(str(mult)) - 1] == "-": operator = "+" else: operator = "-" # Expressing sol_side = "(" + c + operator + mult + "*" + for_variable + ")" + "/" + den sol_side = Expression.beautify(sol_side) return sol_side
def addition(p, q, name=None): """Performs addition between polynomials p and q.""" if not isinstance(p, Polynomial) or not isinstance(q, Polynomial): raise InvalidOperationError(p, q) result = "" alr_added = [] for term in p.terms: if term in alr_added: continue match = PolynomialOperation.find_term_by_degree(term, q) if match is not None and match not in alr_added: result += str( TermOperations.add(term, match, non_algebraic=True)) + '+' alr_added.extend([term, match]) continue result += term + '+' alr_added.append(term) for term in q.terms: if PolynomialOperation.find_term_by_degree(term, p) is None: result += term + '+' return Polynomial(Expression.beautify(result), name)
def multiply(a, b): """Multiplies terms a and b.""" a = Expression(a, no_vars_intended=True) b = Expression(b, no_vars_intended=True) power = str( if_assign( TermOperations.getpower(a.expression) >= TermOperations.getpower(b.expression), TermOperations.getpower(a.expression), TermOperations.getpower(b.expression))) variables = set(a.variables + b.variables) if len(TermOperations.commonvars(a.expression, b.expression)) > 0: power = str( int(TermOperations.getpower(a.expression)) + int(TermOperations.getpower(b.expression))) power = if_assign(power == '1', '', power) if power == '1': return "".join(variables) a_coefficient = a.get_number(0) b_coefficient = b.get_number(0) result = str(int(a_coefficient) * int(b_coefficient)) + "".join(variables) + '**' + power return Expression.beautify(result)
def simplify(self): """Simplify terms of same degree into single term.""" polynomial = "" for k, v in self.terms_by_degree.items(): polynomial += v + "+" return Expression.beautify(polynomial)
def express_as(self, form): """Expresses the equation in the form passed as an argument and returns an instance of that form's class as the new expression. Valid forms are , Standard and . Keyword arguments: form (str): the form the equation will be converted to""" # Required and convenient variables definition. slope, y_intercept = str(self.slope), str(self.y_intercept) if not isinstance(form, LinearForms): raise InvalidFormError(self.equation.form, form) if form == self.form: raise RedundantConversionError(self.form, form) # Express in Standard Form. if form is LinearForms.Standard: slope = slope.replace('-', '') x_op = if_assign(self.slope < 0, '', '-') # 'y' will always be positive, for negative multipliers of it will # be distributed. Hence why '+' is the operator before 'y'. rewritten = x_op + slope + "x" + '+' + "y" + "=" + y_intercept rewritten = Expression.beautify(rewritten) return Standard(rewritten) # Express in form. if form is LinearForms.PointSlope: points = self.get_point(2) x_point, y_point = str(points[0]), str(points[1]) rewritten = "y-" + y_point + "=" + slope + "(x-" + x_point + ")" rewritten = Expression.beautify(rewritten) return PointSlope(rewritten) raise InvalidFormError(form)
def reorder_terms(self): """Reorders the terms of the polynomial; higher degrees first.""" ordered_terms = [] for degree in self.degrees: ordered_terms.append(self.terms_by_degree[degree]) expression = "+".join(ordered_terms) return Expression.beautify(expression)
def sort_for_y(self): """Sorts equation for y.""" eqtn, sol_side = self.equation, self.rhs x_index = eqtn.index('x') operator = if_assign(eqtn[x_index + 1] == '-', '+', '-') slope_sign = if_assign(eqtn[0] == '-', '-', '') sol_side = "(" + self.y_coefficient + "*y" + operator + \ str(self.y_intercept).replace('-', '') + ")/" + slope_sign + str(self.slope) sol_side = Expression.beautify(sol_side) return sol_side
def product(p, q, name=None): """Takes the product of polynomials p and q.""" if not isinstance(p, Polynomial) or not isinstance(q, Polynomial): raise InvalidOperationError(p, q) result = [] for term_of_p in p.terms: for term_of_q in q.terms: result.append(TermOperations.multiply(term_of_p, term_of_q)) result = '+'.join(result) return Polynomial(Expression.beautify(result), name)
def sort_for_x(self): """Sorts equation for x.""" eqtn, sol_side, y_coefficient = self.equation, self.rhs, self.y_coefficient x_index = eqtn.index('x') # Set the solution side sol_side = "(" + sol_side + ")/" + y_coefficient # Add a * symbol before the x if there's a number before it. sol_side = if_assign(eqtn[x_index - 1].isdigit(), sol_side.replace('x', '*x'), sol_side) # Beautify the solution side. sol_side = Expression.beautify(sol_side) return sol_side
def sort_for_y(self): """Sorts equation for y.""" # Required and convenient variables definition. eqtn, y_index = self.equation, self.equation.index('y') x_index = eqtn.index('x') y_point = self.get_number(y_index + 2, eqtn) x_point = self.get_number(x_index + 2, eqtn) x_point_op = if_assign(eqtn[x_index + 1] == '-', '-', '+') # Expression sol_side = "(y" + eqtn[y_index + 1] + y_point + "-" + str(self.slope) + \ "*" + x_point_op + x_point + ")/" + str(self.slope) sol_side = Expression.beautify(sol_side) return sol_side
def divide(a, b): a = Expression(a) b = Expression(b) variables = set(a.variables + b.variables) power = '**' + str( int(TermOperations.getpower(a.expression)) - int(TermOperations.getpower(b.expression))) power = if_assign(power == '**1', '', power) if power == '**0': return "/".join(variables) a_coefficient = a.get_number(0) b_coefficient = b.get_number(0) result = str(num( int(a_coefficient) / int(b_coefficient))) + "/".join(variables) result = if_assign(power != '', '(' + result + ')' + power, result) return Expression.beautify(result)
def _complete(self): if not any('**2' in term for term in self.terms): raise NoSquareError(self.expression) term_count = len(self.terms) if term_count is 3: return if term_count is 2: if any(self.variables[0] not in term for term in self.terms): self.terms.insert(1, '0' + self.variables[0]) else: self.terms.insert(2, '0') if term_count is 1: self.terms.insert(1, '0' + self.variables[0]) self.terms.insert(2, '0') self.expression = Expression.beautify('+'.join(self.terms))
def express_as(self, form): """Expresses the equation in the form passed as an argument and returns an instance of that form's class as the new expression. Valid forms are , Standard and . Keyword arguments: form (str): the form the equation will be converted to""" eqtn, slope = self.equation, str(self.slope) if not isinstance(form, LinearForms): raise InvalidFormError(form) if form is self.form: raise RedundantConversionError(self.form, form) if form is LinearForms.SlopeIntercept: operator = if_assign(self.y_intercept < 0, '', '+') rewritten = 'y=' + slope + "x" + operator + str(self.y_intercept) if '--' in rewritten: rewritten = rewritten.replace('--', '+') return SlopeIntercept(rewritten) if form is LinearForms.Standard: operator = if_assign(eqtn[0] == '-', '-', '+') y_coefficient = if_assign(self.y_coefficient == '1', '', self.y_coefficient) rewritten = '-' + self.x_coefficient + 'x' + operator + \ y_coefficient + 'y' + '=' + str(self.y_intercept) rewritten = Expression.beautify(rewritten) return Standard(rewritten) raise InvalidFormError(form)
def sort_for_x(self): """Sorts equation for x.""" eqtn, y_index = self.equation, self.equation.index('y') x_index = eqtn.index('x') y_point = self.get_number(y_index + 2, eqtn) x_point = self.get_number(x_index + 2, eqtn) slope_pos = self.equal_index + 1 # Gets the slope instead of using self.slope because this method is called # before defining (and to define) the slope attribute. slope = self.get_number(slope_pos, eqtn) first_op = if_assign(eqtn[y_index + 1] == '-', '+', '-') second_op = eqtn[x_index + 1] sol_side = slope + "*(x" + second_op + x_point + ")" + first_op + y_point sol_side = Expression.beautify(sol_side) return sol_side