I added another note to that wiki page. People often forget that Symbol
names can contain any characters, but anything that uses sympify() will
only parse valid Python variable names into Symbols.   So for example

>>> x = Symbol('2x')>>> solve(x - 1, x)[1]>>> solve(x - 1, '2x')Traceback (most 
>>> recent call last):
  File "<ipython-input-12-71e92ee34319>", line 1, in <module>
    solve(Symbol('2d') - 1, '2d ')
  File 
"/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/solvers/solvers.py",
line 672, in solve
    f, symbols = (_sympified_list(w) for w in [f, symbols])
  File 
"/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/solvers/solvers.py",
line 672, in <genexpr>
    f, symbols = (_sympified_list(w) for w in [f, symbols])
  File 
"/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/solvers/solvers.py",
line 663, in _sympified_list
    return map(sympify, w if iterable(w) else [w])
  File "/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/core/sympify.py",
line 297, in sympify
    raise SympifyError('could not parse %r' % a, exc)SympifyError:
Sympify of expression 'could not parse u'2d'' failed, because of
exception being raised:SyntaxError: invalid syntax (<string>, line 1)

Aaron Meurer

On Wed, Jul 10, 2013 at 9:29 AM, Amit Saha <amitsaha...@gmail.com> wrote:

> On Thu, Jul 11, 2013 at 12:26 AM, Aaron Meurer <asmeu...@gmail.com> wrote:
> > On Jul 10, 2013, at 9:17 AM, Amit Saha <amitsaha...@gmail.com> wrote:
> >
> >> On Wed, Jul 10, 2013 at 1:24 AM, Aaron Meurer <asmeu...@gmail.com>
> wrote:
> >>> Yep, strings often (but won't always!) work as input. See
> >>>
> https://github.com/sympy/sympy/wiki/Idioms-and-Antipatterns#strings-as-input
> .
> >>
> >> Thanks. Yeah, I have learned that from one of my earlier posts to the
> >> forum. In this case it should be fine, I guess.
> >> In fact, I think I made my original query ambiguous: I was really
> >> asking about passing the symbol to be solved for as a string,
> >> *not* the expression.
> >
> > Even then, you should avoid string. Many arguments to many SymPy
> > functions accept strings like this, but it's bad practice to use them.
>
> I agree.
>
> >
> >>
> >> Is this a better approach:
> >>
> >>>>> expr = 'x*2 + 5 + y'
> >>>>> expr=sympify(expr)
> >>>>> for symbol in expr.atoms(Symbol):
> >> ...     if str(symbol)=='x':
> >> ...          solve(expr, symbol)
> >> ...
> >> [-y/2 - 5/2]
> >>
> >> So, I look for the symbol I want to solve for and then invoke solve
> >> using the symbol itself, this having to avoid passing a string.
> >
> > It's better to look at symbol.name. Two symbols can have the same
> > string representation but be unequal.
>
> Indeed, thanks for the pointer to symbol.name
>
>
>
> >
> > Aaron Meurer
> >
> >>
> >> --
> >> 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.
> >> For more options, visit https://groups.google.com/groups/opt_out.
> >>
> >>
> >
> > --
> > 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.
> > For more options, visit https://groups.google.com/groups/opt_out.
> >
> >
>
>
>
> --
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
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.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to