-
Notifications
You must be signed in to change notification settings - Fork 16
/
Complex.cs
330 lines (292 loc) · 8.82 KB
/
Complex.cs
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
using System;
using System.Collections.Generic;
using System.Text;
// Copyright (c) 2006, 2007 by Hugh Pyle, inguzaudio.com
namespace DSPUtil
{
/// <summary>
/// Implementation of a double-precision (64-bit float) complex number
/// </summary>
//[Serializable]
public struct Complex
{
// Real and Imaginary parts of a Complex number
public double Re;
public double Im;
public Complex(Complex c)
{
this.Re = c.Re;
this.Im = c.Im;
}
public Complex(double real, double imaginary)
{
this.Re = real;
this.Im = imaginary;
}
public Complex(double phase)
{
this.Re = Math.Cos(phase);
this.Im = Math.Sin(phase);
}
// Accessor methods for accessing/setting private variables
/// <summary>
/// Real component
/// </summary>
public double Real
{
get { return Re; }
set { Re = value; }
}
/// <summary>
/// Imaginary component
/// </summary>
public double Imaginary
{
get { return Im; }
set { Im = value; }
}
/// <summary>
/// Magnitude (always positive)
/// </summary>
public double Magnitude
{
get
{
double abs = Math.Sqrt(Im * Im + Re * Re);
// if (Re > 0) abs = -abs;
return abs;
}
}
/// <summary>
/// Phase, radians
/// </summary>
public double Phase
{
get
{
return Math.Atan2(Im, Re);
}
}
/// <summary>
/// Set value
/// </summary>
/// <param name="re">Real</param>
/// <param name="im">Imaginary</param>
public void Set(double re, double im)
{
Re = re;
Im = im;
}
/// <summary>
/// In-place multiply
/// </summary>
/// <param name="c">Complex</param>
public void mul(Complex c)
{
double gem = c.Re * Re - c.Im * Im;
Im = c.Im * Re + c.Re * Im;
Re = gem;
}
/// <summary>
/// In-place multiply
/// </summary>
/// <param name="d">double</param>
public void mul(double d)
{
Im *= d;
Re *= d;
}
/// <summary>
/// In-place divide: this=this/arg
/// </summary>
/// <param name="c">Complex</param>
public void div(Complex c)
{
double d = c.Re * c.Re + c.Im * c.Im;
//if (d == 0) throw new DivideByZeroException();
double r = (Re * c.Re + Im * c.Im) / d;
double i = (Im * c.Re - Re * c.Im) / d;
Re = r;
Im = i;
}
/// <summary>
/// In-place inverted divide: this = arg/this
/// </summary>
/// <param name="c">Complex</param>
public void idiv(Complex c)
{
double d = Re * Re + Im * Im;
//if (d == 0) throw new DivideByZeroException();
double r = (c.Re * Re + c.Im * Im) / d;
double i = (c.Im * Re - c.Re * Im) / d;
Re = r;
Im = i;
}
/// <summary>
/// Raises to a power
/// </summary>
/// <param name="c">Complex, power</param>
public void Pow(Complex y)
{
// http://home.att.net/~srschmitt/complexnumbers.html
double r = Magnitude;
double t = Phase;
double c = y.Re;
double d = y.Im;
Re = Math.Pow(r, c) * Math.Exp(-d * t) * Math.Cos(c * t + d * Math.Log(r));
Im = Math.Pow(r, c) * Math.Exp(-d * t) * Math.Sin(c * t + d * Math.Log(r));
}
/// <summary>
/// Implicit conversion of Complex to double
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public static implicit operator double(Complex c)
{
return c.Re;
}
/// <summary>
/// Explicit cast of double to Complex
/// </summary>
/// <param name="f"></param>
/// <returns></returns>
public static explicit operator Complex(double f)
{
return new Complex(f, 0);
}
/// <summary>
/// operator add
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public static Complex operator +(Complex c)
{
return c;
}
/// <summary>
/// operator subtract
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public static Complex operator -(Complex c)
{
return new Complex(-c.Re, -c.Im);
}
/// <summary>
/// Complex Conjugate operator
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public static Complex operator ~(Complex c)
{
return new Complex(c.Re, -c.Im);
}
/// <summary>
/// operator add
/// </summary>
/// <param name="c1"></param>
/// <param name="c2"></param>
/// <returns></returns>
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.Re + c2.Re, c1.Im + c2.Im);
}
public static Complex operator +(Complex c1, double num)
{
return new Complex(c1.Re + num, c1.Im);
}
public static Complex operator +(double num, Complex c1)
{
return new Complex(c1.Re + num, c1.Im);
}
/// <summary>
/// Operator subtract
/// </summary>
/// <param name="c1"></param>
/// <param name="num"></param>
/// <returns></returns>
public static Complex operator -(Complex c1, double num)
{
return new Complex(c1.Re - num, c1.Im);
}
public static Complex operator -(double num, Complex c1)
{
return new Complex(num - c1.Re, -c1.Im);
}
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex(c1.Re - c2.Re, c1.Im - c2.Im);
}
/// <summary>
/// Operator multiply
/// </summary>
/// <param name="c1"></param>
/// <param name="c2"></param>
/// <returns></returns>
public static Complex operator *(Complex c1, Complex c2)
{
return new Complex((c1.Re * c2.Re) - (c1.Im * c2.Im), (c1.Re * c2.Im) + (c1.Im * c2.Re));
}
public static Complex operator *(Complex c1, double num)
{
return new Complex(c1.Re * num, c1.Im * num);
}
public static Complex operator *(double num, Complex c1)
{
return new Complex(c1.Re * num, c1.Im * num);
}
/// <summary>
/// Operator divide
/// </summary>
/// <param name="c1"></param>
/// <param name="c2"></param>
/// <returns></returns>
public static Complex operator /(Complex c1, Complex c2)
{
double div = c2.Re * c2.Re + c2.Im * c2.Im;
//if (div == 0) throw new DivideByZeroException();
return new Complex((c1.Re * c2.Re + c1.Im * c2.Im) / div,
(c1.Im * c2.Re - c1.Re * c2.Im) / div);
}
public static Complex operator /(double num, Complex c1)
{
return new Complex(num / c1.Re, num / c1.Im);
}
public static Complex operator /(Complex c1, double num)
{
return new Complex(c1.Re / num, c1.Im / num);
}
/// <summary>
/// Operator equalitytest
/// </summary>
/// <param name="c1"></param>
/// <param name="c2"></param>
/// <returns></returns>
public static bool operator ==(Complex c1, Complex c2)
{
return (c1.Re == c2.Re) && (c1.Im == c2.Im);
}
/// <summary>
/// Operator negative equalitytest
/// </summary>
/// <param name="c1"></param>
/// <param name="c2"></param>
/// <returns></returns>
public static bool operator !=(Complex c1, Complex c2)
{
return (c1.Re != c2.Re) || (c1.Im != c2.Im);
}
public override int GetHashCode()
{
return (Re.GetHashCode() ^ Im.GetHashCode());
}
public override bool Equals(object obj)
{
return (obj is Complex) ? (this == (Complex)obj) : false;
}
public override string ToString()
{
return (String.Format("{0} + {1}i", Real, Imaginary));
}
}
}