65 #define OPH_DLL __declspec(dllexport) 67 #define OPH_DLL __attribute__((visibility("default"))) 82 #if _MSC_VER > 1900 || defined(__GNUC__) 83 template<
typename T =
double>
84 class OPH_DLL Complex :
public std::complex<T>
87 using cplx =
typename std::enable_if<std::is_same<double, T>::value || std::is_same<float, T>::value || std::is_same<long double, T>::value, T>::type;
90 Complex() : std::complex<T>() {}
91 Complex(T p) : std::complex<T>(p) { this->real(p); this->imag((T)0.0); }
92 Complex(T tRe, T tIm) : std::complex<T>(tRe, tIm) {}
93 Complex(
const Complex<T>& p) {
97 T mag2()
const {
return this->real() * this->real() + this->imag() * this->imag(); }
98 T mag()
const {
return sqrt(mag2()); }
103 T theta = acos(this->real() / r);
105 if (sin(theta) - this->imag() / r < 10e-6)
108 return 2.0 *
M_PI - theta;
111 void euler(T& r, T& theta)
119 if (std::is_same<double, T>::value)
120 return atan2(this->imag(), this->real());
121 else if (std::is_same<float, T>::value)
122 return atan2f((
float)this->imag(), (
float)this->real());
126 Complex<T> p(this->real(), this->imag());
127 if (std::is_same<double, T>::value) {
128 this->real(std::exp(p.real()) * cos(p.imag()));
129 this->imag(std::exp(p.real()) * sin(p.imag()));
133 this->real(std::expf(p.real()) * cos(p.imag()));
134 this->imag(std::expf(p.real()) * sin(p.imag()));
136 this->real(std::exp(p.real()) * cos(p.imag()));
137 this->imag(std::exp(p.real()) * sin(p.imag()));
143 Complex<T> conj()
const {
return Complex<T>(this->real(), -this->imag()); }
145 Complex<T>& operator()(T re, T im) {
153 Complex<T>& operator= (
const Complex<T>& p) {
154 this->real(p.real());
155 this->imag(p.imag());
160 Complex<T>& operator = (
const T& p) {
167 Complex<T>
operator+ (
const Complex<T>& p) {
168 Complex<T> n(this->real() + p.real(), this->imag() + p.imag());
173 Complex<T>&
operator+= (
const Complex<T>& p) {
174 this->real(this->real() + p.real());
175 this->imag(this->imag() + p.imag());
181 Complex<T> n(this->real() + p, this->imag());
187 this->real(this->real() + p);
192 Complex<T>
operator- (
const Complex<T>& p) {
193 Complex<T> n(this->real() - p.real(), this->imag() - p.imag());
198 Complex<T>&
operator-= (
const Complex<T>& p) {
199 this->real(this->real() - p.real());
200 this->imag(this->imag() - p.imag());
206 Complex<T> n(this->real() - p, this->imag());
212 this->real(this->real() - p);
218 Complex<T> n(this->real() *
k, this->imag() *
k);
224 this->real(this->real() *
k);
225 this->imag(this->imag() *
k);
230 Complex<T>& operator = (
const std::complex<T>& p) {
231 this->real(p.real());
232 this->imag(p.imag());
237 Complex<T>
operator* (
const Complex<T>& p) {
238 const T tRe = this->real();
239 const T tIm = this->imag();
241 Complex<T> n(tRe * p.real() - tIm * p.imag(), tRe * p.imag() + tIm * p.real());
246 Complex<T>&
operator*= (
const Complex<T>& p) {
247 const T tRe = this->real();
248 const T tIm = this->imag();
250 this->real(tRe * p.real() - tIm * p.imag());
251 this->imag(tRe * p.imag() + tIm * p.real());
257 Complex<T> n(this->real() / p, this->imag() / p);
263 this->real(this->real() /
k);
264 this->imag(this->imag() /
k);
270 std::complex<T> a(this->real(), this->imag());
271 std::complex<T> b(p.real(), p.imag());
273 std::complex<T> c = a / b;
275 Complex<T> n(c.real(), c.imag());
281 std::complex<T> a(this->real(), this->imag());
282 std::complex<T> b(p.real(), p.imag());
285 this->real(a.real());
286 this->imag(a.imag());
291 T& operator [](
const int idx) {
292 return reinterpret_cast<T*
>(
this)[idx];
295 bool operator < (const Complex<T>& p) {
296 return (this->real() < p.real());
300 return (this->real() > p.real());
303 operator unsigned char() {
304 return uchar(this->real());
308 return int(this->real());
311 friend Complex<T>
operator+ (
const Complex<T>&p,
const T q) {
312 return Complex<T>(p.real() + q, p.imag());
315 friend Complex<T>
operator- (
const Complex<T>&p,
const T q) {
316 return Complex<T>(p.real() - q, p.imag());
319 friend Complex<T>
operator* (
const T
k,
const Complex<T>& p) {
320 return Complex<T>(p) *=
k;
323 friend Complex<T>
operator* (
const Complex<T>& p,
const T
k) {
324 return Complex<T>(p) *=
k;
327 friend Complex<T>
operator* (
const Complex<T>& p,
const Complex<T>& q) {
328 return Complex<T>(p.real() * q.real() - p.imag() * q.imag(), p.real() * q.imag() + p.imag() * q.real());
331 friend Complex<T>
operator/ (
const Complex<T>& p,
const Complex<T>& q) {
332 return Complex<T>((1.0 / q.mag2())*(p*q.conj()));
335 friend Complex<T>
operator/ (
const Complex<T>& p,
const T& q) {
336 return Complex<T>(p.real() / q, p.imag() / q);
339 friend Complex<T>
operator/ (
const T& p,
const Complex<T>& q) {
340 return Complex<T>(p / q.real(), p / q.imag());
343 friend bool operator< (const Complex<T>& p,
const Complex<T>& q) {
344 return (p.real() < q.real());
347 friend bool operator> (
const Complex<T>& p,
const Complex<T>& q) {
348 return (p.real() > q.real());
353 template<
typename T =
double>
357 using cplx =
typename std::enable_if<std::is_same<double, T>::value || std::is_same<float, T>::value || std::is_same<long double, T>::value, T>::type;
364 this->_Val[
_RE] = p._Val[
_RE];
365 this->_Val[
_IM] = p._Val[
_IM];
367 T
mag2()
const {
return this->_Val[
_RE] * this->_Val[
_RE] + this->_Val[
_IM] * this->_Val[
_IM]; }
368 T
mag()
const {
return sqrt(mag2()); }
373 T theta = acos(this->_Val[
_RE] / r);
375 if (sin(theta) - this->_Val[
_IM] / r < 10e-6)
378 return 2.0 *
M_PI - theta;
389 if (std::is_same<double, T>::value)
390 return atan2(this->_Val[
_IM], this->_Val[
_RE]);
391 else if (std::is_same<float, T>::value)
392 return atan2f((
float)this->_Val[
_IM], (
float)this->_Val[
_RE]);
397 if (std::is_same<double, T>::value) {
398 this->_Val[
_RE] = std::exp(p._Val[
_RE]) * cos(p._Val[
_IM]);
399 this->_Val[
_IM] = std::exp(p._Val[
_RE]) * sin(p._Val[
_IM]);
403 this->_Val[
_RE] = std::expf(p._Val[
_RE]) * cos(p._Val[
_IM]);
404 this->_Val[
_IM] = std::expf(p._Val[
_RE]) * sin(p._Val[
_IM]);
406 this->_Val[
_RE] = std::exp(p._Val[
_RE]) * cos(p._Val[
_IM]);
407 this->_Val[
_IM] = std::exp(p._Val[
_RE]) * sin(p._Val[
_IM]);
416 this->_Val[
_RE] = re;
417 this->_Val[
_IM] = im;
424 this->_Val[
_RE] = p._Val[
_RE];
425 this->_Val[
_IM] = p._Val[
_IM];
432 this->_Val[
_IM] = 0.0;
444 this->_Val[
_RE] = this->_Val[
_RE] + p._Val[
_RE];
445 this->_Val[
_IM] = this->_Val[
_IM] + p._Val[
_IM];
457 this->_Val[
_RE] = this->_Val[
_RE] + p;
469 this->_Val[
_RE] = this->_Val[
_RE] - p._Val[
_RE];
470 this->_Val[
_IM] = this->_Val[
_IM] - p._Val[
_IM];
482 this->_Val[
_RE] = this->_Val[
_RE] - p;
494 this->_Val[
_RE] = this->_Val[
_RE] *
k;
495 this->_Val[
_IM] = this->_Val[
_IM] *
k;
501 this->_Val[
_RE] = p._Val[
_RE];
502 this->_Val[
_IM] = p._Val[
_IM];
508 const T tRe = this->_Val[
_RE];
509 const T tIm = this->_Val[
_IM];
517 const T tRe = this->_Val[
_RE];
518 const T tIm = this->_Val[
_IM];
520 this->_Val[
_RE] = tRe * p._Val[
_RE] - tIm * p._Val[
_IM];
521 this->_Val[
_IM] = tRe * p._Val[
_IM] + tIm * p._Val[
_RE];
533 this->_Val[
_RE] = this->_Val[
_RE] /
k;
534 this->_Val[
_IM] = this->_Val[
_IM] /
k;
540 std::complex<T> a(this->_Val[
_RE], this->_Val[
_IM]);
541 std::complex<T> b(p._Val[
_RE], p._Val[
_IM]);
543 std::complex<T> c = a / b;
551 std::complex<T> a(this->_Val[
_RE], this->_Val[
_IM]);
552 std::complex<T> b(p._Val[
_RE], p._Val[
_IM]);
555 this->_Val[
_RE] = a._Val[
_RE];
556 this->_Val[
_IM] = a._Val[
_IM];
561 T& operator [](
const int idx) {
562 return reinterpret_cast<T*
>(
this)[idx];
565 bool operator < (const Complex<T>& p) {
566 return (this->_Val[
_RE] < p._Val[
_RE]);
570 return (this->_Val[
_RE] > p._Val[
_RE]);
573 operator unsigned char() {
578 return int(this->_Val[
_RE]);
613 friend bool operator< (const Complex<T>& p,
const Complex<T>& q) {
614 return (p._Val[
_RE] < q._Val[
_RE]);
618 return (p._Val[
_RE] > q._Val[
_RE]);
629 #endif // !__complex_h_
ivec2 operator+=(ivec2 &a, const ivec2 &b)
class for the complex number and its arithmetic. type T == type cplx type only float || double T real...
ivec2 operator-(const ivec2 &a, const ivec2 &b)
ivec2 operator+(const ivec2 &a, const ivec2 &b)
ivec2 operator*(const ivec2 &a, const ivec2 &b)
Complex< T > & operator()(T re, T im)
vec2 operator/=(vec2 &a, const vec2 &b)
vec2 operator/(const vec2 &a, const vec2 &b)
ivec2 operator-=(ivec2 &a, const ivec2 &b)
void euler(T &r, T &theta)
Complex< T > conj() const
typename std::enable_if< std::is_same< double, Real >::value||std::is_same< float, Real >::value||std::is_same< long double, Real >::value, Real >::type cplx
int operator>(const ivec2 &a, const ivec2 &b)
Complex(const Complex< T > &p)
void angle(const std::vector< Complex< T >> &src, std::vector< T > &dst)
ivec2 operator*=(ivec2 &a, const ivec2 &b)