i am in a class of programming and i have to do a program of complex numbers, and cant do it, this is my last proyect and ai have to finish
Help in C++, a program of complex numbers
i am in a class of programming and i have to do a program of complex n
Page 1 of 17 Replies  6930 Views  Last Post: 15 December 2009  01:33 PM
Replies To: Help in C++, a program of complex numbers
#2
Re: Help in C++, a program of complex numbers
Posted 04 December 2006  07:10 AM
there are many methods... it's will be easy for u to do in direct method..... means applying direct formulas for any operation
#3
Re: Help in C++, a program of complex numbers
Posted 04 December 2006  07:59 AM
Can you please elaborate on your requirements, and post the code you've written so far? I believe there may be some implementations of a complex number class in the code snippets section.
#4
Re: Help in C++, a program of complex numbers
Posted 04 December 2006  10:22 AM
Complex numbers? Are they numbers that you can create or did your teacher give you the numbers you have to use? Well either way yes alot of formulas are going to be needed...
#5
Re: Help in C++, a program of complex numbers
Posted 04 December 2006  11:08 AM
Complex Numbers that are a combination of real and imaginary numbers...
http://en.wikipedia..../Complex_number
http://en.wikipedia..../Complex_number
#6
Re: Help in C++, a program of complex numbers
Posted 04 December 2006  11:11 AM
That shouldn't be to difficult... just input the numbers... Maybe that sounds hard to you but it doesn't to me.... Maybe I'm having trouble understanding what you posted... You just have to have a program with complex numbers in it?
#7
Re: Help in C++, a program of complex numbers
Posted 04 December 2006  05:31 PM
I found one snippet in this site
http://www.dreaminco...e/snippet75.htm
Though it needs chnages but it will give you a start.
http://www.dreaminco...e/snippet75.htm
Though it needs chnages but it will give you a start.
#8
Re: Help in C++, a program of complex numbers
Posted 15 December 2009  01:33 PM
Hi, I wrote a nice complex number class that you might want to use or have a look at.
Just execute the file, it includes a tutorial in it and also shows how to use it for operations with arrays matrices and vectors. when you execute it, just read the screen output, it will show exactly how to use all the tools in this class.
Just execute the file, it includes a tutorial in it and also shows how to use it for operations with arrays matrices and vectors. when you execute it, just read the screen output, it will show exactly how to use all the tools in this class.
//A Class for complex numbers written by Steven Lettieri on Dec 15 2009. //Simply execute this file and read the screen output for the tutorial. //Feel free to edit and distribute this file as you wish and please credit me if you choose to do so //Send comments to Steve72182@hotmail.com #include <math.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <iostream.h> #include <vector.h> const double PI = acos(1.); const double TOL = 1.e10; class Complex{ public: double re,im; Complex (){};//default constructor Complex (double,double);//alternative constructor Complex operator + (Complex); Complex operator  (Complex); Complex operator * (Complex); Complex operator / (Complex); unsigned int operator == (Complex); void operator = (Complex); void PrintComplex(); Complex Conjugate(); double MagnitudeSq(); double Magnitude(); Complex ComplexPower(Complex); }; void Complex::operator = (Complex param){ re = param.re; im = param.im; } unsigned int Complex::operator== (Complex param){ if((re == param.re)&&(im == param.im)){return 1;} else{return 0;} } Complex::Complex(double a, double b){ re=a; im=b; } Complex Complex::operator+ (Complex param) { Complex temp; temp.re = re + param.re; temp.im = im + param.im; return (temp); } Complex Complex::operator (Complex param) { Complex temp; temp.re = re  param.re; temp.im = im  param.im; return (temp); } Complex Complex::operator* (Complex param) { Complex temp; temp.re = re*param.re  im*param.im; temp.im = im*param.re + re*param.im; return (temp); } Complex Complex::operator/ (Complex param) { Complex temp; double mag2 = param.MagnitudeSq(); temp.re = re*param.re + im*param.im; temp.im = im*param.re  re*param.im; temp.re = temp.re/mag2; temp.im = temp.im/mag2; return (temp); } void Complex::PrintComplex(void){ cout<<(re)<<" + "<<(im)<<"i"<<endl; } Complex Complex::Conjugate(void) { Complex temp; temp.re=re; temp.im=im; return (temp); } double Complex::MagnitudeSq(){ return re*re+im*im; } double Complex::Magnitude(){ return sqrt(re*re+im*im); } Complex Complex::ComplexPower(Complex z2) //z1^z2 { //basic idea: from math, its true that z1^z2 = exp(z2*ln(z1)). Using this form, the complex^complex can be evaluated. double n1,n2; double a1,a2,b1,b2; double mag2= re*re+im*im; Complex answer; if(mag2<=TOL*TOL) { cout<<"WARNING in z1.ComplexPower(z2):"<<endl; cout<<"The magnitude of z1 is below the tolerance"<<endl; cout<<"Line: "<<__LINE__<<endl; } if(re <=TOL) { cout<<"WARNING in z1.ComplexPower(z2):"<<endl; cout<<"The real part of z1 is below the tolerance and so z1 is almost purely imaginary"<<endl; cout<<"There is a risk of atan2() not being well defined"<<endl; cout<<"Line: "<<__LINE__<<endl; } a1 = 0.5*log(mag2); b1 = atan2(im,re); a2 = z2.re; b2 = z2.im; n1 = a2*a1  b2*b1; n2 = b2*a1 + a2*b1; answer.re = exp(n1)*cos(n2); answer.im = exp(n1)*sin(n2); return (answer); } int main() { Complex a(3.5,2.5); Complex b(2.5,1.5); cout<<"Complex Numbers Tutorial using my Complex Class "<<endl; cout<<"\n"<<endl; cout<<"\nThe file "<<__FILE__<<" compliation began on "<<__DATE__<<" at time "<<__TIME__<<"."<<endl; cout<<"\nPart 1: Elementary Operations: +, , /, *, Conjugation, Magnitude, Exponentiation"<<endl; cout<<"\n"<<endl; Complex c; double k; c= a+b; cout<<"\nDefine the complex number a: Complex a(3.5,2.5) \n"; cout<<"\nAn alternative way to define it is: a = Complex(3.5,2.5)\n"; a = Complex(3.5,2.5); cout<<"\nPrint this number: a.PrintComplex() \n"; a.PrintComplex(); cout<<"\nThe real part of a is: a.re\n"<<a.re<<endl; cout<<"\nThe imaginary part of a is: a.im\n"<<a.im<<endl; cout<<"\nLet b = \n"; b.PrintComplex(); c=a+b; cout<<"\nAddition: a + b =\n"; c.PrintComplex(); c=a*b; cout<<"\nMultiplication: a * b =\n"; c.PrintComplex(); cout<<"\nDivision: a/b = \n"; c = a/b; c.PrintComplex(); cout<<"\nConjugation of a: a.Conjugate() =\n"; a = a.Conjugate(); a.PrintComplex(); cout<<"\nSquared Magnitude of a: a.MagnitudeSq() = \n"; k = a.MagnitudeSq(); cout<<k<<endl; cout<<"\nMagnitude of a: a.Magnitude() = \n"; k = a.Magnitude(); cout<<k<<endl; Complex z1(3,5); Complex z2(2,8); Complex res; cout<<"\nExponentiation(a complex number to a complex power): z1^z2: \n"<<endl; cout<<"Let z1 = "; z1.PrintComplex(); cout<<"Let z2 = "; z2.PrintComplex(); res = z1.ComplexPower(z2); cout<<"\nThen z1^z2 = z1.ComplexPower(z2) = "<<endl; res.PrintComplex(); cout<<"\nPart 2: Class Complex operations with arrays, vectors and matrices"<<endl; cout<<"\n"<<endl; cout<<"\nDeclare a simple complex array of size 5: Complex myarray[5]\n"<<endl; cout<<"\nNote that this is exactly how you would declare any type of array except that the type here is Complex, as opposed to, say, double\n"; Complex myarray[5]; cout<<"\nSimply fill up the values by looping over i:\n"<<endl; cout<<"for(int i=0;i<5;i++){myarray[i] = Complex(3,4);}\n"<<endl; for(int i=0;i<5;i++) { myarray[i] = Complex(3,4); myarray[i].PrintComplex(); } cout<<"\nAll the other operations for complex numbers will apply:\n"<<endl; cout<<"\nFor example, add the first 2 numbers of the array: myarray[0]+myarray[1]\n"<<endl; (myarray[0]+myarray[1]).PrintComplex(); cout<<"\nWe can do exponentiation: myarray[0].ComplexPower(myarray[1]) = "; (myarray[0].ComplexPower(myarray[1])).PrintComplex(); cout<<"\nWe can access the real and imaginary parts of the sum, as expected, by\n"; cout<<"\n(myarray[0]+myarray[1]).re = "<<(myarray[0]+myarray[1]).re<<endl; cout<<"and\n(myarray[0]+myarray[1]).im = "<<(myarray[0]+myarray[1]).im<<endl; cout<<"\nThe same operations apply for Matrices, for example, one can declare Complex mymatrix[5][5] and this would be valid.\n"; cout<<"\nSince operations on elements of the matrix would be exactly the same as arrays, there is nothing new to discuss here.\n"; cout<<"\nFinally, for operations with vectors: we can declare A to be a complex vector with: vector<Complex> A \n"<<endl; vector<Complex> A; cout<<"\nOne way to fill up elements of a vector is the usual way, lets fill it with 2 complex numbers:\n"; cout<<"\nA.push_back(Complex(2,3))\n"<<endl; cout<<"\nA.push_back(Complex(6,7))\n"<<endl; A.push_back(Complex(2,3)); A.push_back(Complex(6,7)); cout<<"\nWe can access the first element of a vector in the usual way: A[0]\n"; A[0].PrintComplex(); cout<<"\nAnd then perform all the usual operations as we did with arrays and scalars\n"; cout<<"\nAs an exercise to the reader, I did not overload the << operator, you may choose to do this\n"; cout<<"\nThis Concludes the tutorial on my Complex class, I hope you find it easy and natural to use!\n"<<endl; return 0; }
Page 1 of 1
