Advise regarding using strings in SymPy

17 views
Skip to first unread message

Amit Saha

unread,
Apr 22, 2014, 4:28:19 PM4/22/14
to sy...@googlegroups.com
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:

# For example:
for s in expr.atoms(Symbol):
if s.name == 'y':
solutions = solve(expr, s)


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

Aaron Meurer

unread,
Apr 22, 2014, 5:21:55 PM4/22/14
to sy...@googlegroups.com
On Tue, Apr 22, 2014 at 3:28 PM, Amit Saha <amits...@gmail.com> 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 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/CANODV3mDRAJjK%3Dcz8vSetRJQvU90yHA3FeDNuZ1aKqenrcn_Og%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Amit Saha

unread,
Apr 22, 2014, 5:58:39 PM4/22/14
to sy...@googlegroups.com
On Wed, Apr 23, 2014 at 7:21 AM, Aaron Meurer <asme...@gmail.com> wrote:
> On Tue, Apr 22, 2014 at 3:28 PM, Amit Saha <amits...@gmail.com> 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.

Thanks, Aaron.

I will take a look at that link. I am sure you have referred to it
earlier as well, but I will try to take a deeper look.

>
>>
>> # 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).

Looks like i had a confusion here, i thought the *same* symbol object
as in the sympified expression had to passed to solve(). That is,
since Symbol('y') creates a different object, it would not work. But
this works. This is certainly simpler than doing all the above. Is
this behavior always true or there are any gotchas?

Best,
Amit.


--
http://echorand.me

Aaron Meurer

unread,
Apr 22, 2014, 6:01:58 PM4/22/14
to sy...@googlegroups.com
No, symbols are compared by their name. Symbol('y') is the same as
Symbol('y'). This is only untrue of Dummy, which intentionally is
different for each instance.

The gotcha is if the symbol has assumptions, those are also taken into
consideration. So for instance Symbol('y') is not the same as
Symbol('y', positive=True). But for parsed input this should not be an
issue, since all parsed symbols are created without assumptions.

Aaron Meurer

>
> Best,
> 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 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/CANODV3n09ys0ZGnqnL0-devuOyNTvw1HJVLeYHRst%3DPJFyF5wg%40mail.gmail.com.

Amit Saha

unread,
Apr 22, 2014, 6:09:37 PM4/22/14
to sy...@googlegroups.com
Thanks a lot, Aaron. That clears my doubts.
Reply all
Reply to author
Forward
0 new messages