# How to define derivative operation of a series acting on normal function

In some cases, I have to do derivarive of higher order acting on some function successively. Here is a toy example:

I have a function named myfunc:

myfunc[x_] := c1*x + c2*x^2 + c3*x^3;  (**This would be a series of arbitrary expansion**)

I want to define an operation named operation which is a series as below:

operation[y_] := A1*y + A2*y^2 + A3*y^3; (**this would be also series of arbitrary expansion**)

At this point operation seems like a polynomial but now I want to really define what it is. Like now I want to relate y^n as a derivative of order n.

op2[x_] := operation[y] /. y^n_ -> D[#, {x, n}] &; (**Here I replace y^n as derivative of order n**)

Lastly I want to apply it on the function from the left:

result = op2[x]@myfunc[x]

expected output for this example:

6 A3 c3 + A2 (2 c2 + 6 c3 x) + A1 (c1 + 2 c2 x + 3 c3 x^2)

What it produces now is unevaluated expression.

However this works only when operation has a single term in it. Any idea how to overcome? In general how do I define such a custom operation?

Another extention (may be more complicated) would be to relate y^n as (func[x] D[#,x,1])^n apllied successively from left on myfunc, i.e. say,

y^3 myfunc[x] == func[x]*D[  func[x]*D[  func[x] D[myfunc[x],x]  ,x] ,x]

where func[x] can be any polynomial of x like func[x] = x^2 or it may be any other operation for example a Derivative again func[x] = D[#,x]&.

In general how do I define such operation?

edit:: A workaround for the first toy example would be like this:

dxx[x_, n_] := HoldForm[D[#, {x, n}] ];
myfunc[x_] := c1*x + c2*x^2 + c3*x^3;

operation[y_] :=  A1*y + A2*y^2 + A3*y^3;

op2[x_] := operation[y] /. {y^n_ -> dxx[x, n], y -> dxx[x, 1]};

result = ReleaseHold[Evaluate[(op2[x])] & [myfunc[x]]]

output:

6 A3 c3 + A2 (2 c2 + 6 c3 x) + A1 (c1 + 2 c2 x + 3 c3 x^2)
• I am really confused trying to understand what you want to achieve. Could you explain the ultimate goal, give an example of a situation in which your current solution does not work, and show the output you would like to obtain if it worked? May 1, 2020 at 15:31
• @MarcoB, see the second edit. May 1, 2020 at 16:03

You can do it with a delayed replacement:

myfunc[x_] = c1*x + c2*x^2 + c3*x^3;
operation[y_] = A1*y + A2*y^2 + A3*y^3;

op2[F_] := operation[y] /. y^n_. :> D[F, {x, n}]
op2[myfunc[x]]
(*    6 A3 c3 + A2 (2 c2 + 6 c3 x) + A1 (c1 + 2 c2 x + 3 c3 x^2)    *)

Notice the delayed replacement :> and the default pattern n_. both playing important roles.

If you want to make the variable $$x$$ explicit,

op2[F_, X_] := operation[y] /. y^n_. :> D[F, {X, n}]

op2[myfunc[x], x]
(*    6 A3 c3 + A2 (2 c2 + 6 c3 x) + A1 (c1 + 2 c2 x + 3 c3 x^2)    *)

### Update

The second example can be constructed with the auxiliary recursive function

myfunc[0, x_] = myfunc[x];
myfunc[n_, x_] := x^2 * D[myfunc[n - 1, x], x]

operation[y] /. y^n_. :> myfunc[n, x]
(*    A1 x^2 (c1 + 2 c2 x + 3 c3 x^2) + A2 x^2 (x^2 (2 c2 + 6 c3 x) + 2 x (c1 + 2 c2 x + 3 c3 x^2)) +
A3 x^2 (x^2 (6 c3 x^2 + 4 x (2 c2 + 6 c3 x) + 2 (c1 + 2 c2 x + 3 c3 x^2)) +
2 x (x^2 (2 c2 + 6 c3 x) + 2 x (c1 + 2 c2 x + 3 c3 x^2)))    *)

There's lots of room for improvements, such as memoization, variable localization, nesting instead of recursion, etc.

• This is nicer. What about for the second part of the question? If I want the replacement to be y^n_. :> (x^2*Derivative )^n acting successively from the left on myfunc? May 1, 2020 at 16:22
• Note that now I can not define a simple replacement like y^n_. :> D[F, {X, n}] since now the derivative from the second time onwards will also act on the extra x^2 in front of it. May 1, 2020 at 16:26