473,799 Members | 2,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

arctan of complex number

Does anybody know an easy way to get the atan of a complex number in C++?
thanks,
marc

Jul 22 '05 #1
8 3662

"Marc Schellens" <m_*********@ho tmail.com> wrote in message
news:3F******** ******@hotmail. com...
Does anybody know an easy way to get the atan of a complex number in C++?
thanks,
marc


Can you do it with pen and paper?

Jul 22 '05 #2
> "Marc Schellens" <m_*********@ho tmail.com> wrote in message
news:3F******** ******@hotmail. com...
Does anybody know an easy way to get the atan of a complex number in C++?
thanks,
marc

Can you do it with pen and paper?


Thank you for this valuable reply!

Jul 22 '05 #3
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.

Jul 22 '05 #4
Gianni Mariani wrote:
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.


But that (performance, elegance) was my point.
As Dan already suggested so helpfully, I could do it myself,
but I thought that somebody might have a tested performant solution.
Actually I suspected, that there is a standard 'hack' how to do it very
easyly in C++ (as its not in the STL).
Anyway, so far I came up myself with this:

// atan() for complex
template< typename C>
inline C atanC(const C& c)
{
const C i(0.0,1.0);
const C one(1.0,0.0);
return log( (one + i * c) / (one - i * c)) / (C(2.0,0.0)*i);
}

But thanks anyway,
marc

Jul 22 '05 #5
Marc Schellens wrote:
Gianni Mariani wrote:
....
But that (performance, elegance) was my point.
As Dan already suggested so helpfully, I could do it myself,
but I thought that somebody might have a tested performant solution.
Actually I suspected, that there is a standard 'hack' how to do it very
easyly in C++ (as its not in the STL).
Anyway, so far I came up myself with this:

// atan() for complex
template< typename C>
inline C atanC(const C& c)
{
const C i(0.0,1.0);
const C one(1.0,0.0);
return log( (one + i * c) / (one - i * c)) / (C(2.0,0.0)*i);
}


OK - seems like I still need to learn to *read* - atan - not tan...hmmm.

I don't have my old texts on my shelf any more so I'll go with what
you've got (and it works ... atan( tan( c ) ) == c ). I suppose it's
easy enough to work out ...

template< typename T >
std::complex<T> atanC(const std::complex<T> & c)
{
register T real = c.real();
register T imag = c.imag();

std::complex<T> log_v =
log(
std::complex<T> ( T(1) - imag, real )
/ std::complex<T> ( T(1) + imag, - real )
);

return std::complex<T> (
log_v.imag() * T(1.0/2), - log_v.real() * T(1.0/2)
);
}

This one does one complex division and one "log(complex<T> )". Hence I
don't think you can make it much faster. A quick perf test shows that
it is about the modified one above is 30% faster (no inlining) than the
one in the original post.

A 1.2GHz AMD does 870K complex atan's per second.

Jul 22 '05 #6
Gianni Mariani wrote:
Marc Schellens wrote:
Gianni Mariani wrote:

...

But that (performance, elegance) was my point.
As Dan already suggested so helpfully, I could do it myself,
but I thought that somebody might have a tested performant solution.
Actually I suspected, that there is a standard 'hack' how to do it
very easyly in C++ (as its not in the STL).
Anyway, so far I came up myself with this:

// atan() for complex
template< typename C>
inline C atanC(const C& c)
{
const C i(0.0,1.0);
const C one(1.0,0.0);
return log( (one + i * c) / (one - i * c)) / (C(2.0,0.0)*i);
}


OK - seems like I still need to learn to *read* - atan - not tan...hmmm.

I don't have my old texts on my shelf any more so I'll go with what
you've got (and it works ... atan( tan( c ) ) == c ). I suppose it's
easy enough to work out ...

template< typename T >
std::complex<T> atanC(const std::complex<T> & c)
{
register T real = c.real();
register T imag = c.imag();

std::complex<T> log_v =
log(
std::complex<T> ( T(1) - imag, real )
/ std::complex<T> ( T(1) + imag, - real )
);

return std::complex<T> (
log_v.imag() * T(1.0/2), - log_v.real() * T(1.0/2)
);
}

