Hi,
I have been experimenting with various finite difference algorithms for 
computing derivatives and I found and implemented a general algorithm for 
computing arbitrarily high derivative to arbitrarily high order [1].  So 
for anyone interested in playing with it I wrapped it up in a small package.

I would also like to ask for your an advice on how to improve the 
performance of the function fdd, so far for a first derivative on a three 
point stencil (on a non-uniformly spaced mesh) it takes ten times as much 
time to compute the derivative, when compared to a hand crafted 
implementation of the same function.  For a comparison you can call

using FiniteDifferenceDerivatives
x=linspace(0,1,1000)
f=sin(x)
df=copy(x)
@elapsed for i = 1:1000; fdd(1,3,x,f); end  # general function => ~0.5 s
@elapsed for i = 1:1000; fdd13(x,f); end  # hand crafted => ~0.05 s

Obviously, most of the time is spent inside fddcoeffs.  One way I could 
increase the performance is to cache the results of fddcoeffs (its result 
depends only on the mesh).  In my use case however, the mesh changes often 
and fddcoeffs has to be called every time fdd is called.  I also tried to 
replace x[i1:i1+order-1] with view(x,i1:i1+order-1) in the call to 
fddcoeffs (with view from ArrayViews), but it didn't result in much of an 
improvement.  I also played with swapping the indexing order of c in 
fddcoeffs, but to no avail (maybe because the typical size of the matrix c 
is small (3x3 or similar)).  Maybe I should try to inline the fddcoeffs 
into the main body of fdd (I did this in my earlier version, and it 
actually resulted in an improvement, but it looked kind of less elegant)?  
Any advice (or pull requests) would be welcome.

[1] https://github.com/pwl/FiniteDifferenceDerivatives.jl

Cheers,
Paweł

Reply via email to