#include #include class Complex { public: Complex(double = 0., double = 0.); double real() ; double imag() { return m_imag; } double mag() ; double mag2() ; Complex coniugate() ; Complex inverse() ; Complex operator+=(Complex rhs); private: double m_real; double m_imag; }; inline double Complex::real() { return m_real; } Complex operator+(Complex lhs, Complex rhs); Complex operator-(Complex lhs, Complex rhs); Complex operator*(Complex lhs, Complex rhs); Complex operator/(Complex lhs, Complex rhs); Complex operator-(Complex lhs); double abs(Complex c); std::ostream& operator<<(std::ostream&, Complex ); Complex::Complex(double re, double im) : m_real(re), m_imag(im){ } Complex Complex::operator+=(Complex rhs) { m_real += rhs.m_real; m_imag += rhs.m_imag; return *this; } Complex operator-(Complex rhs){ return Complex(-rhs.real(), -rhs.imag()); } Complex operator+(Complex lhs, Complex rhs){ return Complex(lhs.real() + rhs.real(), lhs.imag() + rhs.imag()); } Complex operator-(Complex lhs, Complex rhs){ return Complex(lhs.real() - rhs.real(), lhs.imag() - rhs.imag()); } std::ostream& operator<<(std::ostream& os, Complex c){ //(re,im) os << '(' << c.real() << ',' << c.imag() << ')'; return os; } // cout < operator<<(cout,c2) Complex operator*(Complex lhs, Complex rhs){ return Complex(lhs.real()*rhs.real()-lhs.imag()*rhs.imag(), lhs.imag()*rhs.real()+ rhs.imag()*lhs.real()); } Complex operator/(Complex lhs, Complex rhs){ return lhs*rhs.inverse(); } Complex Complex::coniugate() { return Complex(real(),-imag()); } Complex Complex::inverse() { return 1./mag2()*coniugate(); } double Complex::mag2() { return (real()*real()+imag()*imag()); } double Complex::mag() { return sqrt(real()*real()+imag()*imag()); }