I'm a bit new to Mathematica so this question might be a bit basic. Still, I could not find an answer.

I wish to define a symbolic differential operator. Currently, I do something like the following:

op[g_] = g'

Now, I would like this operator to work well with compositions. For example I would like


to return $2g'*g'$. However I only get $(g^2)'$.

Another thing I'm hoping will work is to compose the operator with itself, so that


will return $g''$.

I understand that the problem is that the argument 'g' was never defined to be a 'function of x'. But I'm sure how to define everything correctly.

Any help would be appreciated.


2 Answers 2


You could use the function D:

op[g_] := D[g, x]

Then your result would be:

In[1]:= op[g[x]^2]
Out[2]= 2 g[x]g'[x]

And also

In[2]:= op[op[g[x]]]
Out[2]= g''[x]

Although I don't really see the point if the function D already exists.


The problem is that g^2 does not represent the square of a function, that is, (g^2)[x] is not g[x]^2. The system is not set up to operate that way. A similar thing could be said about 2 g * g', namely, that * does not perform the multiplication of functions. To operate with functions, one can sometimes use Composition (@*) or pure functions. Because of the way Derivative works, we are likely to end up with a pure-function expression anyway.

op[g_] := g'

To get g^2, compose Power[#, 2]& and g:

op[(#^2 &) @* g]
(*  2 g[#1] Derivative[1][g][#1] &  *)

Your Answer

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

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