Looks like I did indeed miss the deliberate mistake. Sorry for being an
idiot and thank you for taking the time.
For clarity, here is what I intended to post:
import sympy as sp
x = sp.Symbol('x')
y = sp.Symbol('x') # deliberate mistake here
x == y # True
x is y # True
I'm aware of Python names not being accessible through introspection. It
still seems as though two calls to "Symbol('x')" should create two distinct
instances of a SymPy symbol, which just happen to share the name. However,
I see the sense in comparing symbols by name.
Has this behaviour changed since a previous version? I am sure that I had
an issue where I had an expression e.g. f=x**2 and differentiating w.r.t a
newly created symbol by the name "x" gave zero, because it was not the same
"x". (This works as expected in recent versions and gives f.diff(x) ==
2*x, so I can't reproduce this half-remembered claimed behaviour.)
On Wednesday, June 3, 2020 at 11:30:31 PM UTC+1, Oscar wrote:
>
> >> >> On Wed, Jun 3, 2020 at 12:53 PM James Bateman <[email protected]>
> wrote:
> >> >>>
> >> >>> I've just discovered a bug in my code which boiled down to the
> following, where a symbol "y" was given the same SymPy name as an existing
> symbol.
> >> >>>
> >> >>> import sympy as sp
> >> >>> x = sp.Symbol('x')
> >> >>> y = sp.Symbol('y')
> >> >>>
> >> >>> x == y # True
> >> >>> x is y # True; expected False
> >> >>> x + y # 2*x; expected x + x (which would have made the bug in my
> code more apparent)
>
> On Wed, 3 Jun 2020 at 23:18, James Bateman <[email protected]
> <javascript:>> wrote:
> >
> > Thank you, but I don't need help debugging my code; I had a typo which
> boiled down to the example I gave. My only question was whether this was
> intended behaviour.
> >
> > Please note the deliberate mistake of "y = Symbol('x')", which you may
> have missed when interpreting my question. True is returned for both "x ==
> y" and "x is y" in SymPy 1.2 (local installation) and SymPy 1.5.1 (
> http://live.sympy.org).
>
> I think you maybe forgot to make the deliberate mistake when posting
> here (see above).
>
> The name of the Python variable does not need to match the name of the
> symbol. Sympy has no way of knowing what name you use for the Python
> variable so if you do
>
> x = Symbol('y')
>
> then there is no way for sympy to know that you assigned the result to
> a variable called x. That's not so much an intended feature but just
> how Python works.
>
> Symbols with the same name (and assumptions) as considered equivalent
> in sympy which is useful in many situations. If you want to create a
> symbol that will only ever compare equal to itself regardless of the
> name of any other symbol then you can use Dummy:
> https://docs.sympy.org/latest/modules/core.html#dummy
>
> Dummy is a bit awkward to use in some situations which is why it isn't
> the default behaviour for symbols in sympy.
>
> --
> Oscar
>
--
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 view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/c915ba41-ee86-404e-af07-689f5e89d174%40googlegroups.com.