This one does one complex division and one "log(complex<T> )". Hence I
don't think you can make it much faster. A quick perf test shows that
it is about the modified one above is 30% faster (no inlining) than the
one in the original post.

A 1.2GHz AMD does 870K complex atan's per second.


That looks quite optimized.
Thanks,
marc

Jul 22 '05 #7
"Marc Schellens" <m_*********@ho tmail.com> wrote in message
news:3F******** ******@hotmail. com...
This one does one complex division and one "log(complex<T> )". Hence I
don't think you can make it much faster. A quick perf test shows that
it is about the modified one above is 30% faster (no inlining) than the
one in the original post.

A 1.2GHz AMD does 870K complex atan's per second.


That looks quite optimized.
Thanks,
marc


It's fast all right, just not terribly accurate.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #8
P.J. Plauger wrote:
"Marc Schellens" <m_*********@ho tmail.com> wrote in message
news:3F******** ******@hotmail. com...

This one does one complex division and one "log(complex<T> )". Hence I
don't think you can make it much faster. A quick perf test shows that
it is about the modified one above is 30% faster (no inlining) than the
one in the original post.

A 1.2GHz AMD does 870K complex atan's per second.


That looks quite optimized.
Thanks,
marc

It's fast all right, just not terribly accurate.

What are you referring to ? atan2 ? rounding errors ? better
polynomial approximation ?

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
6447
by: Shi Mu | last post by:
any python module to calculate sin, cos, arctan?
5
16920
by: Gerald | last post by:
Recently, my program need to be run in embeded enviroment, and I cann't use standard library. But I need to use arctan(x), so I implement it like the following: inline double pow(double x, size_t n) { if (n == 0) return 1; else if (n % 2 == 0) return pow(x * x, n >> 1); else
3
2066
by: Russ | last post by:
I'd like to get output formatting for my own classes that mimics the built-in output formatting. For example, >>> x = 4.54 >>> print "%4.2f" % x 4.54 In other words, if I substitute a class instance for "x" above, I'd like to make the format string apply to an element or elements of the instance. Can I somehow overload the "%" operator for that? Thanks.
12
2768
by: vj | last post by:
Hi! I have a piece of code (shown below) involving complex numbers. The code is not running and giving error ("Invalid floating point operation" and "SQRT:Domain error"). I would be very thankful if someone can tell me where is the problem. I am aware that my code is far from being efficient and organized, and also there are many extra #include statements not really required for the code. I am a novice programmer, as you can see ! At...
7
8617
by: Tina | last post by:
Dear all, I'm looking for a routine which calculates the Gamma Function for a complex valued variable. I'm using #include <complex> to work with complex numbers. I define a complex number as complex <doublealpha(3.0, 1.0)
1
1728
by: jraul | last post by:
Suppose we have a complex number class and we overload the conversion to double to return the real part. We also overload operator* to do complex multiplication. Consider now: complex a(...); complex b(...); complex c = a*b; But the compiler complains ambiguity since it doesn't know whether to
2
1922
by: jraul | last post by:
Suppose you have a complex number class, and you overload conversions to double by only taking the real part. You also overload operator* to do complex multiplication. You then write: complex a(...); complex b(...); complex c = a*b;
4
9378
by: astri | last post by:
i`m doing thesis about comparing calculation of arctan by polynomial and CORDIC. I`ve read a lot of journal and books about CORDIC and this is what i understand. 1. make x and y 2. make iterations 3. process with cordic calculation 4. the arctan result is calculating by arctan(y/x). what i m confused that in cordic equation there`s Zi=Zo-arctan(2^-i)
12
9609
by: astri | last post by:
i`m doing my thesis comparing CORDIC with polynomial in counting arctan with fixed point. I`m using Q15 format now. I`m using this site CORDIC arctan as a referenced when making with floating point. The problem there`s a lot of error when i try to make it with fixed point. this is my program #include "Unit1.h" #include "math.h" #include "fixed_math.hpp" #define MAXBITS 15 static float invGain1;
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10482
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10225
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.