i am getting following as string from MongoDB query:
1000*\frac{1-{(\frac{1}{1 0.1025})^{10}}}{0.09806}'
like to take it to equate to expression
expression = '1000*\frac{1-{(\frac{1}{1 0.1025})^{10}}}{0.09806}'
then convert to sympy expression with
expression_fin = parse_latex(expression)
but I got errors as this is not a raw string
I tried the following:
expression1=f'{expression}'
expression = repr('1000*\frac{1-{(\frac{1}{1 0.1025})^{10}}}{0.09806}')
both attempts does not works please help if any Edited:
def step_exec(step_id, part_req):
print('printing here')
newEq=repr(part_req['extras'][0]['fn_e8wgwASbHjuLR7Mb6mH2pC'])
print(newEq)
# rawtest = r'%s' % newEq
rawtest = parse_expr(newEq, evaluate=False)
sympy_inputFrom_parse_expr=parse_latex(rawtest)
print('sympy_inputFrom_parse_expr',sympy_inputFrom_parse_expr)
latex_inputFromSympy=latex(sympy_inputFrom_parse_expr)
print('latex_inputFromSympy',latex_inputFromSympy)
print(latex_inputFromSympy == part_req['extras'][0]['fn_e8wgwASbHjuLR7Mb6mH2pC'])
pprint.pprint(part_req['extras'][0]['fn_e8wgwASbHjuLR7Mb6mH2pC'])
print('raw here')
rawRstring=r'1200*\frac{1-{(\frac{1}{1 0.1259})^{12}}}{0.11917}'
print('rawRstring',rawRstring)
parsLatToSympy = parse_latex(rawRstring)
print('parsLatToSympy',parsLatToSympy)
SympyTOlatex=latex(parsLatToSympy)
print('SympyTOlatex',SympyTOlatex)
print('latex_inputFromSympy', latex_inputFromSympy)
return step_id
the result:
printing here
'1200*\frac{1-{(\frac{1}{1 0.1259})^{12}}}{0.11917}'
sympy_inputFrom_parse_expr 1200*((1 - 1/(0.1259 1)**12)/0.11917)
latex_inputFromSympy 1200 \frac{1 - \frac{1}{\left(0.1259 1\right)^{12}}}{0.11917}
False
'1200*\frac{1-{(\frac{1}{1 0.1259})^{12}}}{0.11917}'
raw here
rawRstring 1200*\frac{1-{(\frac{1}{1 0.1259})^{12}}}{0.11917}
parsLatToSympy 1200*((1 - 1/(0.1259 1)**12)/0.11917)
SympyTOlatex 1200 \frac{1 - \frac{1}{\left(0.1259 1\right)^{12}}}{0.11917}
latex_inputFromSympy 1200 \frac{1 - \frac{1}{\left(0.1259 1\right)^{12}}}{0.11917}
the raw string problem is solved from "parse_expr" but it changes the expression please help if any
CodePudding user response:
Raw string can be created by prefixing a string literal with r""
. In your case:
from sympy.parsing.latex import parse_latex
r_string = r"1000*\frac{1-{(\frac{1}{1 0.1025})^{10}}}{0.09806}"
expression = parse_latex(r_string)
expression
EDIT to accommodate comment. Yeah, nice problem you have :) For your specific case you cold do this s.encode('unicode_escape').decode()
. But this is likely going to modify the characters following backslashes, so you would also have to perform a replacement to bring them back to the original intent. For example:
s = "1000*\frac{1-{(\frac{1}{1 0.1025})^{10}}}{0.09806}"
# convert to raw, according to https://stackoverflow.com/a/65380295/2329968
r_string = s.encode('unicode_escape').decode()
# at this point you have a problem: the character inside s following a
# backslash might change to something else. Need to bring it back
r_string_mod = r_string.replace("x0c", "f")
parse_latex(r_string_mod)