Reputation: 30116
Is it possible to reveal all symbolic variables that are involved in a sympy expression?
Here is some sample code that could be potentially quite confusing:
from sympy import *
from sympy.stats import Normal, sample, variance
sigma_eps = symbols('sigma_eps')
eps = Normal("eps", 0, sigma_eps)
sigma_any = eps + 1
sigma_1 = (eps+1).subs({sigma_eps:1})
sigma_10 = (eps+1).subs({sigma_eps:10})
If you print sigma_any, sigma_1
or sigma_10
they all look the same. They will all tell you that their value is eps + 1
. But actually, the values are completely different distributions.
Is it possible to reveal what random variables have been used and already substituted for an expression?
Upvotes: 0
Views: 114
Reputation: 19093
It is a general rule (not always followed) that the string form of an expression should be copy and pastable to recreate the object. This case is an exception and an issue at https://github.com/sympy/sympy/issues could be opened.
You can tell they are not the same if you inspect the srepr` forms:
>>> srepr(sigma_1)==srepr(sigma_10)
False
Also, free_symbols
does not show what you want, but the difference between atoms before and after substituion will reveal what has been changed:
>>> sigma_any.free_symbols
{eps}
>>> sigma_any.atoms()
{0, 1, eps, sigma_eps}
>>> sigma_1.atoms()
{0, 1, eps}
Note that sigma_eps
is missing in the last output.
Upvotes: 1