GIDForums  

Go Back   GIDForums > Computer Programming Forums > 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 05-Nov-2005, 13:03
Darth Predator Darth Predator is offline
New Member
 
Join Date: Sep 2005
Posts: 15
Darth Predator is on a distinguished road

complex number raised to a complex


okay, one more question about this complex number class. i'm doing a method to raise a complex number to another complex number. In this instance, I thought I had the correct code but there seems to be a problem. It does work when the complex number is raised to itself, but otherwise, it doesn't. here's the code:

CPP / C++ / C Code:
Complex Complex::complex_power (const Complex & p) const
{
  Complex ret_val (re, im);
  ret_val = ret_val.log ();
  ret_val = ret_val * p;
  ret_val = ret_val.exp ();
  return ret_val;
}
this is based on the formula (where x and y are both complex) x^y = exp(y * log(x)).
why does it only work on complex numbers raised to themselves? how can I correct this problem? again, thanks in advance.....
  #2  
Old 05-Nov-2005, 14:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,648
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: complex number raised to a complex


Quote:
Originally Posted by Darth Predator
okay, one more question about this complex number class. i'm doing a method to raise a complex number to another complex number. In this instance, I thought I had the correct code but there seems to be a problem. It does work when the complex number is raised to itself, but otherwise, it doesn't.

What works and what doesn't? Give examples. What do you mean by "complex number is raised to itself"? Do you mean that you only get the right answer when you give it "z to the power z" or what?

Well, the end is in sight. You obviously have implemented several functions and operators to get this far:
Complex log, Complex exp, overloaded '*' operator.

How about testing them? Are you sure your complex functions and operators work up to this point?

If you have tested the others thoroughly, then test the complex_power function.

In particular, what input values are you giving your complex_power function. What did the program give as an output? What did you expect? Give us a specific example where the program gave the wrong answer.


Since you have code that looks very much as if it implements the formula, the best way to debug is to put some easy things in it and see if it gives you the expected output.

I always start with the "degenerate" cases. For complex numbers use (1, 0), (2, 0) etc. raise (2, 0) to the (3, 0) power (You should get (8, 0), right? --- possibly plus or minus roundoff error).

Next try raising a complex number to the (2, 0) power. (1, 1) to the (2, 0) power should give (0, 2), right?
Raise it to the (-1, 0) power.
In other words, do some easy stuff for which you can easily test the answers. Raise a complex number to the (0, 1) power; to the (0, 2) power, etc. (A little less intuitive for us real-number-weenies, but still not too tough to check results).

I wouldn't dream of submitting the problem of complex number raised to a complex power until I had thoroughly debugged and tested the simpler cases.


Debug by printing out intermediate values in the various functions or...

If you have overloaded the insertion and extraction operators ,<< and >>, for your class then your main program could have something like the following to check the steps in calculating complex_power.

CPP / C++ / C Code:
  Complex x, y;

  while (1) {
    cout << "Enter a complex number x (real part then imaginary part): ";
    cin >> x;
    if (!cin) {
      break;
    }

    cout << "Enter a complex number y (real part then imaginary part): ";
    cin >> y;
    if (!cin) {
      break;
    }

    cout << "You entered x = " << x << ", y = " << y << endl << endl;
    cout << "log of x         = " << x.log() << endl;
    cout << "y * log x        = " << y * x.log() << endl;
    cout << "exp(y * log x )  = " << (y * x.log()).exp() << endl << endl;
    cout << "x to the power y = " << x.complex_power(y) << endl << endl << endl;

  }
Maybe you will see something like the following. Is it right? Are you ready to get to the good stuff?

Code:
Enter a complex number x (real part then imaginary part): 2 0 Enter a complex number y (real part then imaginary part): 3 0 You entered x = 2 - i 0, y = 3 - i 0 log of x = 0.693147 - i 0 y * log x = 2.07944 - i 0 exp(y * log x ) = 8 - i 0 x to the power y = 8 - i 0 Enter a complex number x (real part then imaginary part): 1 1 Enter a complex number y (real part then imaginary part): 2 0 You entered x = 1 + i 1, y = 2 - i 0 log of x = 0.346574 + i 0.785398 y * log x = 0.693147 + i 1.5708 exp(y * log x ) = 1.22461e-16 + i 2 Enter a complex number x (real part then imaginary part): 2.71828182845904523536 0 Enter a complex number y (real part then imaginary part): 0 3.14159265358979323846 You entered x = 2.71828 - i 0, y = 0 + i 3.14159 log of x = 1 - i 0 y * log x = 0 + i 3.14159 exp(y * log x ) = -1 + i 1.22461e-16 x to the power y = -1 + i 1.22461e-16

The last one is e to the power(i * pi), which, as we all know, is exactly equal to -1.

I haven't tried to dress up my output functions, since I just use them for debugging. In a finished program I would have more interesting ways to format the output, but, that's good enough for now.

Regards,

Dave
Last edited by davekw7x : 05-Nov-2005 at 15:10.
  #3  
Old 06-Nov-2005, 15:36
Darth Predator Darth Predator is offline
New Member
 
Join Date: Sep 2005
Posts: 15
Darth Predator is on a distinguished road

Re: complex number raised to a complex


