Skip functions or derivatives in cse

27 views
Skip to first unread message

Jason Moore

unread,
Jul 30, 2025, 3:33:02 AMJul 30
to sympy
Hi,

I tried:

In [18]: import sympy as sm

In [19]: a, b, c = sm.symbols('a, b, c')

In [20]: f = sm.Function('f')(a)

In [21]: expr = f + f.diff() - f.diff()/(f + f.diff()) - a*b + (a*b)**2

In [22]: sm.cse(expr)
Out[22]:
([(x0, f(a)), (x1, Derivative(x0, a)), (x2, x0 + x1)],
 [a**2*b**2 - a*b - x1/x2 + x2])

In [23]: sm.cse(expr, ignore=[f.diff()])
Out[23]:
([(x0, f(a)), (x1, Derivative(x0, a)), (x2, x0 + x1)],
 [a**2*b**2 - a*b - x1/x2 + x2])

The outcome I desire is:

([(x0, f(a)), (x1, Derivative(f(a), a)), (x2, x0 + x1)],
 [a**2*b**2 - a*b - x1/x2 + x2])

or:

([(x1, Derivative(f(a), a)), (x2, f(a) + x1)],
 [a**2*b**2 - a*b - x1/x2 + x2])

that is, that the functions or derivatives are treated like symbols.

The ignore flag is cse does not seem to do that.

Is there a way to use the pre/post processors in cse to do this?

Jason

Aaron Meurer

unread,
Jul 30, 2025, 1:55:29 PMJul 30
to sy...@googlegroups.com
It might be possible to work around this with the optimizations flag,
but I'd say cse should just be updated to treat derivatives as
separate symbols.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/sympy/CAP7f1AjbULou2oaD3Ras8zUb78%3DM3vtorc%3DshSaG-EzMeOogFQ%40mail.gmail.com.

Björn Dahlgren

unread,
Aug 1, 2025, 8:31:52 AMAug 1
to sympy

On Wednesday, 30 July 2025 at 19:55:29 UTC+2 asme...@gmail.com wrote:
It might be possible to work around this with the optimizations flag,
but I'd say cse should just be updated to treat derivatives as
separate symbols.

I agree that this would be preferable. In the mean time I typically find all Derivative and AppliedUndef instances, create dummies for them and substitute them before cse (substituting the derivatives first), and then do a final back-substitution. This is probably what we would do internally.
Reply all
Reply to author
Forward
0 new messages