Howo to implement complex step derivative for complex functions?
I have a complex analytic function of which I want to take the numerical derivative.
\begin{align} f(z) &\equiv f(x,y) = u(x,y) + i v(x,y) \ \frac{d f(z)}{d z} & = \lim_{h \to 0} \frac{f(z + h) - f(h)}{h} = \frac{\partial u(x,y)}{\partial x} + i\frac{\partial v(x,y)}{\partial x} \end{align}
Now, I can do the partial derivatives via the complex step method, as $$ \frac{\partial u(x,y)}{\partial x} = \lim_{h \to 0} \frac{\Im{u(x + i h,y)}}{h} \ \frac{\partial v(x,y)}{\partial x} = \lim_{h \to 0} \frac{\Im{v(x + i h,y)}}{h} \ \frac{d f(z)}{d z} = \lim_{h \to 0} \left[ \frac{\Im{u(x + i h,y)}}{h} + i \frac{\Im{v(x + i h,y)}}{h}\right] $$
Here, $h$ is a real infinitesimally small number.
I want to implement this for a general complex analytic function. I am starting with a complex function as
#include<complex>
#include<iostream>
#include<limits>
using namespace std;
complex <double> f(complex< double > z) { return z*z; }
complex <double> u(complex< double > z) { return real(f(z)); }
complex <double> v(complex< double > z) { return imag(f(z)); }
double h = std<IP_ADDRESS>numeric_limits<double>::epsilon();
double h_inv = 1./std<IP_ADDRESS><IP_ADDRESS>epsilon();
double h_inv = 1./std::numeric_limits<double><IP_ADDRESS>epsilon();
int main()
{
//Calculating derivative at z = 1 + I;
double x = 1; y = 1;
complex<double> xh{x.,h};
complex<double> z = xh+complex<double>(0,y);
complex<double> dudx = imag(u(z))*h_inv;
complex<double> dvdx = imag(v(z))*h_inv;
complex<double> dfdz{dudx,dvdx};
return 0;
}
This doesn't work as I am taking the real part and imaginary part of $f$, both of which are real-valued functions, and I can't get any imaginary part in the complex step method, so dfdz = 0.
* I want this whole process to be numerical so that I don't have to analytically calculate real and imaginary part and plug in the code explicitly
* How to implement a complex step method in this case? 955