In symbolic derivations with Mathematica, one often needs to define an operator with some desired properties, which will be used to stand for a general function or transform.

For example, I would like to define a linear operator myOp[f[x, t]], which has the following basic properties:

  1. Linearity: myOp[a1*f1[x ,t] + a2*f2[x, t]] == a1*myOp[f1[x, t]] + a2*myOp[f2[x, t]] with a1 and a2 being constant;

  2. Commutation with linear differential operators: myOp[D[f[x, t], {x, n}]] == D[myOp[ f[x,t] ], {x, n}];

  3. Inversion: myOp[ myOp[ f[x, t] ] ] == -f[x, t],

such that Mathematica can symbolically evaluate and simplify expressions substituted in. By the way, what necessary and/or useful attributes and/or conditions should also be given to the customized operator?

  • $\begingroup$ There have been multiple questions on this site regarding defining an operator with specified characteristics. See for example the "Related" list to the right. Have you perused those already? They may give you a jump start. $\endgroup$
    – MarcoB
    Mar 11, 2019 at 16:23
  • $\begingroup$ @MarcoB i read a number of related posts, but cannot figure out a proper definition on the property of commutation with linear differential operators without the chain rule applied on f[x,t]. $\endgroup$
    – lxy
    Mar 12, 2019 at 14:23

1 Answer 1


See if the following would work:


myOp[Times[a_., myOp[b_]]] := -a b
myOp[Plus[a_, b_]] := myOp[a] + myOp[b]
myOp[a_ f_[x_, t_]] := a myOp[f[x, t]]
myOp[D[f_[x_, t_], {x_, n_}]] := Derivative[n, 0][myOp[f[a x, t]]][x, t]

For instance:

myOp[4 f[u, p] + 3 g[3 e, f]]

4 myOp[f[u, p]] + 3 myOp[g[3 e, f]]

myOp@myOp[4 f[u, p] + 3 g[3 e, f]]

-4 f[u, p] - 3 g[3 e, f]

myOp[D[f[x, t], {x, 2}]]

Derivative[2, 0][myOp[f[a x, t]]][x, t]

(Thanks to evanb for suggesting a_. within Times)

  • $\begingroup$ Thank you for your reply. I will read the related posts. The property of commutation with differential operators works in an improper way. myOp[D[f[x, t], {x, 1}]] is expected to give Derivative[1, 0][myOp[f[x, t]]][x, t]. That is, no chain rule, just interchange the operators. $\endgroup$
    – lxy
    Mar 12, 2019 at 14:17
  • $\begingroup$ @jsxs I guess we can define it that way already, using Derivative explicitly, instead of D. See if the amended code works better. $\endgroup$
    – MarcoB
    Mar 12, 2019 at 16:24
  • $\begingroup$ You probably want Times[a_., myOp[b_]] to let a default to 1. Also, Plus[a_, b__] := myOp[a] + myOp[Plus[b]] $\endgroup$
    – evanb
    Mar 12, 2019 at 16:28
  • $\begingroup$ @evanb Your first point about the optional default argument in Times is very good, thank you! It saves one definition (changing answer now). On the other hand, it seems to me that your second case is already handled by the myOp[Plus[a_, b_]] definition, which gets applied recursively. $\endgroup$
    – MarcoB
    Mar 12, 2019 at 16:37
  • $\begingroup$ Hmm! Very curious! I know in previous similar applications I have found that kind of rule necessary. I wonder what the difference was. $\endgroup$
    – evanb
    Mar 12, 2019 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.