To download the code, click here
001: #include <iostream>
002: #include <sstream>
003: #include <cmath>
004: #include <string>
005:
006: #include "complex.hpp"
007:
008: namespace dmcgrath{
009:
010: complex::complex(){
011: this->real_val = 0;
012: this->imaginary = 0;
013: }
014:
015: complex::complex(double real_val){
016: this->real_val = real_val;
017: this->imaginary = 0;
018: }
019:
020: complex::complex(double real_val, double imaginary){
021: this->real_val = real_val;
022: this->imaginary = imaginary;
023: }
024:
025: complex::complex(const complex &c){
026: this->real_val = c.real_val;
027: this->imaginary = c.imaginary;
028: }
029:
030: complex::~complex(){
031:
032: }
033:
034: //return the real part
035: const double complex::real(){
036: return this->real_val;
037: }
038:
039: //return the imaginary part
040: const double complex::imag(){
041: return this->imaginary;
042: }
043:
044: //flip sign on imaginary part, return a NEW VALUE
045: const complex complex::conj(){
046: complex tmp((this->real_val), -(this->imaginary));
047: return tmp;
048: }
049:
050: //overload output operator
051: std::ostream& operator<<(std::ostream &os, const complex &c){
052: std::stringstream ss;
053: std::string outs;
054: ss << '(' << c.real_val << ',' << c.imaginary << ')';
055:
056: ss >> outs;
057: os << outs;
058: return os;
059: }
060:
061: //overload input operator
062: std::istream& operator>>(std::istream &is, complex &c){
063:
064: char paren;
065: char paren_or_comma;
066: double real_val;
067: double imaginary;
068:
069: is >> paren;
070: if (paren == '('){
071: //of the format (r,c) or (r) if legal
072: is >> real_val;
073: is >> paren_or_comma;
074: if(paren_or_comma == ',')
075: //then it is (r,c)
076: is >> imaginary >> paren;
077: else if(paren_or_comma == ')')
078: //is just (r)
079: imaginary = 0;
080: }
081: else{
082: //of the format r if legal
083: //we have the first character of the real_val value, so...
084: std::stringstream ss;
085:
086: //stick it on a stringstream
087: ss << paren;
088:
089: //get the rest of the value, if it is there
090: if (is.peek() != '\n'){
091: is >> real_val;
092: //combine with beginning
093: ss << real_val;
094: }
095:
096: //store in real_val
097: ss >> real_val;
098: }
099:
100: c.real_val = real_val;
101: c.imaginary = imaginary;
102:
103: return is;
104: }
105:
106: //absolute value of c
107: double complex::abs(){
108: return std::sqrt(norm());
109: }
110:
111: //square absolute value
112: const double complex::norm(){
113: double real_val_square = this->real_val * this->real_val;
114: double imag_square = this->imaginary * this->imaginary;
115: return real_val_square + imag_square;
116: }
117:
118: //returns the phase angle, in radians
119: double complex::phase(){
120: return std::atan2(this->imaginary, this->real_val);
121: }
122:
123: bool complex::operator==(const complex &RHS){
124: return (this->real_val == RHS.real_val &&
125: this->imaginary == RHS.imaginary);
126: }
127:
128: bool complex::operator!=(const complex &RHS){
129: return !(*this == RHS);
130: }
131:
132:
133: bool complex::operator+=(const complex &RHS){
134: complex tmp(*this + RHS);
135: this->real_val = tmp.real_val;
136: this->imaginary = tmp.imaginary;
137: return true;
138: }
139:
140: bool complex::operator-=(const complex &RHS){
141: complex tmp(*this - RHS);
142: this->real_val = tmp.real_val;
143: this->imaginary = tmp.imaginary;
144: return true;
145: }
146:
147: bool complex::operator*=(const complex &RHS){
148: complex tmp(*this * RHS);
149: this->real_val = tmp.real_val;
150: this->imaginary = tmp.imaginary;
151: return true;
152: }
153:
154: bool complex::operator/=(complex &RHS){
155: complex tmp(*this / RHS);
156: this->real_val = tmp.real_val;
157: this->imaginary = tmp.imaginary;
158: return true;
159: }
160:
161: bool complex::operator=(const complex &RHS){
162: this->real_val = RHS.real_val;
163: this->imaginary = RHS.imaginary;
164: return true;
165: }
166:
167: complex complex::operator+(const complex &RHS){
168: complex tmp;
169: tmp.real_val = this->real_val + RHS.real_val;
170: tmp.imaginary = this->imaginary + RHS.imaginary;
171:
172: return tmp;
173: }
174:
175: complex complex::operator-(const complex &RHS){
176: complex tmp;
177: tmp.real_val = this->real_val - RHS.real_val;
178: tmp.imaginary = this->imaginary - RHS.imaginary;
179:
180: return tmp;
181: }
182:
183: complex complex::operator*(const complex &RHS){
184: complex tmp;
185: tmp.real_val = this->real_val * RHS.real_val - this->imaginary * RHS.imaginary;
186: tmp.imaginary = this->imaginary * RHS.real_val + RHS.imaginary * this->real_val;
187:
188: return tmp;
189: }
190:
191: complex complex::operator/(complex &rhs){
192: complex tmp = rhs.conj();
193:
194: double denominator = rhs.norm();
195: complex numerator = (*this) * tmp;
196:
197: tmp.real_val = numerator.real_val / denominator;
198: tmp.imaginary = numerator.imaginary / denominator;
199:
200: return tmp;
201: }
202:
203: }