Программирование на C++ - Класс Complex

07 Dec in Math

Несмотря на то, что в С++ есть стандартный шаблон complex для представления комплекных чисел, был сделан специальный класс Complex для того, чтобы было проще и понятнее:

class Complex
{
public:
double re, im;

Complex () {}
Complex ( double r ) : re(r), im(0) {}
Complex ( double r, double i ) : re(r), im(i) {}

bool operator ! () const
{
return ! re && ! im;
}

Complex operator ~ () const
{
return Complex ( re, -im );
}

Complex operator - () const
{
return Complex ( -re, -im );
}

Complex & operator = ( const double & b )
{
re = b;
im = 0.;
return *this;
}

Complex & operator += ( const double & b )
{
re += b;
return *this;
}

Complex & operator -= ( const double & b )
{
re -= b;
return *this;
}

Complex & operator *= ( const double b )
{
re *= b;
im *= b;
return *this;
}

Complex & operator /= ( const double b )
{
re /= b;
im /= b;
return *this;
}

Complex & operator += ( const Complex & b )
{
re += b.re;
im += b.im;
return *this;
}

Complex & operator -= ( const Complex & b )
{
re -= b.re;
im -= b.im;
return *this;
}

Complex & operator *= ( const Complex & b )
{
Complex c ( re * b.re - im * b.im, re * b.im + im * b.re );
re = c.re;
im = c.im;
return *this;
}

Complex & operator /= ( const Complex & b )
{
const double rr = re * b.re + im * b.im;
const double ii = im * b.re - re * b.im;
const double sq = b.re * b.re + b.im * b.im;
re = rr / sq;
im = ii / sq;
return *this;
}
};

////////////////////////////////////////////////////////////////////////

inline Complex operator + ( const double & a, const Complex & b )
{
return Complex ( a + b.re, b.im );
}

inline Complex operator - ( const double & a, const Complex & b )
{
return Complex ( a - b.re, - b.im );
}

inline Complex operator * ( const double & a, const Complex & b )
{
return Complex ( a * b.re, a * b.im );
}

inline Complex operator / ( const double & a, const Complex & b )
{
const double sq = b.re * b.re + b.im * b.im;
return Complex ( a * b.re / sq, - a * b.im / sq );
}

////////////////////////////////////////////////////////////////////////

inline Complex operator + ( const Complex & a, const double & b )
{
return Complex ( a.re + b, a.im );
}

inline Complex operator - ( const Complex & a, const double & b )
{
return Complex ( a.re - b, a.im );
}

inline Complex operator * ( const Complex & a, const double & b )
{
return Complex ( a.re * b, a.im * b );
}

inline Complex operator / ( const Complex & a, const double & b )
{
return Complex ( a.re / b, a.im / b );
}

////////////////////////////////////////////////////////////////////////

inline Complex operator + ( const Complex & a, const Complex & b )
{
return Complex ( a.re + b.re, a.im + b.im );
}

inline Complex operator - ( const Complex & a, const Complex & b )
{
return Complex ( a.re - b.re, a.im - b.im );
}

inline Complex operator * ( const Complex & a, const Complex & b )
{
return Complex ( a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re );
}

inline Complex operator / ( const Complex & a, const Complex & b )
{
const double re = a.re * b.re + a.im * b.im;
const double im = a.im * b.re - a.re * b.im;
const double sq = b.re * b.re + b.im * b.im;
return Complex ( re / sq, im / sq );
}

////////////////////////////////////////////////////////////////////////

inline bool operator == ( const Complex & a, const Complex & b )
{
return ( a.re == b.re ) && ( a.im == b.im );
}

inline bool operator != ( const Complex & a, const Complex & b )
{
return ( a.re != b.re ) || ( a.im != b.im );
}

////////////////////////////////////////////////////////////////////////

inline double sqmd ( const Complex & c )
{
return c.re * c.re + c.im * c.im;
}

double abs ( const Complex & );

Complex sqrt ( const Complex & );

 

Comments

Post new comment

 
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.
By submitting this form, you accept the Mollom privacy policy.
Developed by NStudioCorp.com All trademarks and copyrights on this site are owned by their respective owners.
Comments are owned by the Poster. The Rest © 2000-2011 Firstov.com

About Firstov.com |  Terms of Service |  Support |  Contact Us |  Advertise