def create_expression_data(list_of_data, complex_frac): expression_data = [] for index, part in enumerate(list_of_data): before = list_of_data[index - 1] if "{" in part and "{" in before and index != 0: if r"{{" not in part: before, part = subsitute("{|}", "", before), subsitute("{|}", "", part) rational = [RationalObject(before, part)] else: n, d = first_proccess(before[1:]), first_proccess(part[1:]) n_expr_data, d_expr_data = create_expression_data( n, complex_frac), create_expression_data(d, complex_frac) if len(n_expr_data) == 1 and len( d_expr_data) == 1 and complex_frac == False: rational = [ RationalObject(n_expr_data[0].numerator, n_expr_data[0].denomenator), "/", RationalObject(d_expr_data[0].numerator, d_expr_data[0].denomenator) ] else: rational = [ ComplexRationalObject(n_expr_data, d_expr_data) ] expression_data += rational elif check_if_int(part): expression_data.append(RationalObject(part, 1)) elif "{" not in part: expression_data.append(part) return expression_data
def create_solved_expression(self, expression_data, variable_solutions, quotient=None): """ Puts solution dictionary, expression_data, and possibe quotient (if improper) together Replaces solutions into the unknowns, adding the quotient if applicable Returns modified expression_data """ output = [] for part in expression_data: if isinstance(part, (FactoredRationalObject)): numerator_str = part.get_n_str()[1:-1] if numerator_str in variable_solutions.keys(): matched_solution = variable_solutions[numerator_str] temp_numerator = subsitute("[A-Z]", "1", numerator_str) mini_expression_data = [ RationalObject(temp_numerator, part.get_d_str()) * RationalObject(str(matched_solution), 1) ] output += mini_expression_data else: output.append(part) if quotient: index = output.index("=") output.insert(index + 1, quotient) output.insert(index + 2, "+") return output
def __init__(self, numerator, denomenator): self.numerator = [] self.denomenator = [] for n in numerator: without_paren = subsitute(r"\(|\)", "", str(n)) if n != 1 or len(numerator) == 1: self.numerator.append(n) for d in denomenator: without_paren = subsitute(r"\(|\)", "", str(d)) if d != 1 or len(denomenator) == 1: self.denomenator.append(d) if not self.numerator: self.numerator.append(1) if not self.denomenator: self.denomenator.append(1)
def balance_parenthesis(self, n_or_d): n_or_d = str(n_or_d) eliminated_paren = list(subsitute(r"^\(\(?|\)$", "", n_or_d)) balanced_paren = [] counter = 0 for char in eliminated_paren: if char == "(": counter += 1 elif char == ")": counter -= 1 if counter < 0: balanced_paren.insert(0, "(") counter += 1 balanced_paren.append(char) if counter > 0: balanced_paren += [")"] * counter return "".join(balanced_paren)
def first_proccess(latex_str): latex_str = subsitute(r"\\frac|\\left|\\right", "", latex_str) latex_str = subsitute(r"\\cdot", "*", latex_str) latex_str = subsitute("\^", "**", latex_str) latex_str = subsitute("([0-9]+)([a-z]+)", r"\g<1>*\g<2>", latex_str) latex_str = subsitute("([a-z]+)(\()", r"\g<1>*\g<2>", latex_str) latex_str = subsitute("([0-9]+)(\()", r"\g<1>*\g<2>", latex_str) latex_str = subsitute("(\))(\()", r"\g<1>*\g<2>", latex_str) counter = 0 output = [] temp_frac = [] outside = [] for char in latex_str: if char == "{": counter += 1 if outside: output.append("".join(outside)) outside = [] elif char == "}": counter -= 1 if counter > 0: temp_frac.append(char) if counter == 0: if temp_frac: output.append("".join(temp_frac)) temp_frac = [] elif check_if_int(char): outside.append(char) else: if outside: output.append("".join(outside)) outside = [] output.append(char) if outside: output.append("".join(outside)) return output