-
Notifications
You must be signed in to change notification settings - Fork 0
/
Complex.h
45 lines (30 loc) · 1.7 KB
/
Complex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma once
#define TOL 0.000000000000001
#include <iostream>
#include <string>
namespace ComplexMath {
template <class T> class Complex
{
public:
inline Complex(T real = 0.0, T imag = 0.0) { m_real = real; m_imag = imag; }
inline ~Complex() { }
// Operators
inline Complex<T> operator+(const Complex<T>& z) { return Complex(this->m_real + z.m_real, this->m_imag + z.m_imag); }
inline Complex<T> operator+(const T& c) { return Complex(this->m_real + c, m_imag); }
inline Complex<T> operator*(const T& c) { return Complex(m_real*c, m_imag*c); }
inline Complex<T> operator*(const Complex<T>& z) { return Complex((m_real*z.m_real - m_imag * z.m_imag), (m_real*z.m_imag + m_imag * z.m_real)); }
inline Complex<T>& operator+=(const Complex<T>& z) { this->m_real += z.m_real; this->m_imag += z.m_imag; return *this; }
//inline bool operator==(const Complex<T>& z){return (m_real == z.m_real && m_imag == z.m_imag);}
inline bool operator==(const Complex<T>& z) { return ((m_real - z.m_real > TOL && m_real - z.m_real < TOL) && (m_imag - z.m_imag > TOL && m_imag - z.m_imag < TOL)); }
inline bool operator>(const Complex<T>& z) { return (m_real*m_real + m_imag * m_imag > z.m_real*z.m_real + z.m_imag*z.m_imag); }
inline bool operator>(const double& c) { return (m_real*m_real + m_imag * m_imag > c); }
inline Complex<T> conjugate() const { return Complex(m_real, -1 * m_imag); }
inline operator Complex<double>() { return Complex((double)m_real, (double)m_imag); }
inline std::string str() const { return (std::to_string(m_real) + " + " + std::to_string(m_imag) + "i"); }
private:
T m_real;
T m_imag;
};
template <class T>
std::string to_string(const Complex<T>& z) { return z.str(); }
}