On Wed, Apr 23, 2014 at 8:01 AM, Aaron Meurer <asmeu...@gmail.com> wrote:
> On Tue, Apr 22, 2014 at 4:58 PM, Amit Saha <amitsaha...@gmail.com> wrote:
>> On Wed, Apr 23, 2014 at 7:21 AM, Aaron Meurer <asmeu...@gmail.com> wrote:
>>> On Tue, Apr 22, 2014 at 3:28 PM, Amit Saha <amitsaha...@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?
>
> 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.

Thanks a lot, Aaron. That clears my doubts.

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+unsubscr...@googlegroups.com.
To post to this group, send email to sympy@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/CANODV3nmmCnv2Vt%3Dr74C_Z6QhYCBSYn76Gx_Nj0zc%2BNROHQhjw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to