I am not clear why you are working with strings and not just the expressions? I think the following may be what you want: ``` >>> import sympy as sp >>> a,b = sp.symbols('a b', positive = True) >>> expr = 1.01*a**1.01*b**0.99 >>> expr 1.01*a**1.01*b**0.99 >>> expr1 = 2*expr >>> expr1 2.02*a**1.01*b**0.99 >>> expr1/expr 2.00000000000000 ``` Your "verify ratio function" would then just take the two expressions directly. If you are trying to do something where you need to have intermediate strings, we will need more explicit details to provide some direction.
On Monday, January 9, 2023 at 10:00:04 AM UTC-6 Santiago S wrote: > 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 > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/f7bcfd54-d8be-47b3-9bc2-cd8bcc9ba889n%40googlegroups.com.