I don't reproduce your example when running cse on the reconstructed Matrix. Could you paste of reproducing snippet?
```
>>> r
[(z0, 1/h), (z1, wxixi), (z2, wyiyi), (z10, -1/h**2)]
>>> str(m)
'Matrix([[z0, 0, -1, 0, -z0, 0, 0, 0, z10*(xi - xp)], [0, z0, 0, -1, 0, -z0, 0, 0, z10*(yi - yp)], [(-2*z1*z2**2 - 2.5*z2)*Derivative(z1, xi), (-2*z1**2*z2 - 2.5*z1)*Derivative(z2, yi), 2*uxi, 2*uyi, 0, 0, 0, 0, 0]])'
>>> cse(m.xreplace(dict(r)))
([(x0, 1/h), (x1, -x0), (x2, h**(-2))], [Matrix([
[ x0, 0, -1, 0, x1, 0, 0, 0, -x2*(xi - xp)],
[ 0, x0, 0, -1, 0, x1, 0, 0, -x2*(yi - yp)],
[(-2*wxixi*wyiyi**2 - 2.5*wyiyi)*Derivative(wxixi, xi), (-2*wxixi**2*wyiyi - 2.5*wxixi)*Derivative(wyiyi, yi), 2*uxi, 2*uyi, 0, 0, 0, 0, 0]])])
```
As to how a symbol can show up as a replacement: it can happen if a non-symbol collapses to a symbol when evaluating. I am looking at a fix to keep that from inadvertently be happening with an UnevaluatedExpr.
Until I see the reproducer I would just suggest a masking operation on your original matrix M:
from sympy.solvers.solveset import _masked
M, restore = _masked(M, Derivative) # masked handles nested Derivatives
reps, [M] = cse(M)
for d, e in restore:
reps = [(s, rhs.xreplace({d: e})) for s, rhs in reps]
M = M.xreplace({d: e})
The `ignore` tells what variables (symbols) should not appear in any identified patterns.
>>> eq=exp(y) + Derivative(f(x) + Derivative(f(x), x), x) + 1/(exp(y) + 1)
>>> cse(eq)
([(x0, exp(y)), (x1, f(x))], [x0 + Derivative(x1 + Derivative(x1, x), x) + 1/(x0 + 1)])
>>> cse(eq,ignore=[x])
([(x0, exp(y))], [x0 + Derivative(f(x) + Derivative(f(x), x), x) + 1/(x0 + 1)])
>>> cse(eq,ignore=[y])
([(x0, f(x))], [exp(y) + Derivative(x0 + Derivative(x0, x), x) + 1/(exp(y) + 1)])
Why don't you want Derivatives in the cse? Is it because you then have to special case replacement of them before doing doit? That could be solved by wrapping Derivatives in Subs as in Derivative(f(x), x) -> Subs(Derivative(x0, x), x0, f(x))
>>> Subs(Derivative(x0, x), x0, f(x))
Subs(Derivative(x0, x), x0, f(x))
>>> _.doit()
Derivative(f(x), x)
/c