       ND's implementation is slow

• To: mathgroup at smc.vnet.net
• Subject: [mg82251] ND's implementation is slow
• From: "Andrew Moylan" <andrew.j.moylan at gmail.com>
• Date: Tue, 16 Oct 2007 03:28:41 -0400 (EDT)

NumericalCalculus`ND uses Richardson extrapolation to estimate the
derivative, so e.g. ND[Sin[x], x, 5] has the following equivalent one-line
implementation in Mathematica:

InterpolatingPolynomial[{#, (Sin[5. + #] -
Sin[5.]) / #} &  /@ (2.^-Range[0, 6]), 0]
>> 0.2836621854529269

Compare with:

Needs["NumericalCalculus`"]
ND[Sin[x], x, 5]
>> 0.2836621854529268

Interestingly, the one-line implementation in Mathematica is quite a bit
faster than ND! :

Do[
InterpolatingPolynomial[{#, (Sin[5. + #] -
Sin[5.]) / #} &  /@ (2.^-Range[0, 6]), 0]
,
{10000}
] // Timing
>> {1.392, Null}

Do[ND[Sin[x], x, 5], {10000}] // Timing
>> {7.561, Null}

Part of this is due to extra overheads for ND like processing user input and
options. Even taking this into account, however, the
InterpolatingPolynomial-based implementation above is still about twice as
fast. I've modfied my NDerivative package to use this method instead of
NumericalCalculus`ND and I've also added a drop-in replacement for ND,
called FastND.

Do[FastND[Sin[x], x, 5], {10000}] // Timing
>> {4.046, Null}

If you want to use NDerivative or FastND, you can get them from