# Defining an operator with given properties

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?

• 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. Mar 11, 2019 at 16:23
• @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].
– jsxs
Mar 12, 2019 at 14:23

See if the following would work:

ClearAll[myOp]

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)

• 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.
– jsxs
Mar 12, 2019 at 14:17
• @jsxs I guess we can define it that way already, using Derivative explicitly, instead of D. See if the amended code works better. Mar 12, 2019 at 16:24
• You probably want Times[a_., myOp[b_]] to let a default to 1. Also, Plus[a_, b__] := myOp[a] + myOp[Plus[b]] Mar 12, 2019 at 16:28
• @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. Mar 12, 2019 at 16:37
• Hmm! Very curious! I know in previous similar applications I have found that kind of rule necessary. I wonder what the difference was. Mar 12, 2019 at 18:13