-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcomplex_fast.inl
164 lines (133 loc) · 4.25 KB
/
complex_fast.inl
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ifndef __COMPLEX_FAST_INL
#define __COMPLEX_FAST_INL
namespace RealLib {
template <class T> class complex {
public:
T re;
T im;
complex(const T re_=T(), const T im_=T()) : re(re_), im(im_) {}
//complex(const complex<T> &s) : re(s.re), im(s.im) {}
template <class X>
explicit complex(const complex<X> &s) : re(s.re), im(s.im) {}
T real() const {return re;}
T imag() const {return im;}
inline complex<T> operator= (const T re_) {re=re_;im=T(0.0f);return *this;}
inline complex<T> operator+=(const T re_) {re+=re_;return *this;}
inline complex<T> operator-=(const T re_) {re-=re_;return *this;}
inline complex<T> operator*=(const T re_) {re*=re_;im*=re_;return *this;}
inline complex<T> operator/=(const T re_) {re/=re_;im/=re_;return *this;}
template <class X> inline
complex<T> operator= (const complex<X> s)
{re = s.re; im = s.im; return *this;}
template <class X> inline
complex<T> operator+=(const complex<X> s)
{re += s.re; im += s.im;return *this;}
template <class X> inline
complex<T> operator-=(const complex<X> s)
{re -= s.re; im -= s.im; return *this;}
template <class X> inline
complex<T> operator*=(const complex<X> s)
{ T v = re*s.im;
T w = im*s.im;
re *= s.re;
im *= s.re;
re -= w;
im += v;
return *this;
}
template <class X> inline
complex<T> operator/= (const complex<X> s)
{ T v = re*s.im;
T w = im*s.im;
T u = s.re*s.re + s.im*s.im;
re *= s.re;
im *= s.re;
re = (re + w)/u;
im = (im - v)/u;
return *this;
}
inline void mulj() { T t=-im;im=re;re=t; }
};
template <class T>
inline complex<T> operator+(const complex<T> a, const complex<T> b)
{
return complex<T> (a.re + b.re, a.im + b.im);
}
template <class T>
inline complex<T> operator-(const complex<T> a, const complex<T> b)
{
return complex<T> (a.re - b.re, a.im - b.im);
}
template <class T>
inline complex<T> operator-(const complex<T> a)
{
return complex<T> (-a.re, -a.im);
}
template <class T>
inline complex<T> operator<<(const complex<T> a, const int b)
{
return complex<T> (a.re << b, a.im << b);
}
template <class T>
inline complex<T> operator*(const complex<T> a, const complex<T> b)
{
return complex<T> (a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re);
}
template <class T>
inline complex<T> operator*(const complex<T> a, const T b)
{
return complex<T> (a.re * b, a.im * b);
}
template <class T> inline
complex<T> operator/ (const complex<T> a, const complex<T> b)
{
T v = a.re*b.im;
T w = a.im*b.im;
T u = b.re*b.re + b.im*b.im;
T re = a.re * b.re;
T im = a.im * b.re;
return complex<T> ((re + w)/u, (im - v)/u);
}
template <class T>
inline complex<T> operator/(const complex<T> a, const T b)
{
return complex<T> (a.re / b, a.im / b);
}
template <class T>
inline complex<T> exp(const complex<T> s)
{
T exp_x = exp(s.re);
return complex<T> (exp_x * cos(s.im), exp_x * sin(s.im));
}
template <class T>
inline T abs(const complex<T> s)
{
return sqrt(s.re*s.re + s.im*s.im);
}
template <class T>
inline complex<T> mulj(const complex <T> s)
{
return complex<T> (-s.im, s.re);
}
template <class T>
inline complex<T> submulj(const complex <T> a, const complex<T> b)
{
return complex<T> (b.im - a.im, a.re - b.re);
}
template <class T>
inline complex<T> addconj(const complex <T> a, const complex<T> b)
{
return complex<T> (a.re + b.re, a.im - b.im);
}
template <class T>
inline complex<T> conj(const complex <T> a)
{
return complex<T> (a.re, -a.im);
}
template <class T>
inline complex<T> subconj(const complex <T> a, const complex<T> b)
{
return complex<T> (a.re - b.re, a.im + b.im);
}
}
#endif // __COMPLEX_FAST_INL