def process_sympy(sympy): matherror = MathErrorListener(sympy) stream = antlr4.InputStream(sympy) lex = PSLexer(stream) lex.removeErrorListeners() lex.addErrorListener(matherror) tokens = antlr4.CommonTokenStream(lex) parser = PSParser(tokens) # remove default console error listener parser.removeErrorListeners() parser.addErrorListener(matherror) relation = parser.math().relation() expr = convert_relation(relation) return expr
def latex2sympy(sympy: str, variable_values={}): # Remove \n and \t # sympy = sympy.replace('\n', '', -1).replace('\t', '', -1) # Translate Derivative sympy = sympy.replace(r'\mathrm{d}', 'd', -1).replace(r'{\rm d}', 'd', -1) # variable values global VARIABLE_VALUES if len(variable_values) > 0: VARIABLE_VALUES = variable_values else: VARIABLE_VALUES = {} # setup listener matherror = MathErrorListener(sympy) # stream input stream = InputStream(sympy) lex = PSLexer(stream) lex.removeErrorListeners() lex.addErrorListener(matherror) tokens = CommonTokenStream(lex) parser = PSParser(tokens) # remove default console error listener parser.removeErrorListeners() parser.addErrorListener(matherror) # process the input return_data = None math = parser.math() # if a list if math.relation_list(): return_data = [] # go over list items relation_list = math.relation_list().relation_list_content() for list_item in relation_list.relation(): expr = convert_relation(list_item) return_data.append(expr) # if not, do default else: relation = math.relation() return_data = convert_relation(relation) return return_data
def process_sympy(latex, variable_values={}): # variable values global VARIABLE_VALUES if len(variable_values) > 0: VARIABLE_VALUES = variable_values else: VARIABLE_VALUES = {} # pre-processing pre_processed_latex = pre_process_latex(latex) # setup listener matherror = MathErrorListener(pre_processed_latex) # stream input stream = antlr4.InputStream(pre_processed_latex) lex = PSLexer(stream) lex.removeErrorListeners() lex.addErrorListener(matherror) tokens = antlr4.CommonTokenStream(lex) parser = PSParser(tokens) # remove default console error listener parser.removeErrorListeners() parser.addErrorListener(matherror) # process the input return_data = None math = parser.math() # if a list if math.relation_list(): return_data = [] # go over list items relation_list = math.relation_list().relation_list_content() for list_item in relation_list.relation(): expr = convert_relation(list_item) return_data.append(expr) # if not, do default else: relation = math.relation() return_data = convert_relation(relation) return return_data
def process_sympy(sympy, placeholder_values={}): # placeholder values global PLACEHOLDER_VALUES if len(placeholder_values) > 0: PLACEHOLDER_VALUES = placeholder_values else: PLACEHOLDER_VALUES = {} # setup listener matherror = MathErrorListener(sympy) # stream input stream = antlr4.InputStream(sympy) lex = PSLexer(stream) lex.removeErrorListeners() lex.addErrorListener(matherror) tokens = antlr4.CommonTokenStream(lex) parser = PSParser(tokens) # remove default console error listener parser.removeErrorListeners() parser.addErrorListener(matherror) # process the input return_data = None math = parser.math() # if a list if math.relation_list(): return_data = [] # go over list items relation_list = math.relation_list().relation_list_content() for list_item in relation_list.relation(): expr = convert_relation(list_item) return_data.append(expr) # if not, do default else: relation = math.relation() return_data = convert_relation(relation) return return_data