// Complex class definition (version 2 -- internal representation is polar. // All functions are in-line. There's no separate implementation code file. #ifndef COMPLEX /* Multiple definition guard */ #define COMPLEX const Complex /* Conventional notation for constants */ #include /* For sqrt and trig functions */ #include /* For conversion-to-real error */ #include "global.hpp" class Complex { double mag, ang; // Polar coordinates // Constructors and destructor // --------------------------- public: Complex(DOUBLE x, DOUBLE y = 0) : mag(sqrt(x*x + y*y)), ang(atan(y/x)) {} Complex() : mag(0), ang(0) {} // The compiler will generate an appropriate destructor, copy constructor, // and assignment operator. // Accessors // --------- double realPart() const {return mag*cos(ang);} double imagPart() const {return mag*sin(ang);} double rho() const {return mag;} double theta() const {return ang;} // Member operators // ---------------- Complex operator-() const // Unary minus operator {Complex result; static DOUBLE piOver2 = 3.14159265 / 2.0; result.mag = mag; result.ang = ang + piOver2; return result;} operator double () const // Conversion to real {assert(imagPart() == 0); return realPart();} // (Provided that value is real) }; // ************ End of class definition // Non-member operators and functions // ---------------------------------- // These functions are neither member nor friend functions, since they // can gain access to the component data through the accessors. inline ostream& operator<< (ostream& ls, // External representation is COMPLEX rs) // ordered pair in parens. {ls << '(' << rs.realPart() << ", " << rs.imagPart() << ')'; return ls;} //%SPACE 2 // Arithmetic operations // --------------------- inline Complex operator+ (COMPLEX ls, COMPLEX rs) {return Complex(ls.realPart() + rs.realPart(), ls.imagPart() + rs.imagPart());} inline Complex operator- (COMPLEX ls, COMPLEX rs) {return ls + (-rs);} inline Complex operator* (COMPLEX ls, COMPLEX rs) {return Complex (ls.realPart() * rs.realPart() - ls.imagPart() * rs.imagPart(), ls.realPart() * rs.imagPart() + ls.imagPart() * rs.realPart());} inline Complex operator/ (COMPLEX ls, COMPLEX rs) {const double denom = rs.realPart() * rs.realPart() + rs.imagPart() * rs.imagPart(); return Complex ((ls.realPart() * rs.realPart() + ls.imagPart() * rs.imagPart()) / denom, (rs.realPart() * ls.imagPart() - rs.imagPart() * ls.realPart()) / denom);} inline Complex& operator+= (Complex& ls, COMPLEX rs) {return ls = ls + rs;} inline Complex& operator-= (Complex& ls, COMPLEX rs) {return ls = ls - rs;} inline Complex& operator*= (Complex& ls, COMPLEX rs) {return ls = ls * rs;} inline Complex& operator/= (Complex& ls, COMPLEX rs) {return ls = ls / rs;} inline double abs(COMPLEX x){return x.rho();} // Relations // --------- inline bool operator== (COMPLEX ls, COMPLEX rs) {return ls.realPart() == rs.realPart() && ls.imagPart() == rs.imagPart();} inline bool operator!= (COMPLEX ls, COMPLEX rs) {return !(ls == rs);} #endif