4

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?

xzczd
  • 65,995
  • 9
  • 163
  • 468
Kellen Myers
  • 2,701
  • 15
  • 18
  • Try Trace[...]. – Michael E2 Nov 21 '22 at 00:38
  • "I suppose this means the f''''''' construction does about the same as Derivative". Not "about the same", they're the same i.e. ' is the shorthand of Derivative. Try f''''''' // Hold // FullForm. – xzczd Nov 21 '22 at 01:46
  • @xzczd Agreed, but what's the reason for the significant difference in timing? – Ulrich Neumann Nov 21 '22 at 07:20
  • @UlrichNeumann Honestly speaking, I don't know. I didn't know there's such a big difference in performance before reading this question. (Already +1. ) Even more surprising: ff = 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:13
  • @MichaelE2 The trace for the slower ones uses System'Private'DerivativeX while setting g[x_]=D[f[x],{x,n}] uses \[PartialD]. I'm not really sure how to interpret Trace objects though. @xzczd How would we do that? I don't know much about that either. – Kellen Myers Nov 23 '22 at 20:04
  • One of the traces is really, really long compared another one, which explains in part why it takes longer. (Another factor is how long each step takes.) Length@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
  • One can only @ one member in one comment. (Related: https://meta.stackexchange.com/q/43019/284701 ) As to bug reporting, it can be done in: https://www.wolfram.com/support/contact/?topic=feedback (You can find this in Mathematica menu Help -> Give Feedback ) – xzczd Dec 17 '22 at 11:24

0 Answers0