Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programming Complex Equations...

Status
Not open for further replies.

DrkPaladin

Programmer
May 18, 2002
33
CA
How would I program a complex equation such as:
z = z^2 + c, where 'z' and 'c' are: 'real' + 'imaginary' so
it would look like this: (r+i) = (r+i)^2 + (r+i)???

Please put it as simple as you can...
Thanks...
 
Early on in my learning, I created an ultra simple complex number class once with members "real" and "imaginary". This is easy enough, and you can just overload all the operators and add a custom pow() function for the exponent .

I tweaked it to use overloaded operators, am posting the source below. Tell me if I can explain anything, or help you with the rest.

//*************Header File*****************************

//Header file for complex number class
//function definitions found in ComNum.cpp
//prob 6.6 p 449 D & D

#ifndef ComNum_H
#define ComNum_H

#include <iostream>
using std::eek:stream;

class ComNum
{
friend ostream& operator<<( ostream &, const ComNum & );
public:
ComNum ( double real = 0, double imaginary = 0 );
ComNum operator +( const ComNum & );
private:
double real;
double imaginary;
};

#endif

//****************class function definitions***************

//function definitions for complex number class
//prob 6.6 p 449 D & D

#include &quot;ComNum.h&quot;

ComNum::ComNum( double realPart, double imaginaryPart )
{
real = realPart;
imaginary = imaginaryPart;
}

ComNum ComNum::eek:perator +( const ComNum &addThis)
{
ComNum temp;
temp.imaginary = imaginary + addThis.imaginary;
temp.real = real + addThis.real;
return temp;
}

ostream& operator<<( ostream &output, const ComNum &x )
{
output << x.real << &quot; + &quot; << x.imaginary << 'i';
return output;
}

//******************driver to test code*******************

//works with ComNum class
//prob 6.6 p 449 D & D

#include <iostream>
using std::cout;
using std::endl;

#include &quot;ComNum.h&quot;

int main()
{

ComNum com1 = ComNum( 5, 10 ),
com2 = ComNum( 3, 1 );

cout << &quot;Complex numbers before calculations: &quot; << '\n'
<< &quot;First: &quot; << com1 << '\n'
<< &quot;Second: &quot; << com2 << '\n' << endl;


cout << &quot;First plus third: &quot; << com1 + com3 << '\n' << endl;

return 0;
}
 
Sorry, but I didn't understand any of it... For the equation: z = z^2 + c I was able to calculate it like this:

r2 and i2 the user gives the values for
r and i are x and y values on a complex plain

r3 = (r2 * r2) + (-(i2 * i2));
i3 = (i2 * r2) + (i2 * r2);

r5 = r3 + r;
i5 = i3 + i;

r2 = r5;
i2 = i5;

z = abs(pow(pow(r5, 2) + pow(i5, 2), 0.5));

I am using MFC AppWizard(exe)... I need the answer in complex form before I do the last line...
 
I'm not sure where you are in your learning, so I apologize if I'm explaining something you already know. A &quot;class&quot; is an &quot;object&quot; that has certain attributes and behaivior. A &quot;dog&quot; class might have the attributes &quot;fur color&quot;, &quot;size&quot;, &quot;tail&quot;, &quot;legs&quot;, etc. It also might have behaiviors &quot;sniff other dog's butt&quot;, &quot;wag tail&quot;, &quot;chase mailman&quot;, etc. If you make a dog class then you can &quot;instantiate&quot; it like:

Dog fido;

passing or not passing arguements as needed. This is just like &quot;instantiating&quot; an integer like:

int i; //&quot;i&quot; is an instantiation of an integer.

Once you've instantiated a dog, you can access dog behaiviors via member functions like:

fido.sniffButt();



What I did in the previously-posted complex number class is create a reusable object that can simplify complex number calculations. For example, instead of saying:

double r1 = 5,
i1 = 3;

for each needed complex number. I &quot;instantiate&quot; a complex number like:

ComNum num1( 5, 3 ); //i.e. 5 + 3i

The main advantages to the object-oriented approach is clarity, ease of calculation, and reusability. For example, to add two complex numbers the way you are doing, and store them in a new complex number, you'd say:

r3 = r1 + r2;
i3 = i1 + i2;

The class approach would eliminate half of the code, like this:

num3 = num1 + num2;

If the application grows to the point that hundreds of calculations are made, and there are several complex numbers bouncing around, you can imagine that the object-oriented approach would make it easier to program the code, track down errors, and save confusion.

I'm probably boring you by now, but tell me if I can help more.
 
You can also overload operator ^ Ion Filipski
1c.bmp


filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top