I have the following code
import sympy as sp
a, b = sp.symbols('a,b', real=True, positive=True)
expr2 = 1.01 * a**1.01 * b**0.99
print(type(expr2), '->', expr2)
Now I want a function that takes the string `'expr2'` and returns the expression `1.01 * a**1.01 * b**0.99`.
The ultimate objective is to put together the strings for two different expressions `'expr2'` and `'expr3'`, which should presumably give the same result, and verify their ratio, as in
def verify_ratio(vstr1, vstr2):
"""Compare the result of two different computations of the same quantity"""
ratio = sp.N(sp.parsing.sympy_parser.parse_expr(vstr1)) / sp.parsing.sympy_parser.parse_expr(vstr2)
print(vstr1 + ' / ' + vstr2, '=', sp.N(ratio))
return
which does not work, as per what I tried:
expr2 = 1.01 * a**1.01 * b**0.99
print(type(expr2), '->', expr2)
expr2b = sp.parsing.sympy_parser.parse_expr('expr2')
print(type(expr2b), '->', expr2b)
expr2c = sp.N(sp.parsing.sympy_parser.parse_expr('expr2'))
print(type(expr2c), '->', expr2c)
#print(sp.N(sp.parsing.sympy_parser.parse_expr('expr2')))
expr2d = sp.sympify('expr2')
print(type(expr2d), '->', expr2d)
with output
<class 'sympy.core.mul.Mul'> -> 1.01*a**1.01*b**0.99
<class 'sympy.core.symbol.Symbol'> -> expr2
<class 'sympy.core.symbol.Symbol'> -> expr2
<class 'sympy.core.symbol.Symbol'> -> expr2
None of my attempts achieved the objective.
Questions or links which did not help (at least for me):
1.
https://stackoverflow.com/questions/33606667/from-string-to-sympy-expression 2.
https://docs.sympy.org/latest/tutorials/intro-tutorial/basic_operations.html 3.
https://docs.sympy.org/latest/modules/parsing.html 4.
https://docs.sympy.org/latest/modules/core.html#sympy.core.sympify.sympify 5.
https://docs.sympy.org/latest/tutorials/intro-tutorial/manipulation.html**Note**:
Besides the practical aspects of my objective, I don't know if there is any formal difference between `Symbol` (which is a specific class) and *expression*. From the sources I read (e.g., [this][1]) I did not arrive to a conclusion.
This understanding may help in solving the question.
[1]:
https://docs.sympy.org/latest/tutorials/intro-tutorial/manipulation.html