I was tinkering with something and needed a high-order derivative of a function that, when differentiated, needs the product rule (and so, subsequent derivatives - without simplification - become messy).
I was trying to sort of get a nice one liner without typing too many "prime" symbols or doing anything tricky (trying to share code with students who don't all have serious Mathematica experience), but I couldn't really sort out some subtle difference between D and Derivative.
So here's my minimal working example that is confusing me:
f[x_] = (x + 1) (x - 3) (x + 2) (x - 5)/((x + 10) (x - 12) (x + 13));
n = 10;
AbsoluteTiming[Derivative[n][f][0]]
AbsoluteTiming[g[x_] = D[f[x], {x, n}]; g[0]]
AbsoluteTiming[ ToExpression["f" <> StringJoin @@ Table["'", {n}] <> "[0]"] ]
(* Out[]= {0.109908, 618976254473740800} )
( Out[]= {0.001136, 618976254473740800} )
( Out[]= {0.119808, 618976254473740800} *)
(* output with n=20 *)
{4.06785, 4754242157890032492148882597005066240000}
{0.0179687, 4754242157890032492148882597005066240000}
{4.5623, 4754242157890032492148882597005066240000}
I suppose this means the f''''''' construction does about the same as Derivative, but how is it that I'm having so much more efficiency with D instead?
Trace[...]. – Michael E2 Nov 21 '22 at 00:38f'''''''construction does about the same asDerivative". Not "about the same", they're the same i.e.'is the shorthand ofDerivative. Tryf''''''' // Hold // FullForm. – xzczd Nov 21 '22 at 01:46ff = x |-> (x + 1) (x - 3) (x + 2) (x - 5)/((x + 10) (x - 12) (x + 13)); AbsoluteTiming[Derivative[n][ff][0];]takes about 40 seconds on 2GHz laptop! Tested in v12.3. Let's report this to WRI. – xzczd Nov 21 '22 at 09:13System'Private'DerivativeXwhile settingg[x_]=D[f[x],{x,n}]uses\[PartialD]. I'm not really sure how to interpretTraceobjects though. @xzczd How would we do that? I don't know much about that either. – Kellen Myers Nov 23 '22 at 20:04Length@Flatten@Trace[<derivative code>, TraceInternal -> True]is approximately how many steps are taken, although "step" is not a clearly defined unit (steps are not pairwise equivalent). – Michael E2 Nov 23 '22 at 20:42