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 On Wed, Jan 28, 2015 at 4:31 PM, Aaron Meurer <[email protected]> wrote: > 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 > > On Wed, Jan 28, 2015 at 4:28 PM, Denis Akhiyarov < > [email protected]> wrote: > >> 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 >> >> -- >> 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 [email protected]. >> To post to this group, send email to [email protected]. >> 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 >> <https://groups.google.com/d/msgid/sympy/26d01fa4-15fd-45da-b043-9197410f0286%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > -- > 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 > [email protected]. > To post to this group, send email to [email protected]. > 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/CAKgW%3D6%2B5Sqnx%2B%2BPhnScj-S34rfAZQB_BuJK6RTRKPgXGBBvvJA%40mail.gmail.com > <https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2B5Sqnx%2B%2BPhnScj-S34rfAZQB_BuJK6RTRKPgXGBBvvJA%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To post to this group, send email to [email protected]. 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/CALxxJLRiiGRK1ZTPdhP1N2QRFLyz3phDQ6D9g0%2BRZ109%3D38C2g%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
