I want to remove all occurrences of “a” in expression “exp” except for the first one, which I want to keep.
To do that,
I have tried
X = symbols(‘X’)
<<replace first occurrence of a by X>>
replace all remaining occurrences by 0
replace X by a
For the second statement, an Internet search suggested replace, like this:
X = symbols(‘X’)
exp = exp.replace(a, X, 1) # maximum of one replacement
exp = exp.subs(a, 0)
exp = exp.subs(X, a)
Unfortunately, this version of replace doesn’t work, and I have not found any way to do it with subs of xreplace. This post tells me that subs and replace have kwargs, but I haven’t found any documentation on that.
https://stackoverflow.com/questions/56584025/sympy-subs-vs-replace-vs-xreplace