-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathComplex.cpp
190 lines (161 loc) · 3.47 KB
/
Complex.cpp
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
#include "Complex.h"
#include <iostream>
#include <cmath>
using namespace std;
namespace DDG
{
MyComplex::MyComplex( double a, double b )
// constructs number a+bi
: re( a ), im( b )
{}
void MyComplex::operator+=( const MyComplex& z )
// add z
{
re += z.re;
im += z.im;
}
void MyComplex::operator-=( const MyComplex& z )
// subtract z
{
re -= z.re;
im -= z.im;
}
void MyComplex::operator*=( const MyComplex& z )
// MyComplex multiply by z
{
double a = re;
double b = im;
double c = z.re;
double d = z.im;
re = a*c-b*d;
im = a*d+b*c;
}
void MyComplex::operator*=( double r )
// scalar multiply by r
{
re *= r;
im *= r;
}
void MyComplex::operator/=( double r )
// scalar divide by r
{
re /= r;
im /= r;
}
void MyComplex::operator/=( const MyComplex& z )
// scalar divide by r
{
*this *= z.inv();
}
MyComplex MyComplex::operator-( void ) const
{
return MyComplex( -re, -im );
}
MyComplex MyComplex::conj( void ) const
// returns MyComplex conjugate
{
return MyComplex( re, -im );
}
MyComplex MyComplex::inv( void ) const
// returns inverse
{
return this->conj() / this->norm2();
}
double MyComplex::arg( void ) const
// returns argument
{
return atan2( im, re );
}
double MyComplex::norm( void ) const
// returns norm
{
return sqrt( re*re + im*im );
}
double MyComplex::norm2( void ) const
// returns norm squared
{
return re*re + im*im;
}
MyComplex MyComplex::unit( void ) const
// returns complex number with unit norm and same modulus
{
return *this / this->norm();
}
MyComplex MyComplex::exponential( void ) const
// complex exponentiation
{
return exp( re ) * MyComplex( cos( im ), sin( im ));
}
MyComplex operator+( const MyComplex& z1, const MyComplex& z2 )
// binary addition
{
MyComplex z = z1;
z += z2;
return z;
}
MyComplex operator-( const MyComplex& z1, const MyComplex& z2 )
// binary subtraction
{
MyComplex z = z1;
z -= z2;
return z;
}
MyComplex operator*( const MyComplex& z1, const MyComplex& z2 )
// binary MyComplex multiplication
{
MyComplex z = z1;
z *= z2;
return z;
}
MyComplex operator*( const MyComplex& z, double r )
// right scalar multiplication
{
MyComplex zr = z;
zr *= r;
return zr;
}
MyComplex operator*( double r, const MyComplex& z )
// left scalar multiplication
{
return z*r;
}
MyComplex operator/( const MyComplex& z, double r )
// scalar division
{
MyComplex zr = z;
zr /= r;
return zr;
}
MyComplex operator/( const MyComplex& z1, const MyComplex& z2 )
// complex division
{
MyComplex z = z1;
z /= z2;
return z;
}
double dot( const MyComplex& z1, const MyComplex& z2 )
{
return z1.re*z2.re + z1.im*z2.im;
}
double cross( const MyComplex& z1, const MyComplex& z2 )
{
return z1.re*z2.im - z1.im*z2.re;
}
std::ostream& operator<<( std::ostream& os, const MyComplex& z )
// prints components
{
if( z.im > 0 )
{
os << z.re << " + " << z.im << "i";
}
else if( z.im < 0 )
{
os << z.re << " - " << -z.im << "i";
}
else
{
os << z.re;
}
return os;
}
}