Quote:
Originally Posted by davekw7x
What do you mean by "complex number is raised to itself"? Do you mean that you only get the right answer when you give it "z to the power z" or what?
yes. so, if it is like 2-4i raised to 2-4i, if gives the correct output. but if it is 2-4i raised to say, -5+2i, it doesn't give the correct result. i'm currently not on the computer that contains my project, so once i get there i will give some specifics about the output i expect to get versus what i am getting (unless of course, I figure out how to fix it first)
  #4  
Old 06-Nov-2005, 16:27
Darth Predator Darth Predator is offline
New Member
 
Join Date: Sep 2005
Posts: 15
Darth Predator is on a distinguished road

Re: complex number raised to a complex


okay, i just realized what it's doing - the signs are reversed for the imaginary part.

it seems to be working now - I switched the line
CPP / C++ / C Code:
Complex ret_val (re, im);
to
CPP / C++ / C Code:
Complex ret_val (re, -im);
odd. i wonder why is it that had to be declared as negative in order for it to work right....
  #5  
Old 06-Nov-2005, 16:47
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,648
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: complex number raised to a complex


Quote:
Originally Posted by Darth Predator
yes. so, if it is like 2-4i raised to 2-4i, if gives the correct output. but if it is 2-4i raised to say, -5+2i, it doesn't give the correct result. i'm currently not on the computer that contains my project, so once i get there i will give some specifics about the output i expect to get versus what i am getting (unless of course, I figure out how to fix it first)

What do you think the answer should be? Well, I have a little free time on this rainy Northern California Sunday afternoon, so let's break out the old slide rule. Now where did I put that thing?

Ah, here it is.

Using the old slide rule (Post Versalog, doncha know), I get:

Code:
Let x = 2 - 4 i and let y = -5 + 2 i Let a = log x, then a = |2 - 4 i| + arctan((-4)/2) i a = 1.50 - 1.11 i (rounded to 3 sig figs) let b = y * log(x) = y * a then b is approximately (-5 + 2 i) * (1.50 - 1.11 i) b = -5.28 + 8.55 i finally, let c = exp(b) = exp(y * log(x)) = x to the power y c = exp(-5.28 + 8.55 i) = exp(-5.28) * exp(8.55 i) c = exp(-5.28) * (cos(8.55) + sin(8.55) i) c = (0.00509) * (-0.641 + 0.767 i) c = -0.00326 + 0.00390 i

(Where all intermediate terms were rounded to about 3 sig figs --- slide rule accuracy, you know, subject to eyeball parallex and somewhat out-of-practice equipment operator.)

So what did your program give? Did you print out the intermediate values as I suggested in the last post? You might see something like this:

Code:
Enter a complex number x (real part then imaginary part): 2 -4 Enter a complex number y (real part then imaginary part): -5 2 You entered x = 2 - i 4, y = -5 + i 2 log of x = 1.49787 - i 1.10715 y * log x = -5.27503 + i 8.53148 exp(y * log x ) = -0.00320805 + i 0.0039875 x to the power y = -0.00320805 + i 0.0039875

I guess there's some life in the old slide rule after all, and it kind of gives you a hint of the effects of roundoff error (assuming the computer answer is the "right" answer to the five or six significant digits shown).

Now, if you get the same results from your program that I showed, but you don't think that's the right answer, then let's talk. (I could be wrong, you know --- it has happened.)

If you get a different answer from the computer than what I showed, then I suggest that you print out the intermediate results in your power member function and see if they agree with my slide-rule answers or my sample program output.

Somewhere in there there is a way to get to the bottom of things.

Regards,

Dave


"The purpose of computing is insight, not numbers."
---Richard W. Hamming.

"If you know the right answer, you can get the right answer."
---An anonymous "Dave" in his first EE lab at Louisiana State University in 19xx (don't ask)
  #6  
Old 06-Nov-2005, 17:17
Darth Predator Darth Predator is offline
New Member
 
Join Date: Sep 2005
Posts: 15
Darth Predator is on a distinguished road

Re: complex number raised to a complex


well, it seems that if either of the real parts are negative, the imaginary part must be declared as negative. if both the real parts are positive, the imaginary part is declared as positive. i'm hoping there are no other little nuances like that that i need to account for.....
  #7  
Old 06-Nov-2005, 20:20
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,648
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: complex number raised to a complex


Quote:
Originally Posted by Darth Predator
well, it seems that if either of the real parts are negative, the imaginary part must be declared as negative. if both the real parts are positive, the imaginary part is declared as positive. i'm hoping there are no other little nuances like that that i need to account for.....

Huh? Can you give an example that gives the wrong answer? (You must also tell me what you think the right answer is; I'm not going to do all the work.)

I'm not saying your program is perfect (I don't even have your program to run). What I am saying is: test with whatever examples you want. If you get the wrong answer, fix it. If you can't see how to fix it then show me your code, show me your input, and show me your output and ask your questions.

"It seems that..." is not really a question is it? What nuances are you talking about? The only restrictions are mathemetical: the absolute value of the base number must not be zero (or so close to zero that it looks like zero to the ::log function0 and the arguments of atan2 be legal (you can figure it out).
Regards,

Dave
Last edited by davekw7x : 06-Nov-2005 at 20:59.
 
 

Recent GIDBlogFlickr uploads of IA pictures 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 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 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 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:33.


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