|
2 | 2 | #define CP_ALGO_UTIL_COMPLEX_HPP
|
3 | 3 | #include <cmath>
|
4 | 4 | namespace cp_algo {
|
5 |
| - template<typename T> |
6 |
| - struct complex { |
7 |
| - T x, y; |
8 |
| - constexpr complex() {} |
9 |
| - constexpr complex(T x): x(x), y(0) {} |
10 |
| - constexpr complex(T x, T y): x(x), y(y) {} |
11 |
| - complex& operator *= (T t) {x *= t; y *= t; return *this;} |
12 |
| - complex& operator /= (T t) {x /= t; y /= t; return *this;} |
13 |
| - complex operator * (T t) const {return complex(*this) *= t;} |
14 |
| - complex operator / (T t) const {return complex(*this) /= t;} |
15 |
| - complex& operator += (complex t) {x += t.x; y += t.y; return *this;} |
16 |
| - complex& operator -= (complex t) {x -= t.x; y -= t.y; return *this;} |
17 |
| - complex operator * (complex t) const {return {x * t.x - y * t.y, x * t.y + y * t.x};} |
18 |
| - complex operator / (complex t) const {return *this * t.conj() / t.norm();} |
19 |
| - complex operator + (complex t) const {return complex(*this) += t;} |
20 |
| - complex operator - (complex t) const {return complex(*this) -= t;} |
21 |
| - complex& operator *= (complex t) {return *this = *this * t;} |
22 |
| - complex& operator /= (complex t) {return *this = *this / t;} |
23 |
| - complex operator - () const {return {-x, -y};} |
24 |
| - complex conj() const {return {x, -y};} |
25 |
| - T norm() const {return x * x + y * y;} |
26 |
| - T abs() const {return std::sqrt(norm());} |
27 |
| - T real() const {return x;} |
28 |
| - T imag() const {return y;} |
29 |
| - static complex polar(T r, T theta) {return {r * std::cos(theta), r * std::sin(theta)};} |
| 5 | + template<typename T> |
| 6 | + struct complex { |
| 7 | + T x, y; |
| 8 | + constexpr complex() {} |
| 9 | + constexpr complex(T x): x(x), y(0) {} |
| 10 | + constexpr complex(T x, T y): x(x), y(y) {} |
| 11 | + complex& operator *= (T t) {x *= t; y *= t; return *this;} |
| 12 | + complex& operator /= (T t) {x /= t; y /= t; return *this;} |
| 13 | + complex operator * (T t) const {return complex(*this) *= t;} |
| 14 | + complex operator / (T t) const {return complex(*this) /= t;} |
| 15 | + complex& operator += (complex t) {x += t.x; y += t.y; return *this;} |
| 16 | + complex& operator -= (complex t) {x -= t.x; y -= t.y; return *this;} |
| 17 | + complex operator * (complex t) const {return {x * t.x - y * t.y, x * t.y + y * t.x};} |
| 18 | + complex operator / (complex t) const {return *this * t.conj() / t.norm();} |
| 19 | + complex operator + (complex t) const {return complex(*this) += t;} |
| 20 | + complex operator - (complex t) const {return complex(*this) -= t;} |
| 21 | + complex& operator *= (complex t) {return *this = *this * t;} |
| 22 | + complex& operator /= (complex t) {return *this = *this / t;} |
| 23 | + complex operator - () const {return {-x, -y};} |
| 24 | + complex conj() const {return {x, -y};} |
| 25 | + T norm() const {return x * x + y * y;} |
| 26 | + T abs() const {return std::sqrt(norm());} |
| 27 | + T real() const {return x;} |
| 28 | + T imag() const {return y;} |
| 29 | + static complex polar(T r, T theta) {return {r * std::cos(theta), r * std::sin(theta)};} |
30 | 30 | auto operator <=> (complex const& t) const = default;
|
31 |
| - }; |
32 |
| - template<typename T> |
33 |
| - complex<T> operator * (auto x, complex<T> y) {return y * x;} |
34 |
| - template<typename T> complex<T> conj(complex<T> x) {return x.conj();} |
35 |
| - template<typename T> T norm(complex<T> x) {return x.norm();} |
36 |
| - template<typename T> T abs(complex<T> x) {return x.abs();} |
37 |
| - template<typename T> T real(complex<T> x) {return x.real();} |
38 |
| - template<typename T> T imag(complex<T> x) {return x.imag();} |
39 |
| - template<typename T> complex<T> polar(T r, T theta) {return complex<T>::polar(r, theta);} |
| 31 | + }; |
| 32 | + template<typename T> |
| 33 | + complex<T> operator * (auto x, complex<T> y) {return y * x;} |
| 34 | + template<typename T> complex<T> conj(complex<T> x) {return x.conj();} |
| 35 | + template<typename T> T norm(complex<T> x) {return x.norm();} |
| 36 | + template<typename T> T abs(complex<T> x) {return x.abs();} |
| 37 | + template<typename T> T real(complex<T> x) {return x.real();} |
| 38 | + template<typename T> T imag(complex<T> x) {return x.imag();} |
| 39 | + template<typename T> complex<T> polar(T r, T theta) {return complex<T>::polar(r, theta);} |
40 | 40 | }
|
41 | 41 | #endif // CP_ALGO_UTIL_COMPLEX_HPP
|
0 commit comments