New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sympify("E") unexpectedly returns something else than Symbol("E") #24397
Comments
>>> print(sympify('A') == Symbol('A'))
True
>>> sympify('E') == Symbol('E')
False
>>> type(sympify('E'))
sympy.core.numbers.Exp1 consider using >>> type(parse_expr('E', local_dict={'E': Symbol('E')}))
sympy.core.symbol.Symbol That said, I've also been bitten by 'E' mapping to Exp1, so I agree that it's unexpected. Perhaps an alternative to |
The main problem is that expression, |
This is a known issue: |
I'd say this should be closed as intended behavior. This is also documented in the sympify docstring (see the "locals" section https://docs.sympy.org/latest/modules/core.html#sympy.core.sympify.sympify). |
But it's a pretty big gotcha. I wonder why E instead of e (or I instead of i) was chosen as the representation. But either way, being so early in the alphabet it is susceptible to confusion.
Maybe something like as |
Because uppercase letters are much less common than lowercase letters for user defined variables. If we used Also note that this gotcha only exists if you're using string parsing. If you define expressions using normal Python syntax where you are explicit about defining symbols, it doesn't happen.
As confusing as this gotcha is, the default behavior of Personally I would have avoided including so many single variable name symbols in SymPy in the first place. IMO we don't really need N (already a method, actually two methods), E (can just be exp(1)), or O (a nice shorthand but could have also have just been called order()). Q and S are convenient shorthands but I don't know if they're worth it. I is the only one that seems worth it to me. But they already exist so that's really a moot point now. I'm not sure if this is a big enough issue to try deprecating any of these names at this point. |
I don't think we should deprecate the names but there should be a straight-simple way to disable all of them in
Everything in the whole of |
Great points added by both of you. Thanks for entertaining the idea of a better way. Note, too, that
>>> from sympy import S
>>> from sympy.abc import _clash1, _clash2, _clash
>>> S("Q & C", locals=_clash1)
C & Q
>>> S('pi(x)', locals=_clash2)
pi(x)
>>> S('pi(C, Q)', locals=_clash)
pi(C, Q) |
Instead, |
expr.evalf get a wrong result.
The text was updated successfully, but these errors were encountered: