You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Today, I stumbled into a very subtle pitfall that might happen to you when writing more complex stuff with sympy (pythons symbolic mathematics library).
Maybe you define some symbols multiple times (e.g. because some live in a class, and some in the main program using that class). It works fine: same symbols will be recognized by sympy to be the same.
But here is the pitfall: if you pass different options when defining the symbols (e.g. positive=True for one definition, omitted for the other), you end up with sympy treating those two character-wise identical symbols as differing.
This will very probably lead to trouble.
In my case, sympy simply couldn't find a solution to an equation - because internally there were two symbols for what I expected to be the same.
Maybe there is a way to warn users or rise an error?
The text was updated successfully, but these errors were encountered:
This is expected behavior. Ideally, every instance should be treated completely different even if they have exactly the same attributes. Parts of the code sometimes use the same name for variables like '_n' which would lead to bugs if they were treated the same. This is part of the reason why Dummy was created I guess.
Here is how I go about sorting it out:
fromsympyimport*x=symbols('x')
defmultiply_by_x(expr: Expr):
# this will be a function in a different# class that cant see x.# now we try to extract the x from expr# we need to know the string name of x thoughx= [symforsyminexpr.free_symbolsifstr(sym) =='x'][0]
# this x is now the same as the x outside the function# do stuff with the functionreturnx*exprprint(multiply_by_x(sin(x)))
There are probably better ways of doing this but this seems the most straight-forward to me.
Otherwise you could always have multiply_by_x(expr, x). That way you pass all your symbols in as arguments. Now that I'm typing this, this second option seems far better.
Today, I stumbled into a very subtle pitfall that might happen to you when writing more complex stuff with sympy (pythons symbolic mathematics library).
Maybe you define some symbols multiple times (e.g. because some live in a class, and some in the main program using that class). It works fine: same symbols will be recognized by sympy to be the same.
But here is the pitfall: if you pass different options when defining the symbols (e.g. positive=True for one definition, omitted for the other), you end up with sympy treating those two character-wise identical symbols as differing.
This will very probably lead to trouble.
In my case, sympy simply couldn't find a solution to an equation - because internally there were two symbols for what I expected to be the same.
Maybe there is a way to warn users or rise an error?
The text was updated successfully, but these errors were encountered: