# Defining an iterated differential operator with compositions

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

op[g^2].


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

op[op[g]]


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.

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] &  *)