#include "complex.h"

//Complex constructor, initialises to 0 + i0.
Complex::Complex() 
{
  real = 0;
  imaginary = 0;
}

Complex::Complex(double r, double i)
{
  real = r;
  imaginary = i;
}

//Complex destructor.
Complex::~Complex() 
{
}

double Complex::abs()
{
  return sqrt(pow(real,2)+pow(imaginary,2));
}

void Complex::Set(double a)
{
  real = cos(a);
  imaginary = sin(a);
}

//Overloaded = operator.
Complex Complex::operator=(Complex c) {
  if (&c != this) {
    real = c.Real();
    imaginary = c.Imaginary();
    return *this;
  }
  return *this;
}

//Overloaded = operator.
Complex Complex::operator=(double c) {
  real = c;
  imaginary = 0;
  return *this;
}

//Overloaded + operator.
Complex Complex::operator+(Complex c) { 
  Complex tmp; 
  double new_real, new_imaginary; 
  new_real = real + c.Real();
  new_imaginary = imaginary + c.Imaginary(); 
  tmp.Set(new_real,new_imaginary); 
  return tmp;
}

//Overloaded - operator.
Complex Complex::operator-(Complex c) { 
  Complex tmp; 
  double new_real, new_imaginary; 
  new_real = real - c.Real();
  new_imaginary = imaginary - c.Imaginary(); 
  tmp.Set(new_real,new_imaginary); 
  return tmp;
}

//Overloaded * operator.
Complex Complex::operator*(Complex c) { 
  Complex tmp; 
  double new_real, new_imaginary; 
  new_real = real * c.Real() - imaginary * c.Imaginary();
  new_imaginary = real * c.Imaginary() + imaginary * c.Real();
  tmp.Set(new_real,new_imaginary); 
  return tmp;
}

//Overloaded * operator.
Complex Complex::operator*(double c) { 
  Complex tmp;
  double new_real, new_imaginary; 
  new_real = real * c;
  new_imaginary = imaginary * c;
  tmp.Set(new_real,new_imaginary); 
  return tmp;
}

//Overloaded == operator.  Small error tolerances.
int Complex::operator==(Complex c) {
  //This is to take care of round off errors.
  if (fabs(c.Real() - real) > pow(10,-14)) {
    return 0;
  }
  if (fabs(c.Imaginary()- imaginary) > pow(10,-14)) {
    return 0;
  }
  return 1;
}

//Sets private data members.
void Complex::Set(double new_real, double new_imaginary) {
  real = new_real;
  imaginary = new_imaginary;
}

//Returns the real part of the complex number.
double Complex::Real() {
  return real;
}

//Returns the imaginary part of the complex number.
double Complex::Imaginary() {
  return imaginary;
}
