parse_expr

47 views
Skip to first unread message

Denis Akhiyarov

unread,
Jan 28, 2015, 5:28:51 PM1/28/15
to sy...@googlegroups.com
Can I use parse_expr to parse an expression such as "a1*x1**2+a2*x1+a3+a4*x1*x2+a5*x2**2" and inject the symbols into globals or locals namespace? 

Looks like auto_symbol is not doing this. So I have to manually inject each symbol using sympy.var() method.

exp0=parse_expr("a1*x1**2+a2*x1+a3+a4*x1*x2+a5*x2**2",transformations=(auto_number,auto_symbol))
exp0.subs(a1,10)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-19dc46bbe490> in <module>()
----> 1 exp0.subs(a1,10)

NameError: name 'a1' is not defined


But after my hack:

for each_sym in exp0.atoms(sympy.Symbol):
    sympy.var(each_sym.__str__())


exp0.subs(a1,10)

2*x1 + a3 + a4*x1*x2 + a5*x2**2 + 10*x1**2

Aaron Meurer

unread,
Jan 28, 2015, 5:32:07 PM1/28/15
to sy...@googlegroups.com
auto_symbol doesn't do namespace injection. It just means that the parser won't fail with a NameError (undefined names in the namespace that the parser users will be converted to Symbols). Your hack works (I would use each_sym.name instead of __str__(), and note that str(each_sym) is the same as each_sym.__str__()).

Aaron Meurer

--
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+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/26d01fa4-15fd-45da-b043-9197410f0286%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Denis Akhiyarov

unread,
Jan 28, 2015, 6:01:50 PM1/28/15
to sy...@googlegroups.com
Thanks Aaron, I'm going to use .name. Perhaps an auto_inject transformation will be useful for parse_expr. BTW, why is var() not recommended?
I cannot think of any way to avoid using var() when parsing string expressions.

It's recommended not to use :func:`var` in library code, where :func:`symbols` has to be used

--
You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/VY5m2pmF7dM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.

To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.

Denis Akhiyarov

unread,
Aug 18, 2015, 7:19:07 AM8/18/15
to sympy
For those who wonder why not use var() especially in combination with parse_expr(), the main reason I found is difficulty dealing with globals() and locals(). Instead of symbol injection, build your own dictionary. This maybe difficult to comprehend coming from interactive environment such as Mathematica.

Aaron Meurer

unread,
Aug 19, 2015, 2:48:20 PM8/19/15
to sy...@googlegroups.com
This is why var() is not recommended except for interactive use. Aside from doing an un-Pythonic thing (creating Python variables without using an assignment), it can only inject variables into the global scope, due to the way variables work in Python. This means that if you use var() inside of a function, the variables will not be local to that function, but injected into the global scope. 

Aaron Meurer

On Tue, Aug 18, 2015 at 5:19 AM, Denis Akhiyarov <denis.a...@gmail.com> wrote:
For those who wonder why not use var() especially in combination with parse_expr(), the main reason I found is difficulty dealing with globals() and locals(). Instead of symbol injection, build your own dictionary. This maybe difficult to comprehend coming from interactive environment such as Mathematica.
--
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+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
Reply all
Reply to author
Forward
0 new messages