1
+ // complex.h - declaration of class
2
+ // of complex number
3
+ //
4
+ // The code is property of LIBROW
5
+ // You can use it on your own
6
+ // When utilizing credit LIBROW site
7
+
8
+ #ifndef _COMPLEX_H_
9
+ #define _COMPLEX_H_
10
+
11
+ class complex
12
+ {
13
+ protected:
14
+ // Internal presentation - real and imaginary parts
15
+ double m_re;
16
+ double m_im;
17
+
18
+ public:
19
+ // Imaginary unity
20
+ static const complex i;
21
+ static const complex j;
22
+
23
+ // Constructors
24
+ complex (): m_re(0 .), m_im(0 .) {}
25
+ complex (double re, double im): m_re(re), m_im(im) {}
26
+ complex (double val): m_re(val), m_im(0 .) {}
27
+
28
+ // Assignment
29
+ complex& operator = (const double val)
30
+ {
31
+ m_re = val;
32
+ m_im = 0 .;
33
+ return *this ;
34
+ }
35
+
36
+ // Basic operations - taking parts
37
+ double re () const { return m_re; }
38
+ double im () const { return m_im; }
39
+
40
+ // Conjugate number
41
+ complex conjugate () const
42
+ {
43
+ return complex (m_re, -m_im);
44
+ }
45
+
46
+ // Norm
47
+ double norm () const
48
+ {
49
+ return m_re * m_re + m_im * m_im;
50
+ }
51
+
52
+ // Arithmetic operations
53
+ complex operator + (const complex& other) const
54
+ {
55
+ return complex (m_re + other.m_re , m_im + other.m_im );
56
+ }
57
+
58
+ complex operator - (const complex& other) const
59
+ {
60
+ return complex (m_re - other.m_re , m_im - other.m_im );
61
+ }
62
+
63
+ complex operator * (const complex& other) const
64
+ {
65
+ return complex (m_re * other.m_re - m_im * other.m_im ,
66
+ m_re * other.m_im + m_im * other.m_re );
67
+ }
68
+
69
+ complex operator / (const complex& other) const
70
+ {
71
+ const double denominator = other.m_re * other.m_re + other.m_im * other.m_im ;
72
+ return complex ((m_re * other.m_re + m_im * other.m_im ) / denominator,
73
+ (m_im * other.m_re - m_re * other.m_im ) / denominator);
74
+ }
75
+
76
+ complex& operator += (const complex& other)
77
+ {
78
+ m_re += other.m_re ;
79
+ m_im += other.m_im ;
80
+ return *this ;
81
+ }
82
+
83
+ complex& operator -= (const complex& other)
84
+ {
85
+ m_re -= other.m_re ;
86
+ m_im -= other.m_im ;
87
+ return *this ;
88
+ }
89
+
90
+ complex& operator *= (const complex& other)
91
+ {
92
+ const double temp = m_re;
93
+ m_re = m_re * other.m_re - m_im * other.m_im ;
94
+ m_im = m_im * other.m_re + temp * other.m_im ;
95
+ return *this ;
96
+ }
97
+
98
+ complex& operator /= (const complex& other)
99
+ {
100
+ const double denominator = other.m_re * other.m_re + other.m_im * other.m_im ;
101
+ const double temp = m_re;
102
+ m_re = (m_re * other.m_re + m_im * other.m_im ) / denominator;
103
+ m_im = (m_im * other.m_re - temp * other.m_im ) / denominator;
104
+ return *this ;
105
+ }
106
+
107
+ complex& operator ++ ()
108
+ {
109
+ ++m_re;
110
+ return *this ;
111
+ }
112
+
113
+ complex operator ++ (int )
114
+ {
115
+ complex temp (*this );
116
+ ++m_re;
117
+ return temp;
118
+ }
119
+
120
+ complex& operator -- ()
121
+ {
122
+ --m_re;
123
+ return *this ;
124
+ }
125
+
126
+ complex operator -- (int )
127
+ {
128
+ complex temp (*this );
129
+ --m_re;
130
+ return temp;
131
+ }
132
+
133
+ complex operator + (const double val) const
134
+ {
135
+ return complex (m_re + val, m_im);
136
+ }
137
+
138
+ complex operator - (const double val) const
139
+ {
140
+ return complex (m_re - val, m_im);
141
+ }
142
+
143
+ complex operator * (const double val) const
144
+ {
145
+ return complex (m_re * val, m_im * val);
146
+ }
147
+
148
+ complex operator / (const double val) const
149
+ {
150
+ return complex (m_re / val, m_im / val);
151
+ }
152
+
153
+ complex& operator += (const double val)
154
+ {
155
+ m_re += val;
156
+ return *this ;
157
+ }
158
+
159
+ complex& operator -= (const double val)
160
+ {
161
+ m_re -= val;
162
+ return *this ;
163
+ }
164
+
165
+ complex& operator *= (const double val)
166
+ {
167
+ m_re *= val;
168
+ m_im *= val;
169
+ return *this ;
170
+ }
171
+
172
+ complex& operator /= (const double val)
173
+ {
174
+ m_re /= val;
175
+ m_im /= val;
176
+ return *this ;
177
+ }
178
+
179
+ friend complex operator + (const double left, const complex& right)
180
+ {
181
+ return complex (left + right.m_re , right.m_im );
182
+ }
183
+
184
+ friend complex operator - (const double left, const complex& right)
185
+ {
186
+ return complex (left - right.m_re , -right.m_im );
187
+ }
188
+
189
+ friend complex operator * (const double left, const complex& right)
190
+ {
191
+ return complex (left * right.m_re , left * right.m_im );
192
+ }
193
+
194
+ friend complex operator / (const double left, const complex& right)
195
+ {
196
+ const double denominator = right.m_re * right.m_re + right.m_im * right.m_im ;
197
+ return complex (left * right.m_re / denominator,
198
+ -left * right.m_im / denominator);
199
+ }
200
+
201
+ // Boolean operators
202
+ bool operator == (const complex &other) const
203
+ {
204
+ return m_re == other.m_re && m_im == other.m_im ;
205
+ }
206
+
207
+ bool operator != (const complex &other) const
208
+ {
209
+ return m_re != other.m_re || m_im != other.m_im ;
210
+ }
211
+
212
+ bool operator == (const double val) const
213
+ {
214
+ return m_re == val && m_im == 0 .;
215
+ }
216
+
217
+ bool operator != (const double val) const
218
+ {
219
+ return m_re != val || m_im != 0 .;
220
+ }
221
+
222
+ friend bool operator == (const double left, const complex& right)
223
+ {
224
+ return left == right.m_re && right.m_im == 0 .;
225
+ }
226
+
227
+ friend bool operator != (const double left, const complex& right)
228
+ {
229
+ return left != right.m_re || right.m_im != 0 .;
230
+ }
231
+ };
232
+
233
+ #endif
0 commit comments