GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Nov-2005, 18:39
Darth Predator Darth Predator is offline
New Member
 
Join Date: Sep 2005
Posts: 15
Darth Predator is on a distinguished road

log of a complex number


i'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:
Complex Complex::log () const
{
  return (std::log(abs()) + arg ()); 
}
where abs() and arg() are the following:

CPP / C++ / C Code:
double abs (const Complex & c)
{
  double ret_val;
  ret_val = ((c.re * c.re) + (c.im * c.im));
  ret_val = sqrt (ret_val);
  return ret_val;
}

double Complex::arg () const
{
  return atan2 (im, re);
}
re and im are the two (type double) variables in the class for the real and imaginary parts of the complex number.

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  
Old 04-Nov-2005, 20:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: log of a complex number


Quote:
Originally Posted by Darth Predator
i'm writing a complex number class


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:
Complex Complex::log() const
{
  Complex retval;

  retval.re = sqrt(re*re + im*im);
  retval.im = atan2(im, re);

  return retval;
}

Then
CPP / C++ / C Code:

int main()
{
  Complex a;
  Complex b(1.0, 2.0);

  a = b.log();
.
.
.
  return 0;
}

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  
Old 05-Nov-2005, 06:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Oops


Quote:
Originally Posted by davekw7x
CPP / C++ / C Code:
  retval.re = sqrt(re*re + im*im);


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:
  retval.re = ::log(::sqrt(re*re + im*im));

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  
Old 05-Nov-2005, 12:57
Darth Predator Darth Predator is offline
New Member
 
Join Date: Sep 2005
Posts: 15
Darth Predator is on a distinguished road

Re: log of a complex number


okay, gotcha. good to see that I had the right idea; I was just applying it wrong.

many thanks dave!
 
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 01:47.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.