Operations on ComposedFunctions using Symbolics.jl and in Julia in general

I am using Symbolics.jl for a project, and would like to play around with the Operator structure.
In particular, I would like to build operators such as a Lie operator, defined as
:f: = \frac{\partial f}{\partial x_1} \frac{\partial}{\partial x_2} - \frac{\partial f}{\partial x_2} \frac{\partial}{\partial x_1} .
However, I have issues coming from the fact that any product involving a Differential operator becomes a composition and that Julia ComposedFunctions cannot be summed/subtracted…
The solution I am employing now is to create Operators substructures for every needed operation (\pm, *, /, \circ…). Here is an example

struct Sum <: Operator
    O1 :: Operator
    O2 :: Operator
(O::Sum)(x::Num) = O.O1(x) + O.O2(x)
Base.:+(O1::Operator, O2::Operator) = Sum(O1, O2)

I have the impression however that these recursive calls to operators is causing severe performance issues, in particular when I exponentiate the Lie Operator with
e^{:f:} = \sum_{k=0}^N \frac{{:f:}^k}{k!}
Is there a clever, more efficient way to define these operations?

1 Like