35 constexpr Complex(
double real,
double imag) : real(real), imag(imag) {}
37 constexpr Complex operator-()
const
39 return Complex(-this->real, -this->imag);
42 constexpr Complex operator+(
double rhs)
const
44 return Complex(this->real + rhs, this->imag);
47 constexpr Complex operator+(
const Complex& rhs)
const
49 return Complex(this->real + rhs.real, this->imag + rhs.imag);
52 inline constexpr Complex& operator+=(
double rhs)
58 inline constexpr Complex& operator+=(
const Complex& rhs)
60 this->real += rhs.real;
61 this->imag += rhs.imag;
65 constexpr Complex operator-(
double rhs)
const
67 return Complex(this->real - rhs, this->imag);
70 constexpr Complex operator-(
const Complex& rhs)
const
72 return Complex(this->real - rhs.real, this->imag - rhs.imag);
75 inline constexpr Complex& operator-=(
double rhs)
81 inline constexpr Complex& operator-=(
const Complex& rhs)
83 this->real -= rhs.real;
84 this->imag -= rhs.imag;
88 constexpr Complex operator*(
double rhs)
const
90 return Complex(this->real * rhs, this->imag * rhs);
93 constexpr Complex operator*(
const Complex& rhs)
const
95 return Complex(this->real * rhs.real - this->imag * rhs.imag, this->imag * rhs.real + this->real * rhs.imag);
98 inline constexpr Complex& operator*=(
double rhs)
105 inline constexpr Complex& operator*=(
const Complex& rhs)
107 const double newReal = this->real * rhs.real - this->imag * rhs.imag;
108 this->imag = this->imag * rhs.real + this->real * rhs.imag;
109 this->real = newReal;
113 constexpr Complex operator/(
double rhs)
const
115 return Complex(this->real / rhs, this->imag / rhs);
118 constexpr Complex operator/(
const Complex& rhs)
const
120 const double denomiter = rhs.
real * rhs.real + rhs.imag * rhs.imag;
121 return Complex((this->real * rhs.real + this->imag * rhs.imag) / denomiter,
122 (this->imag * rhs.real - this->real * rhs.imag) / denomiter);
125 inline constexpr Complex& operator/=(
double rhs)
132 inline constexpr Complex& operator/=(
const Complex& rhs)
134 const double denomiter = rhs.real * rhs.real + rhs.imag * rhs.imag;
135 const double newReal = (this->real * rhs.real + this->imag * rhs.imag) / denomiter;
136 this->imag = (this->imag * rhs.real - this->real * rhs.imag) / denomiter;
137 this->real = newReal;
141 constexpr bool operator==(
const Complex& rhs)
const
143 return this->real == rhs.real && this->imag == rhs.imag;
146 constexpr bool operator!=(
const Complex& rhs)
const
148 return this->real != rhs.real || this->imag != rhs.imag;
157 return Complex(this->real, -this->imag);
166 return this->real * this->real + this->imag * this->imag;