-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Indexed.free_symbols includes itself if the base has free symbols #14899
Comments
I'm sorry to hear that. The reasoning behind this change was the introduction of expr.free_symbols - expr.expr_free_symbols Maybe this could work as a stopgap measure? The point is that See also: |
Thank you for the reply. I see your point.
I use |
It's available starting from 1.2
Have you tried |
Wouldn't Also atoms is ~3 times slower. Doesn't matter in my case (premature optimization) and its not even an order of magnitude but I would have guessed that free_symbols is the more complicated Task. Edit: |
This behavior is also giving troubles to me in #21425: >>> f, g = symbols('f g', cls=Function)
>>> f(x).func
f
>>> f(x).free_symbols
{x}
>>> f(x).subs(f,g)
g(x)
>>> f, g = symbols('f g', cls=IndexedBase)
>>> f[x].base
f
>>> f[x].free_symbols
{f, x, f[x]} <---------------------------- why is `f` included
>>> f[x].subs(f,g)
g[x] Whereas a function does not report its Ways to see symbols in an expression tree: >>> f[x].atoms(Symbol)
{f, x}
>>> f[x].expr_free_symbols
{f[x]}
>>> f[x].free_symbols
{f, x, f[x]} Is the reason that |
It's a bit fiddly here to define what we mean by a "free symbol". A
The idea is that Right now we couldn't return |
I believe that allowing Indexed to list itself as a free symbol is a design mistake that should be reverted. It blurs the line between symbol and generator (a feature that I am proposing): >>> eq=exp(x)+1
>>> generators(eq)
{exp(x)}
>>> eq.free_symbols
{x}
>>> from sympy.stats import Normal
>>> eq=Normal('X', 2, 4)
>>> generators(eq)
{X}
>>> eq.free_symbols
{X}
>>> eq=MatrixSymbol("A",2,2)[1,1]
>>> generators(eq)
{A[1, 1]}
>>> eq.free_symbols
{A}
>>> Indexed('x',0)+1
x[0] + 1
>>> eq=_
>>> generators(eq)
{x[0]}
>>> eq.free_symbols
{x, x[0]} <---- should just be {x} |
Wouldn't this imply that you can no longer substitute I think this would be useful, but what does "generator" mean in mathematical terms? I feel that the underlying problem with these issues is that a computer science concept is used that only maps to its mathematical counterpart in some contexts but not all (similar to classes being used as functions). |
Something doesn't need to be in In [1]: e = exp(x) + log(y)
In [2]: e
Out[2]:
x
ℯ + log(y)
In [3]: e.free_symbols
Out[3]: {x, y}
In [4]: e.subs(log(y), cos(t))
Out[4]:
x
ℯ + cos(t) |
From the docstring,
I'm still thinking about what to do with |
Upgrading to version 1.2 breaks my code because
Indexed.free_symbols
now includes{self}
if the base has any free symbols. Is there any reason for this? I think it's quite unintuitive asa[i]
is not a free symbol ifa
andi
are defined.My fix is to filter the free_symbols and drop all Indexed instances. Is there a better way to get te behaviour i need?
Comits that introduced the change:
f1dcc89
327421b
@Upabjojr
The text was updated successfully, but these errors were encountered: