On Tue, Apr 22, 2014 at 3:28 PM, Amit Saha <[email protected]> wrote: > Hello all, > > In my early days of exploring SymPy, I found often that one *could* > use strings as arguments to various SymPy's functions, instead of > passing Symbol objects. A case in point is the solve() function. For > example: > >>>> expr = input('Enter an expression: ') > > Enter an expression: x + 3*y - 6 >>>> expr = sympify(expr) >>>> solve(expr, 'y') > # get the solution back. > > However, I have also learned along the way (from you all) that this is > not be relied upon. I shouldn't really use strings here. So, I > thought that (something along these lines) is the more correct > approach:
Yes. Here are some reasons why it is bad https://github.com/sympy/sympy/wiki/Idioms-and-Antipatterns#strings-as-input. Basically, using strings to do symbolic math is like going back to the stone age. You can't do any mathematical manipulation on the string, at least not without making things very complicated. Whenever I see code that does '(' + expression1 + ')' + '+' + '(' + expression2 + ') it makes me want to scream. > > # For example: > for s in expr.atoms(Symbol): > if s.name == 'y': > solutions = solve(expr, s) Why do you need to do this? Just solve(expr, Symbol('y')). Or better yet, if you know the variable name ahead of time, just do y = Symbol('y') once and be done with it (even if you don't know the name, you can do that; the name of the Python variable does not have to match the name of the Symbol that it points to). Aaron Meurer > > > So, I think the latter is the approach I should really follow myself > and also tell others since that is the right thing to do? > Suggestions/comments are very welcome. > > Thanks, > Amit. > -- > http://echorand.me > > -- > 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/CANODV3mDRAJjK%3Dcz8vSetRJQvU90yHA3FeDNuZ1aKqenrcn_Og%40mail.gmail.com. > 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/CAKgW%3D6JHKDRuQB066YNzMk%3DYm6t30xq_BiHHHRttigSQt0y60w%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
