-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathComplex.cpp
More file actions
375 lines (327 loc) · 9.01 KB
/
Complex.cpp
File metadata and controls
375 lines (327 loc) · 9.01 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
* Complex number class template by igor
* Stores and manipulates complex numbers represented by a pair
* of numbers of any type.
*
* REQUIREMENTS on type T:
* - -1, 0 and 1 must be valid parameters to T's constructor
* - all basic arithmetic operators must be defined for T
*
* INVARIANTS:
* - capacity is never smaller than 16
* - capacity is not the smallest it can be because every
* modifying member function first grows digits as much as
* it needs and then does its job.
*
* This file is part of my library of algorithms found here:
* http://www.palmcommander.com:8081/tools/
* LICENSE:
* http://www.palmcommander.com:8081/tools/LICENSE.html
* Copyright (c) 2004
* Contact author:
* igor at cs.ubc.ca
**/
#include <string>
#include <iostream>
#include <math.h>
#include <sstream>
#ifndef min
#define min(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef max
#define max(x,y) ((x) > (y) ? (x) : (y))
#endif
template< class T >
class Complex
{
public:
/** Real and Imaginary parts. **/
T re, im;
/** Creates a new Complex number with value zero. **/
Complex();
/** Creates a new Complex number "re + i*0". **/
Complex( T re );
/** Creates a new Complex number "re + i*im". **/
Complex( T re, T im );
/** Arithmetic **/
Complex< T > &operator++();
Complex< T > &operator++( int );
Complex< T > &operator--();
Complex< T > &operator--( int );
Complex< T > operator- ();
Complex< T > operator+ ( const T &n ) const;
Complex< T > operator+ ( const Complex< T > &n ) const;
Complex< T > &operator+=( const T &n );
Complex< T > &operator+=( const Complex< T > &n );
Complex< T > operator- ( const T &n ) const;
Complex< T > operator- ( const Complex< T > &n ) const;
Complex< T > &operator-=( const T &n );
Complex< T > &operator-=( const Complex< T > &n );
Complex< T > operator* ( const T &n ) const;
Complex< T > operator* ( const Complex< T > &n ) const;
Complex< T > &operator*=( const T &n );
Complex< T > &operator*=( const Complex< T > &n );
Complex< T > operator/ ( const T &n ) const;
Complex< T > operator/ ( const Complex< T > &n ) const;
Complex< T > &operator/=( const T &n );
Complex< T > &operator/=( const Complex< T > &n );
/** Casting and type conversion **/
bool operator!() const;
//operator bool();
//operator T();
operator string() const;
string toString() const;
/** Outputs decimal value to stdout **/
void print() const;
/** I/O Friends **/
friend istream &operator>><>( istream &in, const Complex< T > &n );
friend ostream &operator<<<>( ostream &out, const Complex< T > &n );
/** Math **/
friend T abs2<>( const Complex< T > &n );
friend T abs<>( const Complex< T > &n );
/*
friend long double log2( Complex x, long double epsilon = 0.000000000000001 );
inline friend long double log( Complex x, long double epsilon = 0.000000000000001 );
inline friend long double log10( Complex x, long double epsilon = 0.000000000000001 );
inline friend long double lg( Complex x, long double epsilon = 0.000000000000001 );
inline friend long double ln( Complex x, long double epsilon = 0.000000000000001 );
*/
};
template< class T > istream &operator>>( istream &in, const Complex< T > &n );
template< class T > ostream &operator<<( ostream &out, const Complex< T > &n );
template< class T > T abs2( const Complex< T > &n );
template< class T > T abs( const Complex< T > &n );
template< class T >
Complex< T >::Complex()
{
re = im = T( 0 );
}
template< class T >
Complex< T >::Complex( T re )
{
this->re = re;
this->im = T( 0 );
}
template< class T >
Complex< T >::Complex( T re, T im )
{
this->re = re;
this->im = im;
}
template< class T >
Complex< T > &Complex< T >::operator++()
{
Complex old = *this;
this->re += T( 1 );
return old;
}
template< class T >
Complex< T > &Complex< T >::operator++( int )
{
this->re += T( 1 );
return *this;
}
template< class T >
Complex< T > &Complex< T >::operator--()
{
Complex old = *this;
this->re -= T( 1 );
return old;
}
template< class T >
Complex< T > &Complex< T >::operator--( int )
{
this->re -= T( 1 );
return *this;
}
template< class T >
Complex< T > Complex< T >::operator-()
{
return Complex< T >( -re, -im );
}
template< class T >
Complex< T > Complex< T >::operator+( const T &n ) const
{
return Complex< T >( re + n, im );
}
template< class T >
Complex< T > Complex< T >::operator+( const Complex< T > &n ) const
{
return Complex< T >( re + n.re, im + n.im );
}
template< class T >
Complex< T > &Complex< T >::operator+=( const T &n )
{
re += n;
return *this;
}
template< class T >
Complex< T > &Complex< T >::operator+=( const Complex< T > &n )
{
re += n.re;
im += n.im;
return *this;
}
template< class T >
Complex< T > Complex< T >::operator-( const T &n ) const
{
return Complex< T >( re - n, im );
}
template< class T >
Complex< T > Complex< T >::operator-( const Complex< T > &n ) const
{
return Complex< T >( re - n.re, im - n.im );
}
template< class T >
Complex< T > &Complex< T >::operator-=( const T &n )
{
re -= n;
return *this;
}
template< class T >
Complex< T > &Complex< T >::operator-=( const Complex< T > &n )
{
re -= n.re;
im -= n.im;
return *this;
}
template< class T >
Complex< T > Complex< T >::operator*( const T &n ) const
{
return Complex< T >( re * n, im * n );
}
template< class T >
Complex< T > Complex< T >::operator*( const Complex< T > &n ) const
{
return Complex< T >( re * n.re - im * n.im, re * n.im + im * n.re );
}
template< class T >
Complex< T > &Complex< T >::operator*=( const T &n )
{
re *= n;
im *= n;
return *this;
}
template< class T >
Complex< T > &Complex< T >::operator*=( const Complex< T > &n )
{
return *this = operator*( n );
}
template< class T >
Complex< T > Complex< T >::operator/( const T &n ) const
{
return Complex< T >( re / n, im / n );
}
template< class T >
Complex< T > Complex< T >::operator/( const Complex< T > &n ) const
{
T f = T( 1 ) / abs2( n );
return Complex< T >( f * ( re * n.re + im * n.im ), f * ( im * n.re - re * n.im ) );
}
template< class T >
Complex< T > &Complex< T >::operator/=( const T &n )
{
re /= n;
im /= n;
return *this;
}
template< class T >
Complex< T > &Complex< T >::operator/=( const Complex< T > &n )
{
return *this = operator/( n );
}
/** Casting **/
template< class T >
bool Complex< T >::operator!() const
{
return !abs2< T >( *this );
}
/** Overloaded casting operators and templates don't mix!
template< class T >
Complex< T >::operator bool()
{
return abs2< T >( *this );
}
**/
/** Returns the real part only!
template< class T >
Complex< T >::operator T()
{
return re;
}
**/
template< class T >
Complex< T >::operator string() const
{
return toString();
}
template< class T >
string Complex< T >::toString() const
{
ostringstream out;
out << re << ( im < T( 0 ) ? " - I*" : " + I*" ) << im * ( im < T( 0 ) ? T( -1 ) : T( 1 ) );
return out.str();
}
/** Outputs decimal value to stdout **/
template< class T >
void Complex< T >::print() const
{
cout << *this;
}
/** I/O Friends **/
template< class T >
istream &operator>>( istream &in, const Complex< T > &n )
{
in >> n.re;
char sig; in >> sig;
if( sig != '+' & sig != '-' )
{
in.clear( ios::failbit );
return;
}
char i; in >> i;
if( i != 'I' && i != 'i' )
{
in.clear( ios::failbit );
return;
}
in >> n.im;
if( sig == '-' ) n.im *= T( -1 );
return in;
}
template< class T >
ostream &operator<<( ostream &out, const Complex< T > &n )
{
return( out << ( string )n );
}
/** Math **/
template< class T >
T abs2( const Complex< T > &x )
{
return x.re * x.re + x.im * x.im;
}
template< class T >
T abs( const Complex< T > &x )
{
return sqrt( abs2( x ) );
}
/** TESTING and DEBUGGING **/
typedef Complex< double > CD;
int main()
{
cout << "Constructors and printing" << endl;
cout << "0 = " << CD().toString() << endl;
cout << "1 = " << CD( 1. ).toString() << endl;
cout << "i = " << ( string )CD( 0., 1. ) << endl;
cout << "1.5 + I*(-3.4) = " << ( string )CD( 1.5, -3.4 ) << endl;
cout << endl;
cout << "Algebra" << endl;
cout << "0 + i = " << ( CD() + CD( 0., 1. ) ).toString() << endl;
cout << "(1 + i) + (2 - i) = " << ( string )( CD( CD(1., 1.) + CD(2., -1.) )) << endl;
cout << "(5 + i) - (5 - i) = " << ( string )( CD(5., 1.) - CD(5., -1.) ) << endl;
cout << "(2 + 3i)*(3 - 3i) = " << ( string )( CD(2., 3.) * CD(3., -3.) ) << endl;
cout << "(-1 + 5i)/(2 + 3i) = " << ( string )( CD(-1., 5.) / CD(2., 3.) ) << endl;
cout << "(0 + 0i)/(0 + 0i) = " << (CD() / CD()) << endl;
cout << endl;
return 0;
}