       Re: Defining a derivative that distributes for a function

• To: mathgroup at smc.vnet.net
• Subject: [mg64257] Re: [mg64222] Defining a derivative that distributes for a function
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Wed, 8 Feb 2006 03:53:50 -0500 (EST)
• References: <200602070835.DAA29828@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```On 7 Feb 2006, at 08:35, Andres Corrada-Emmanuel wrote:

> Hello,
>
> I'm trying to define a derivative for a function that distributes:
>
> D[f[expr_],x_] ^:= f[D[expr,x]]
>
> This gives me:
>
> D[f[x^2],x] = f[2x]
>
> and
>
> D[f[x^4],x] = f[4x^3]
>
> But D[f[x^2] + f[x^4],x] = 2xf'[x^2] + 4x^3f'[x^4] instead of the
> desired:
>
> D[f[x^2] + f[x^4],x] = f[2x] + f[4x^3]. Why? And how do I get the
> desired behaviour.

The reason is that the pattern in the rule you have defined does not
match the expression you are differentiating so the built-in rules
for D (distributivity with respect to addition and the chain rule)
are triggered off and once they are applied it is too late to apply

At the moment the only way I can see to get the desired behaviour is
by using a slightly unpleasant trick, which involoves a global
variable and Unprotecting D:

In:=
flag = True;

In:=
Unprotect[D];

In:=
D[expr_, x_] /; flag := ReleaseHold[Block[{flag = False},
D[expr /. f[t_] :> Hold[f[t]], x] /. HoldPattern[D[Hold[f[v_]],
u_]] :> f[D[v, u]]]]

In:=
Protect[D];

In:=
D[f[x^2], x]

Out=
f[2*x]

In:=
D[f[x^2] + f[x^4], x]

Out=
f[2*x] + f[4*x^3]

Andrzej Kozlowski

>
> --------------------------------------