-
Notifications
You must be signed in to change notification settings - Fork 2
/
complex.hh
129 lines (114 loc) · 3.03 KB
/
complex.hh
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
complex - fast complex math
Written in 2015 by <Ahmet Inan> <[email protected]>
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#ifndef COMPLEX_HH
#define COMPLEX_HH
template <typename T>
class Complex
{
T re, im;
public:
typedef T value_type;
Complex() : re(0), im(0) {}
Complex(T r) : re(r), im(0) {}
Complex(T r, T i) : re(r), im(i) {}
inline T real() const { return re; }
inline T imag() const { return im; }
inline void real(T r) { re = r; }
inline void imag(T i) { im = i; }
inline Complex<T> operator = (T a)
{
re = a;
im = 0;
return *this;
}
inline Complex<T> operator += (Complex<T> a)
{
return *this = a + *this;
}
inline Complex<T> operator -= (Complex<T> a)
{
return *this = *this - a;
}
inline Complex<T> operator *= (Complex<T> a)
{
return *this = a * *this;
}
inline Complex<T> operator *= (T a)
{
return *this = a * *this;
}
inline Complex<T> operator /= (T a)
{
return *this = *this / a;
}
inline Complex<T> operator /= (Complex<T> a)
{
return *this = *this / a;
}
};
template <typename T>
static inline Complex<T> operator + (Complex<T> a, Complex<T> b)
{
return Complex<T>(a.real() + b.real(), a.imag() + b.imag());
}
template <typename T>
static inline Complex<T> operator + (Complex<T> a)
{
return a;
}
template <typename T>
static inline Complex<T> operator - (Complex<T> a, Complex<T> b)
{
return Complex<T>(a.real() - b.real(), a.imag() - b.imag());
}
template <typename T>
static inline Complex<T> operator - (Complex<T> a)
{
return Complex<T>(-a.real(), -a.imag());
}
template <typename T>
static inline Complex<T> operator * (T a, Complex<T> b)
{
return Complex<T>(a * b.real(), a * b.imag());
}
template <typename T>
static inline Complex<T> operator / (Complex<T> a, T b)
{
return Complex<T>(a.real() / b, a.imag() / b);
}
template <typename T>
static inline Complex<T> operator * (Complex<T> a, Complex<T> b)
{
return Complex<T>(a.real() * b.real() - a.imag() * b.imag(), a.real() * b.imag() + a.imag() * b.real());
}
template <typename T>
static inline Complex<T> operator / (Complex<T> a, Complex<T> b)
{
return Complex<T>((a.real() * b.real() + a.imag() * b.imag()) / (b.real() * b.real() + b.imag() * b.imag()),
(a.imag() * b.real() - a.real() * b.imag()) / (b.real() * b.real() + b.imag() * b.imag()));
}
template <typename T>
static inline Complex<T> exp(Complex<T> a)
{
return Complex<T>(exp(a.real()) * cos(a.imag()), exp(a.real()) * sin(a.imag()));
}
template <typename T>
static inline T abs(Complex<T> a)
{
return hypot(a.real(), a.imag());
}
template <typename T>
static inline T arg(Complex<T> a)
{
return atan2(a.imag(), a.real());
}
template <typename T>
static inline T norm(Complex<T> a)
{
return a.real() * a.real() + a.imag() * a.imag();
}
#endif