Marc Schellens wrote:
Does anybody know an easy way to get the atan of a complex number in C++?
thanks,
marc
tan(cplx) = sin(cplx)/cos(cplx)
cplx= real + i*imag
sin(i*imag) = i * sinh(imag)
sinh(imag) = ( exp(imag) - exp(-imag) )/2
cos(i*imag) = cosh(imag)
cosh(imag) = ( exp(imag) + exp(-imag) )/2
then
sin(cplx) = sin(real)*cosh(imag) + sinh(imag)*cos(real)
cos(cplx) = cos(real)&cosh(imag) - sin(real)*sinh(imag)
so finally
tan( real + i * imag ) =
sin(real)*cosh(imag) + i * sinh(imag)*cos(real)
-----------------------------------------------
cos(real)&cosh(imag) - i * sin(real)*sinh(imag)
- this leads to this:
template <typename T>
std::complex<T> tan( const std::complex<T> & theta )
{
register T r = theta.real();
register T v = theta.imag();
T exp_v = exp(v);
T exp_mv = 1/exp_v; // same as exp(-v)
T cos_r = cos(r);
T cosh_v = ( exp_v + exp_mv ) / 2;
T sin_r = sin(r);
T sinh_v = ( exp_v - exp_mv ) / 2;
std::complex<T> numerator( sin_r*cosh_v, cos_r*sinh_v );
std::complex<T> denominator( cos_r*cosh_v, - sin_r*sinh_v );
return numerator / denominator;
}
- I didn't check it, I'll leave that exercise to you.
If performance is critical, I suspect that you can do better than this.