Message Boards Message Boards

0
|
11103 Views
|
7 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Force to replace the derivatives of a function with given expression?

Hi everybody,

I have a function defined by $F = \log(f)$, so that $f' = fF'$. Now I want to compute the derivatives of $f$ in terms of $f$ and the derivatives of $F$ (instead of those of $f$). In other words, I would like to compute

$f'' = fF'^2 + fF'',$

$f''' = fF'^3 + 3fF'F'' + fF''',$ etc.

The problem is that, for example, when computing $f''$, Mathematica computes $f'F' + fF''$ instead of $fF'^2+fF''$. And I don't really know how to tell it to always replace the derivatives of $f$ while keeping the derivatives of $F$. That is, I don't how to force Mathematica to always replace any derivatives of a given function $f$ with a given expression $fF'$.

Hope that was clear! Clément

POSTED BY: Clément Justin
7 Replies

This is more difficult to explain:

myDerivationRule = 
  Derivative[i__ /; Total[{i}] == 1][f][x__] :> 
   f[x] Derivative[i][F][x];
D[f[x1, x2], x2] == (D[f[x1, x2], x2] /. myDerivationRule)
D[%, x1] /. myDerivationRule
D[%, x1] /. myDerivationRule
POSTED BY: Gianluca Gorni

Hi everyone,

I have another question. Now I'm trying to generalize it this way. I still have $F = \log(f)$, but $f$ and $F$ depend on many variables $t_i$ whith $i\geq 1$. And I want to compute for example

$\displaystyle \frac{\partial f}{\partial t_1} = f \frac{\partial F}{\partial t_1}, $

$\displaystyle \frac{\partial^2 f}{\partial t_1\partial t_2} = f \frac{\partial F}{\partial t_1} \frac{\partial F}{\partial t_2} + f\frac{\partial^2 F}{\partial t_1\partial t_2}, $

I tried to adapt the code but I couldn't do it :/

Thanks, Clément

POSTED BY: Clément Justin

Hi Celement,

I think the best way is to use Faa Di Bruno's formula, which gives a nice generalization of the chain rule. Compare the following one-line calc. codes:

DerivativeList1[n_] :=  With[{rep = Rule[D[f[x], {x, #}] /. x -> F[x], 1] & /@ Range[n]},
  (D[f[F[x]], {x, #}] & /@ Range[n]) /. rep]
DerivativeList2[n_] :=  Table[BellY[i, j, D[F[x], {x, #}] & /@ Range[n]], {i, 1, n}, {j, 1, i}]    

Short of doing the recursive analysis, you can see that these are ouput equivalent by writing a logical function that applies SameQ to the output and returns true for any $n$. You should also check the timing statistics:

t1 = AbsoluteTiming[DerivativeList1[#];][[1]] & /@ Range[30]
t2 = AbsoluteTiming[DerivativeList2[#];][[1]] & /@ Range[30]

which show that BellY does much better than the naive implementation. However, in other threads I have shown that the current implementation of BellY is slow, and that's true again here. Using the recursion relations we get more time savings

DerivativeList3[n_] := Table[B2[i, j], {i, 1, n}, {j, 1, i}]
t3 = AbsoluteTiming[
     Clear@B2;
     B2[0, 0] = 1;
     B2[n_ /; n > 0, 0] := 0;
     B2[0, k_ /; k > 0] := 0;
     B2[n_ /; n > 0, k_ /; k > 0] := 
      B2[n, k] =        Total[Binomial[n - 1, # - 1] D[F[x], {x, #}] B2[n - #, 
            k - 1] & /@ Range[1, n - k + 1]];
     DerivativeList3[#];][[1]] & /@ Range[30]
ListLinePlot[{t1, t2, t3}, PlotStyle -> {Red, Orange, Green}]

time test

and the output up to $n=5$ :

DerivativeList1[5] // TableForm
DerivativeList2[5] // TableForm
DerivativeList3[5] // TableForm

Output

Brad

Edit: Code from Gianluca Gorni seems to be time-equivalent to the naive implementation above:

myDerivationRule = f'[x_] :> f[x]*F'[x];
AbsoluteTiming[  NestList[D[#, x] /. myDerivationRule &, f[x]*F'[x], 30] /. f[x] -> 1;][[1]]
Out[]=3.14197(s)

Oh, wow! An unexpected approximation of Pi.

POSTED BY: Brad Klee

Well thank you very much!

Clément

To reduce confusion, consider a simplified version:

myDerivationRule = f'[x] -> f[x]*F'[x];
D[f[x]*F'[x], x] /. myDerivationRule

The arrow -> stands for a replacement rule: it instructs to replace f'[x] with f[x]*F'[x]. We store this replacement rule in the symbol myDerivationRule, so that it is easier to apply repeatedly.

Then consider f[x]*F'[x] and take its derivative with respect to x, i.e., D[f[x]*F'[x], x]. We get

f'[x] F'[x] + f[x] F''[x]

We want to replace f'[x] in this expression with f[x]*F'[x]. This is done with our replacement rule, which is applied with the shorthand slash-dot /.

D[f[x]*F'[x], x] /. myDerivationRule

After the replacement is done, we get the desired formula f[x]F'[x]^2+f[x]F''[x].

POSTED BY: Gianluca Gorni

Hi Gianluca, well it seems to work fine! But I don't even understand the code... Could you explain it to me? Like, what is the meaning of the $==$ sign here? I thought it was for conditions. And what does the $:>$ mean? Is it like a rule and a definition at the same time?

Thanks! Clément

POSTED BY: Clément Justin

You can try something like this:

myDerivationRule = f'[x_] :> f[x]*F'[x];
f'[x] == f[x]*F'[x]
D[%, x] /. myDerivationRule
D[%, x] /. myDerivationRule
D[%, x] /. myDerivationRule
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract