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
x.subs(1/x,y) behavior differs when x is symbol and when x is a function #26405
Comments
Should be controlled with a An alternative would be to make a new function: def mask_inverse_subs(self, old, new):
"""don't let old target 1/old in self
EXAMPLES
========
>>> from sympy.abc import x, y
>>> mask_inverse_subs(2*x + 1/x, x, y)
2*y + 1/x
>>> mask_inverse_subs(2*x + 1/x, 1/x, y)
2*x + y
"""
if not hasattr(old, 'exp'):
base = old
exp = S.One
else:
base = old.base
exp = old.exp.as_coeff_Mul()[0]
d=Dummy()
if exp < 0:
return self.subs(base, 1/base).replace(
lambda p: hasattr(p,'exp') and p.base == base and p.exp.as_coeff_Mul()[0]<0,
lambda p:d**p.exp).subs(1/old, new).xreplace({d: 1/base})
return self.replace(
lambda p: hasattr(p,'exp') and p.base == base and p.exp.as_coeff_Mul()[0]<0,
lambda p:d**p.exp).subs(old, new).xreplace({d: base}) |
if this error only can be seen in reciprocals then we can do this hack I guess. But I think we should also must provide a way to solve more complex versions of substitution. like this,
should return |
In SymPy, the behavior of x.subs(1/x, y) can indeed differ based on whether x is a symbol or a function. When x is a symbol, 1/x represents the inverse of x. However, when x is a function, 1/x represents the functional inverse of x, which may not necessarily be the same as the symbolic inverse.
If old is a function, it computes the functional inverse (1/old(x)) and performs the substitution accordingly. |
The text was updated successfully, but these errors were encountered: