-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffd4bdd
commit 094f2a7
Showing
5 changed files
with
55 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef CP_ALGO_UTIL_COMPLEX_HPP | ||
#define CP_ALGO_UTIL_COMPLEX_HPP | ||
#include <cmath> | ||
namespace cp_algo { | ||
template<typename T> | ||
struct complex { | ||
T x, y; | ||
constexpr complex() {} | ||
constexpr complex(T x): x(x), y(0) {} | ||
constexpr complex(T x, T y): x(x), y(y) {} | ||
complex& operator *= (T t) {x *= t; y *= t; return *this;} | ||
complex& operator /= (T t) {x /= t; y /= t; return *this;} | ||
complex operator * (T t) const {return complex(*this) *= t;} | ||
complex operator / (T t) const {return complex(*this) /= t;} | ||
complex& operator += (complex t) {x += t.x; y += t.y; return *this;} | ||
complex& operator -= (complex t) {x -= t.x; y -= t.y; return *this;} | ||
complex operator * (complex t) const {return {x * t.x - y * t.y, x * t.y + y * t.x};} | ||
complex operator / (complex t) const {return *this * t.conj() / t.norm();} | ||
complex operator + (complex t) const {return complex(*this) += t;} | ||
complex operator - (complex t) const {return complex(*this) -= t;} | ||
complex& operator *= (complex t) {return *this = *this * t;} | ||
complex& operator /= (complex t) {return *this = *this / t;} | ||
complex operator - () const {return {-x, -y};} | ||
complex conj() const {return {x, -y};} | ||
T norm() const {return x * x + y * y;} | ||
T abs() const {return std::sqrt(norm());} | ||
T real() const {return x;} | ||
T imag() const {return y;} | ||
static complex polar(T r, T theta) {return {r * std::cos(theta), r * std::sin(theta)};} | ||
auto operator <=> (complex const& t) const = default; | ||
}; | ||
template<typename T> | ||
complex<T> operator * (auto x, complex<T> y) {return y * x;} | ||
template<typename T> complex<T> conj(complex<T> x) {return x.conj();} | ||
template<typename T> T norm(complex<T> x) {return x.norm();} | ||
template<typename T> T abs(complex<T> x) {return x.abs();} | ||
template<typename T> T real(complex<T> x) {return x.real();} | ||
template<typename T> T imag(complex<T> x) {return x.imag();} | ||
template<typename T> complex<T> polar(T r, T theta) {return complex<T>::polar(r, theta);} | ||
} | ||
#endif // CP_ALGO_UTIL_COMPLEX_HPP |