-
Notifications
You must be signed in to change notification settings - Fork 39
/
complex.h
203 lines (164 loc) · 5.12 KB
/
complex.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// complex.h - declaration of class
// of complex number
//
// The code is property of LIBROW
// You can use it on your own
// When utilizing credit LIBROW site
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
class complex {
protected:
// Internal presentation - real and imaginary parts
double m_re;
double m_im;
public:
// Imaginary unity
static const complex i;
static const complex j;
// Constructors
complex(): m_re(0.), m_im(0.) {}
complex(double re, double im): m_re(re), m_im(im) {}
complex(double val): m_re(val), m_im(0.) {}
// Assignment
complex &operator= (const double val) {
m_re = val;
m_im = 0.;
return *this;
}
// Basic operations - taking parts
double re() const {
return m_re;
}
double im() const {
return m_im;
}
// Conjugate number
complex conjugate() const {
return complex(m_re, -m_im);
}
// Norm
double norm() const {
return m_re * m_re + m_im * m_im;
}
// Arithmetic operations
complex operator+ (const complex &other) const {
return complex(m_re + other.m_re, m_im + other.m_im);
}
complex operator- (const complex &other) const {
return complex(m_re - other.m_re, m_im - other.m_im);
}
complex operator* (const complex &other) const {
return complex(m_re * other.m_re - m_im * other.m_im,
m_re * other.m_im + m_im * other.m_re);
}
complex operator/ (const complex &other) const {
const double denominator = other.m_re * other.m_re + other.m_im * other.m_im;
return complex((m_re * other.m_re + m_im * other.m_im) / denominator,
(m_im * other.m_re - m_re * other.m_im) / denominator);
}
complex &operator+= (const complex &other) {
m_re += other.m_re;
m_im += other.m_im;
return *this;
}
complex &operator-= (const complex &other) {
m_re -= other.m_re;
m_im -= other.m_im;
return *this;
}
complex &operator*= (const complex &other) {
const double temp = m_re;
m_re = m_re * other.m_re - m_im * other.m_im;
m_im = m_im * other.m_re + temp * other.m_im;
return *this;
}
complex &operator/= (const complex &other) {
const double denominator = other.m_re * other.m_re + other.m_im * other.m_im;
const double temp = m_re;
m_re = (m_re * other.m_re + m_im * other.m_im) / denominator;
m_im = (m_im * other.m_re - temp * other.m_im) / denominator;
return *this;
}
complex &operator++ () {
++m_re;
return *this;
}
complex operator++ (int) {
complex temp(*this);
++m_re;
return temp;
}
complex &operator-- () {
--m_re;
return *this;
}
complex operator-- (int) {
complex temp(*this);
--m_re;
return temp;
}
complex operator+ (const double val) const {
return complex(m_re + val, m_im);
}
complex operator- (const double val) const {
return complex(m_re - val, m_im);
}
complex operator* (const double val) const {
return complex(m_re * val, m_im * val);
}
complex operator/ (const double val) const {
return complex(m_re / val, m_im / val);
}
complex &operator+= (const double val) {
m_re += val;
return *this;
}
complex &operator-= (const double val) {
m_re -= val;
return *this;
}
complex &operator*= (const double val) {
m_re *= val;
m_im *= val;
return *this;
}
complex &operator/= (const double val) {
m_re /= val;
m_im /= val;
return *this;
}
friend complex operator+ (const double left, const complex &right) {
return complex(left + right.m_re, right.m_im);
}
friend complex operator- (const double left, const complex &right) {
return complex(left - right.m_re, -right.m_im);
}
friend complex operator* (const double left, const complex &right) {
return complex(left * right.m_re, left * right.m_im);
}
friend complex operator/ (const double left, const complex &right) {
const double denominator = right.m_re * right.m_re + right.m_im * right.m_im;
return complex(left * right.m_re / denominator,
-left * right.m_im / denominator);
}
// Boolean operators
bool operator== (const complex &other) const {
return m_re == other.m_re && m_im == other.m_im;
}
bool operator!= (const complex &other) const {
return m_re != other.m_re || m_im != other.m_im;
}
bool operator== (const double val) const {
return m_re == val && m_im == 0.;
}
bool operator!= (const double val) const {
return m_re != val || m_im != 0.;
}
friend bool operator== (const double left, const complex &right) {
return left == right.m_re && right.m_im == 0.;
}
friend bool operator!= (const double left, const complex &right) {
return left != right.m_re || right.m_im != 0.;
}
};
#endif