-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex_t.h
154 lines (127 loc) · 2.91 KB
/
complex_t.h
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
#ifndef COMPLEX_T_H
#define COMPLEX_T_H
#include "assert.h"
#include "point.h"
#include <cmath>
struct complex_t
{
double real;
double img;
complex_t(double real=0, double img=0)
: real(real), img(img) {}
complex_t(Point const & p)
{
assert(p.get_dimension() == 2);
real = p[0];
img = p[1];
}
complex_t& operator=(const complex_t & a)
{
real=a.real;
img=a.img;
return *this;
}
complex_t operator+(const complex_t & a) const
{
return complex_t(a.real+real, a.img+img);
}
complex_t& operator+=(const complex_t & a)
{
real+=a.real;
img+=a.img;
return *this;
}
bool operator==(const complex_t& a) const
{
return (real == a.real && img == a.img);
}
bool operator!=(const complex_t& a) const
{
return !(*this == a);
}
complex_t operator-(const complex_t & a) const
{
return complex_t(real-a.real, img - a.img);
}
complex_t& operator-=(const complex_t & a)
{
real-=a.real;
img-=a.img;
return *this;
}
complex_t & operator*=(const complex_t & a)
{
double old_real = real;
real = old_real*a.real - img*a.img;
img = old_real*a.img + img*a.real;
return *this;
}
complex_t operator*(const complex_t & a) const
{
return complex_t(real*a.real-img*a.img, a.img*real + a.real*img);
}
complex_t & operator*=(const double a)
{
real*=a;
img*=a;
return *this;
}
complex_t operator*(const double a) const
{
return complex_t(a*real,a*img);
}
double abs2() const
{
return real*real + img*img;
}
double abs() const
{
return std::sqrt(real*real+img*img);
}
complex_t conj() const
{
return complex_t(real,-img);
}
complex_t operator/(const double a) const
{
assert(a != 0);
return complex_t(real/a,img/a);
}
complex_t & operator/=(const double a)
{
assert(a!=0);
real/=a;
img/=a;
return *this;
}
complex_t operator/(const complex_t & a) const
{
assert(a != complex_t(0,0));
return ((*this)*a.conj())/a.abs2();
}
complex_t & operator/=(const complex_t & a)
{
assert(a!= complex_t(0,0));
double old_real = real;
double a_abs2 = a.abs2();
real = old_real*a.real + img*a.img;
real /= a_abs2;
img = img * a.real - old_real*a.img;
img /= a_abs2;
return *this;
}
complex_t operator-()
{
return complex_t(-real,-img);
}
double arg() const
{
assert(img != 0 || real !=0);
return atan2(img, real);
}
static complex_t log(complex_t const & fst)
{
return complex_t(std::log(fst.abs()),fst.arg());
}
};
#endif