#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
#include <math.h>
class Complex {
public:
Complex(double r = 0.0, double i = 0.0) {re = r; im = i;}
Complex(const Complex& rhs) {re = rhs.re; im = rhs.im;}
~Complex(void) {}
double real(void) const {return re;}
double imag(void) const {return im;}
void imag(double i) {im = i;}
void real(double r) {re = r;}
Complex& operator=(const Complex& rhs) {
if (this == &rhs) return *this;
re = rhs.re; im = rhs.im;
return *this;
}
bool operator==(const Complex& rhs) {
return ((real()==rhs.real()) && (imag()==rhs.imag()));
}
double norm() {
return real()*real() + imag()*imag();
}
double arg() {
if (imag()==0.0 && real()==0.0)
return 0.0;
else
return atan2(imag(), real());
}
const Complex operator+(const Complex& rhs) const {
return Complex(re+rhs.re, im+rhs.im);
}
const Complex operator*(const Complex& rhs) const {
return Complex(re*rhs.re - im*rhs.im, re*rhs.im + im*rhs.re);
}
private:
double re, im;
};
Complex polar(double mag, double arg=0.0) {
return Complex(mag * cos(arg), mag * sin(arg));
}
Complex conj(const Complex& x) {
return Complex(x.real(), -x.imag());
}
double norm(const Complex& x) {
return x.real()*x.real() + x.imag()*x.imag();
}
const double epsilon = 1.0e-15;
ostream& operator<<(ostream& output, const Complex& rhs) {
if ( -epsilon < rhs.real() && rhs.real() < epsilon )
output << "(0,";
else
output << "(" << rhs.real() << ",";
if ( -epsilon < rhs.imag() && rhs.imag() < epsilon )
output << "0i)";
else
output << rhs.imag() << "i)";
return output;
}
#endif