00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <qvmath.h>
00026 #include <QVComplex>
00027
00029
00030
00031 QVComplex::QVComplex():_real(0.0), _imaginary(0.0) { }
00032 QVComplex::QVComplex(const QVComplex &complex): _real(complex._real), _imaginary(complex._imaginary) { }
00033 QVComplex::QVComplex(const double real, const double imaginary): _real(real), _imaginary(imaginary) { }
00034 QVComplex::QVComplex(const QPointF &point): _real(point.x()), _imaginary(point.y()) { }
00035
00036 QVComplex & QVComplex::operator=(const QVComplex &complex)
00037 {
00038 _real = complex._real;
00039 _imaginary = complex._imaginary;
00040 return *this;
00041 }
00042
00043 bool QVComplex::operator==(const QVComplex &complex) const { return (_real == complex._real) and (_imaginary == complex._imaginary); };
00044 QVComplex & QVComplex::operator+=(const QVComplex &complex) { _real += complex._real; _imaginary += complex._imaginary; return *this; }
00045 bool QVComplex::operator!=(const QVComplex &complex) const { return (_real != complex._real) or (_imaginary != complex._imaginary); };
00046
00047 QVComplex QVComplex::operator*(const QVComplex &complex) const
00048 {
00049
00050
00051
00052
00053
00054 return QVComplex( _real * complex._real - _imaginary * complex._imaginary,
00055 _real * complex._imaginary + _imaginary * complex._real);
00056 };
00057
00058 QVComplex QVComplex::operator/(const QVComplex &complex) const
00059 {
00060
00061
00062
00063
00064
00065 const double denominator = POW2(complex._real) + POW2(complex._imaginary);
00066 return QVComplex( (_real * complex._real + _imaginary * complex._imaginary) / denominator,
00067 (_imaginary * complex._real - _real * complex._imaginary) / denominator);
00068 }
00069
00070 QVComplex QVComplex::operator+(const QVComplex &complex) const { return QVComplex(_real + complex._real, _imaginary + complex._imaginary); };
00071 QVComplex QVComplex::operator-(const QVComplex &complex) const { return QVComplex(_real - complex._real, _imaginary - complex._imaginary); };
00072 QVComplex QVComplex::operator-() const { return QVComplex(-_real, -_imaginary); };
00073 QVComplex QVComplex::operator*(const double value) const { return QVComplex(value * _real, value * _imaginary); };
00074 QVComplex QVComplex::operator/(const double value) const { return QVComplex(_real / value, _imaginary / value); };
00075 QVComplex QVComplex::operator+(const double value) const { return QVComplex(_real + value, _imaginary); };
00076 QVComplex QVComplex::operator-(const double value) const { return QVComplex(_real - value, _imaginary); };
00077 double QVComplex::norm2() const { return sqrt( POW2(_real) + POW2(_imaginary) ); }
00078 QVComplex QVComplex::i() { return QVComplex( 0.0, 1.0); }
00079
00080
00082 std::ostream& operator << ( std::ostream &os, const QVComplex &complex )
00083 {
00084 if (complex.imaginary() == 0.0)
00085 os << complex.real();
00086 else if (complex.real() == 0.0)
00087 {
00088 if (complex.imaginary() == 1.0)
00089 os << "i";
00090 else if (complex.imaginary() == -1.0)
00091 os << "-i";
00092 else
00093 os << complex.imaginary() << "i";
00094 }
00095 else {
00096 os << complex.real();
00097
00098 if (complex.imaginary() < 0)
00099 os << " - ";
00100 else
00101 os << " + ";
00102 if (complex.imaginary() == 1.0)
00103 os << "i";
00104 else
00105 os << ABS(complex.imaginary()) << "i";
00106 }
00107
00108 return os;
00109 }
00110
00111