Lost me a bit at Im(f(x+ih))/h near the end (that's a fancy I that android FF won't paste). Can anyone explain where 'm' came from? Or is 'Im' just a fn returning the imaginary part of its argument? EDIT: reading the code following, it's clear that Im is just that.
Hack the derivative
11–20 of 36 posts
Re: Hack the derivative
#12I DO think Python libraries can do this already.
Applying the Cauchy–Riemann equations to essentially take finite differences in the imaginary direction is an interesting trick I hadn't seen before, so I appreciated an article introducing it.
Edit: A separate python package, Numdifftools, does seem to support complex-step differentiation https://pypi.python.org/pypi/Numdifftools
Re: Hack the derivative
#13I'm not sure it is so surprising. In Nick Trefethen's Numerical Analysis entry in The Princeton Companion to Mathematics, he notes:
"Thus, on a computer, the interval [1 , 2] , for example, is approximated by about 10^16 numbers. It is interesting to compare the fineness of this discretization with that of the discretizations of physics. In a handful of solid or liquid or a balloonful of gas, the number of atoms or molecules in a line from one point to another is on the order of 10^8 (the cube root of Avogadro’s number). Such a system behaves enough like a continuum to justify our definitions of physical quantities such as density, pressure, stress, strain, and temperature. Computer arithmetic, however, is more than a million times finer than this. Another comparison with physics concerns the precision to which fundamental constants are known, such as (roughly) 4 digits for the gravitational constant G, 7 digits for Planck’s constant h and the elementary charge e, and 12 digits for the ratio μ_e/μ_B of the magnetic moment of the electron to the Bohr magneton. At present, almost nothing in physics is known to more than 12 or 13 digits of accuracy. Thus IEEE numbers are orders of magnitude more precise than any number in science. (Of course, purely mathematical quantities like π are another matter.)" http://people.maths.ox.ac.uk/trefethen/NAessay.pdf
Re: Hack the derivative
#14I don't see any mention of automatic differentiation which I thought was the dominant technique in this area. Is there an advantage to the method described in OP (other than mathematical cuteness)?
The only reason to use the complex step method is if you're using legacy languages where it's difficult to implement dual numbers but you have good support for complex numbers. I don't think anybody should be using the complex step method in new applications.
Some reading ~ http://aero-comlab.stanford.edu/Papers/martins.aiaa.01-0921....
Re: Hack the derivative
#15Take a small interval around your smooth function that includes the point where you want to compute the derivative. Joint the right and left ends by just reflecting itself, so that you have a differentiable periodic function. Now sample that at a suitable rate, take its FFT, derive the FFT of the derivative by multiplying it with 2\pi I. Take the inverse FFT and evaluate it at the point of interest. May be useful if you want to compute the derivative at many points, but seems wasteful otherwise.
One of course has to dot the i's and cross the t's (of aliasing effects, Nyquist limits etc). Perhaps some other fast integral transform would work even better. I will be surprised if there isn't an old industry around it.
Re: Hack the derivative
#16Lost me a bit at Im(f(x+ih))/h near the end (that's a fancy I that android FF won't paste). Can anyone explain where 'm' came from? Or is 'Im' just a fn returning the imaginary part of its argument? EDIT: reading the code following, it's clear that Im is just that.
I guess the author has the unstated assumption that f is the restriction to R of a function on C.
Re: Hack the derivative
#17Had the following thought just to entertain myself, I don't expect it to have any utility in practice, but here goes: Take a small interval around your smooth function that includes the point where you want to compute the derivative. Joint the right and left ends by just reflecting itself, so that you have a differentiable periodic function. Now sample that at a suitable rate, take its FFT, derive the FFT of the deri…
I highly recommend Spectral Methods in MATLAB by Trefethen (who someone mentions above) for a very good tutorial. You can freely ignore the MATLAB part and use whatever programming language you want, as long as you have an FFT routine.
Re: Hack the derivative
#18Had the following thought just to entertain myself, I don't expect it to have any utility in practice, but here goes: Take a small interval around your smooth function that includes the point where you want to compute the derivative. Joint the right and left ends by just reflecting itself, so that you have a differentiable periodic function. Now sample that at a suitable rate, take its FFT, derive the FFT of the deri…
This is called a spectral method and is widely used in numerical codes. I highly recommend Spectral Methods in MATLAB by Trefethen (who someone mentions above) for a very good tutorial. You can freely ignore the MATLAB part and use whatever programming language you want, as long as you have an FFT routine.
Does other integral transforms work better than Fourier ?
Re: Hack the derivative
#19Earlier quoted context omitted.
This is called a spectral method and is widely used in numerical codes. I highly recommend Spectral Methods in MATLAB by Trefethen (who someone mentions above) for a very good tutorial. You can freely ignore the MATLAB part and use whatever programming language you want, as long as you have an FFT routine.
Thanks for pointing to the right direction in case I want scratch that itch more. I am not surprised though that this has been done, expected the same. From a quick look, no one seems to suggest reflecting the original analytic function to get a periodic function, I guess some caveats lurk there. Does other integral transforms work better than Fourier ?
And no FFTs are the only transformations I am aware of. I've never heard of anyone using other transformations in a general numerical context, outside of specialized problems.
Re: Hack the derivative
#20I don't see any mention of automatic differentiation which I thought was the dominant technique in this area. Is there an advantage to the method described in OP (other than mathematical cuteness)?
No. Automatic differentiation is superior to the complex step method in every way. The only reason to use the complex step method is if you're using legacy languages where it's difficult to implement dual numbers but you have good support for complex numbers. I don't think anybody should be using the complex step method in new applications. Some reading ~ http://aero-comlab.stanford.edu/Papers/martins.aiaa.01-0921...…
Automatic differentiation only works for the simplest functions for which you already know what the Taylor series looks like. For those cases, you might as well just hardcode derivative functions and the basic derivative rules (linearity and chain rule). It is not a general-purpose method.
For functions that you can't even express by a simple formula, you still have to rely on finite differencing.
Don't call "legacy" anything that you don't understand.