![]() |
|
#1
|
|||
|
|||
log of a complex numberi'm writing a complex number class and having a bit of trouble with finding the log of a complex. I've obtained numerous sources of how you find it, so I have the basic idea, but the transition to C++ hasn't gone so great. Here's what I currently have (which is wrong):
CPP / C++ / C Code:
CPP / C++ / C Code:
One other problem is that I've seen about three or so slightly different methods of how to compute the log of complex numbers. Could someone show me the correct way to implement this log function? thanks! |
|
#2
|
|||
|
|||
Re: log of a complex numberQuote:
You want a function takes a complex argument and returns a complex value. You have shown how to calculate the real part and the imaginary part of the result. You can't just add them as real numbers, you simply set the return value's real and complex parts to the values you showed. As a first effort you could try something like the following: CPP / C++ / C Code:
Then CPP / C++ / C Code:
Usually in mathematics, the angle is taken to be from 0 to 2 pi, whereas the atan2 function returns an angle between -pi and pi, so you might want to do a little adjustment here to keep the mathematicians happy. Anyhow this is a way to get started. (The complex log is an infinite-valued function since incrementing the result by any integer multiple of i times two pi gives the same value. The value with the angle between 0 and 2 pi is called the principal value.) Regards, Dave |
|
#3
|
|||
|
|||
OopsQuote:
A small detail: While the example that I gave showed how to make a complex-valued member function, it does not give the correct value for the complex logarithm of a complex number(!) The real part of the (Complex) return value is calculated by using the standard (real-valued) log function and the standard (real-valued) sqrt function CPP / C++ / C Code:
Sorry. Of course if someone had simply copied my example they would have spotted the discrepency immediately upon testing on anything that had a non-zero real part. Note also that I mentioned that the little function that I gave might be a suitable starting point. A finished product would contain protection against taking the log of 0 for example. Regards, Dave Last edited by davekw7x : 05-Nov-2005 at 07:58.
|
|
#4
|
|||
|
|||
Re: log of a complex numberokay, gotcha. good to see that I had the right idea; I was just applying it wrong.
many thanks dave! |
Recent GIDBlog
First week of IA training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need Help with my Cards Program (C++) | krisopotamus | CPP / C++ Forum | 2 | 06-Oct-2005 16:48 |
| Knights Tour - Reloaded . | kobi_hikri | C Programming Language | 12 | 03-Oct-2005 12:15 |
| Roman to decimal to roman | SpudNuts | CPP / C++ Forum | 2 | 16-Feb-2005 19:44 |
| Anyone can write a program code for this??? | chriskan76 | C Programming Language | 1 | 19-Oct-2004 20:25 |
| complex number | tinzi | CPP / C++ Forum | 2 | 01-Jun-2004 14:47 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The