forked from nkast/MonoGame
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Complex.cs
482 lines (416 loc) · 17.5 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Copyright (C)2022 Nick Kastellanos
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
namespace Microsoft.Xna.Framework
{
[DataContract]
[DebuggerDisplay("{DebugDisplayString,nq}")]
public struct Complex : IEquatable<Complex>
{
#region Private Fields
private static readonly Complex _zero = new Complex(0, 0);
private static readonly Complex _one = new Complex(1, 0);
private static readonly Complex _imaginaryOne = new Complex(0, 1);
#endregion
#region Public Fields
/// <summary>
/// The real part of this <see cref="Complex"/> number.
/// </summary>
[DataMember]
public float R;
/// <summary>
/// The imaginary part of this <see cref="Complex"/> number.
/// </summary>
[DataMember]
public float i;
#endregion
#region Properties
/// <summary>
/// Returns a <see cref="Complex"/> number with components (0, 0i).
/// </summary>
public static Complex Zero { get { return _zero; } }
/// <summary>
/// Returns a <see cref="Complex"/> number with components (1, 0i).
/// </summary>
public static Complex One { get { return _one; } }
/// <summary>
/// Returns a <see cref="Complex"/> number with components (0, 1i).
/// </summary>
public static Complex ImaginaryOne { get { return _imaginaryOne; } }
/// <summary>
/// Returns a <see cref="Complex"/> number with components (1, 0i).
/// </summary>
public static Complex Identity { get { return _one; } }
public float Phase
{
get { return (float)Math.Atan2(i, R); }
}
public float Magnitude
{
get { return (float)Math.Sqrt((R * R) + (i * i)); }
}
internal string DebugDisplayString
{
get
{
if (this == Complex.Identity)
return "Identity";
return String.Format("{{R: {0} i: {1} Magnitude: {2} Phase: {3}}}",
R, i, Magnitude, Phase);
}
}
#endregion
/// <summary>
/// Constructs a complex number with real and imaginary components.
/// </summary>
/// <param name="real">The real part of this <see cref="Complex"/> number.</param>
/// <param name="imaginary"> The imaginary part of this <see cref="Complex"/> number.</param>
public Complex(float real, float imaginary)
{
R = real;
i = imaginary;
}
/// <summary>
/// Constructs a complex number from <see cref="Vector2"/>.
/// </summary>
/// <param name="value">The R,i components.</param>
public Complex(Vector2 value)
{
R = value.X;
i = value.Y;
}
/// <summary>
/// Creates a new <see cref="Complex"/> number from the specified angle.
/// </summary>
/// /// <param name="phase">The angle in radians.</param>
/// /// <param name="magnitude">The magnitude.</param>
public static Complex CreateFromPolarCoordinates(float phase, float magnitude)
{
Complex result;
result.R = (float)Math.Cos(phase) * magnitude;
result.i = (float)Math.Sin(phase) * magnitude;
return result;
}
/// <summary>
/// Creates a new <see cref="Complex"/> number from the specified angle.
/// </summary>
/// /// <param name="angle">The angle in radians.</param>
public static Complex CreateFromAngle(float angle)
{
Complex result;
result.R = (float)Math.Cos(angle);
result.i = (float)Math.Sin(angle);
return result;
}
/// <summary>
/// Transforms this complex number into its conjugated version.
/// </summary>
public void Conjugate()
{
i = -i;
}
/// <summary>
/// Flips the sign of the all the complex number components.
/// </summary>
public void Negate()
{
R = -R;
i = -i;
}
/// <summary>
/// Returns the squared magnitude of the complex number.
/// </summary>
/// <returns>The squared magnitude of the complex number components.</returns>
public float MagnitudeSquared()
{
return (R * R) + (i * i);
}
/// <summary>
/// Scales the complex number magnitude to unit length.
/// </summary>
public void Normalize()
{
var mag = Magnitude;
R = R / mag;
i = i / mag;
}
/// <summary>
/// Gets a <see cref="Vector2"/> representation for this complex number.
/// </summary>
/// <returns>A <see cref="Vector2"/> representation for this complex number.</returns>
public Vector2 ToVector2()
{
return new Vector2(R, i);
}
internal Complex Rotate90()
{
return new Complex(-i, +R);
}
internal Complex Rotate180()
{
return new Complex(-R, -i);
}
internal Complex Rotate270()
{
return new Complex(+i, -R);
}
/// <summary>
/// Creates a new <see cref="Complex"/> number that contains a multiplication of two complex numbers.
/// </summary>
/// <param name="left">Source <see cref="Complex"/> number.</param>
/// <param name="right">Source <see cref="Complex"/> number.</param>
/// <returns>The result of the complex number multiplication.</returns>
public static Complex Multiply(Complex left, Complex right)
{
return new Complex(left.R * right.R - left.i * right.i,
left.i * right.R + left.R * right.i);
}
/// <summary>
/// Creates a new <see cref="Complex"/> number that contains a multiplication of two complex numbers.
/// </summary>
/// <param name="left">Source <see cref="Complex"/> number.</param>
/// <param name="right">Source <see cref="Complex"/> number.</param>
/// <param name="result">The result of the complex number multiplication as an output parameter.</param>
public static void Multiply(ref Complex left, ref Complex right, out Complex result)
{
result = new Complex(left.R * right.R - left.i * right.i,
left.i * right.R + left.R * right.i);
}
/// <summary>
/// Creates a new <see cref="Complex"/> that contains a multiplication of <see cref="Complex"/> and a scalar.
/// </summary>
/// <param name="left">Source <see cref="Complex"/>.</param>
/// <param name="right">Scalar value.</param>
/// <returns>The result of the Complex multiplication with a scalar.</returns>
public static Complex Multiply(Complex left, float right)
{
left.R *= right;
left.i *= right;
return left;
}
/// <summary>
/// Creates a new <see cref="Complex"/> that contains a multiplication of <see cref="Complex"/> and a scalar.
/// </summary>
/// <param name="left">Source <see cref="Complex"/>.</param>
/// <param name="right">Scalar value.</param>
/// <param name="result">The result of the multiplication with a scalar as an output parameter.</param>
public static void Multiply(ref Complex left, float right, out Complex result)
{
result.R = left.R * right;
result.i = left.i * right;
}
public static Complex Divide(Complex left, Complex right)
{
return new Complex(right.R * left.R + right.i * left.i,
right.R * left.i - right.i * left.R);
}
public static void Divide(ref Complex left, ref Complex right, out Complex result)
{
result = new Complex(right.R * left.R + right.i * left.i,
right.R * left.i - right.i * left.R);
}
/// <summary>
/// Divides the components of a <see cref="Complex"/> by a scalar.
/// </summary>
/// <param name="left">Source <see cref="Complex"/>.</param>
/// <param name="right">Divisor scalar.</param>
/// <returns>The result of dividing a Complex by a scalar.</returns>
public static Complex Divide(Complex left, float right)
{
left.R /= right;
left.i /= right;
return left;
}
/// <summary>
/// Divides the components of a <see cref="Complex"/> by a scalar.
/// </summary>
/// <param name="left">Source <see cref="Complex"/>.</param>
/// <param name="right">Divisor scalar.</param>
/// <param name="result">The result of dividing a Complex by a scalar as an output parameter.</param>
public static void Divide(ref Complex left, float right, out Complex result)
{
result.R = left.R / right;
result.i = left.i / right;
}
/// <summary>
/// Transforms this complex number into its conjugated version.
/// </summary>
/// <param name="value">The complex number which values will be used to create the conjugated version.</param>
/// <returns>The conjugate version of the specified complex number.</returns>
public static Complex Conjugate(Complex value)
{
return new Complex(value.R, -value.i);
}
/// <summary>
/// Transforms this complex number into its conjugated version.
/// </summary>
/// <param name="value">The complex number which values will be used to create the conjugated version.</param>
/// <param name="result">The conjugated version of the specified complex number as an output parameter.</param>
public static void Conjugate(ref Complex value, ref Complex result)
{
result = new Complex(value.R, -value.i);
}
/// <summary>
/// Flips the sign of the all the complex number components.
/// </summary>
/// <param name="value">Source <see cref="Complex"/>.</param>
/// <returns>The result of the complex number negation.</returns>
public static Complex Negate(Complex value)
{
return new Complex(-value.R, -value.i);
}
/// <summary>
/// Flips the sign of the all the complex number components.
/// </summary>
/// <param name="value">Source <see cref="Complex"/>.</param>
/// <param name="result">The result of the complex number negation as an output parameter.</param>
public static void Negate(ref Complex value, ref Complex result)
{
result = new Complex(-value.R, -value.i);
}
/// <summary>
/// Scales the complex number magnitude to unit length.
/// </summary>
/// <param name="value">Source <see cref="Complex"/>.</param>
/// <returns>The unit length complex number.</returns>
public static Complex Normalize(Complex value)
{
var mag = value.Magnitude;
return new Complex(value.R / mag, -value.i / mag);
}
/// <summary>
/// Scales the complex number magnitude to unit length.
/// </summary>
/// <param name="value">Source <see cref="Complex"/>.</param>
/// <param name="result">The unit length complex number as an output parameter.</param>
public static void Normalize(ref Complex value, ref Complex result)
{
var mag = value.Magnitude;
result = new Complex(value.R / mag, -value.i / mag);
}
/// <summary>
/// Deconstruction method for <see cref="Complex"/>.
/// </summary>
/// <param name="real">The real part of this <see cref="Complex" /> number.</param>
/// <param name="imaginary">The imaginary part of this <see cref="Complex"/> number.</param>
public void Deconstruct(out float real, out float imaginary)
{
real = this.R;
imaginary = this.i;
}
#region Operators
/// <summary>
/// Flips the sign of the all the complex number components.
/// </summary>
/// <param name="value">Source <see cref="Complex"/> on the right of the sub sign.</param>
/// <returns>The result of the complex number negation.</returns>
public static Complex operator -(Complex value)
{
return new Complex(-value.R, -value.i);
}
/// <summary>
/// Multiplies two complex numbers.
/// </summary>
/// <param name="left">Source <see cref="Complex"/> number on the left of the mul sign.</param>
/// <param name="right">Source <see cref="Complex"/> number on the right of the mul sign.</param>
/// <returns>Result of the complex numbers multiplication.</returns>
public static Complex operator *(Complex left, Complex right)
{
return new Complex(left.R * right.R - left.i * right.i,
left.i * right.R + left.R * right.i);
}
/// <summary>
/// Multiplies the components of a <see cref="Complex"/> number by a scalar.
/// </summary>
/// <param name="left">Source <see cref="Complex"/> on the left of the mul sign.</param>
/// <param name="right">Scalar value on the right of the mul sign.</param>
/// <returns>Result of the complex number multiplication with a scalar.</returns>
public static Complex operator *(Complex left, float right)
{
left.R *= right;
left.i *= right;
return left;
}
/// <summary>
/// Divides a <see cref="Complex"/> number by the other <see cref="Complex"/> number.
/// </summary>
/// <param name="left">Source <see cref="Complex"/> number on the left of the div sign.</param>
/// <param name="right">Divisor <see cref="Complex"/> number on the right of the div sign.</param>
/// <returns>The result of dividing the complex numbers.</returns>
public static Complex operator /(Complex left, Complex right)
{
return new Complex(right.R * left.R + right.i * left.i,
right.R * left.i - right.i * left.R);
}
/// <summary>
/// Divides the components of a <see cref="Complex"/> number by a scalar.
/// </summary>
/// <param name="left">Source <see cref="Complex"/> on the left of the div sign.</param>
/// <param name="right">Divisor scalar on the right of the div sign.</param>
/// <returns>The result of dividing a complex number by a scalar.</returns>
public static Complex operator /(Complex left, float right)
{
left.R /= right;
left.i /= right;
return left;
}
/// <summary>
/// Compares whether two <see cref="Complex"/> instances are equal.
/// </summary>
/// <param name="left"><see cref="Complex"/> instance on the left of the equal sign.</param>
/// <param name="right"><see cref="Complex"/> instance on the right of the equal sign.</param>
/// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
public static bool operator ==(Complex left, Complex right)
{
return left.R == right.R && left.i == right.i;
}
/// <summary>
/// Compares whether two <see cref="Complex"/> instances are not equal.
/// </summary>
/// <param name="left"><see cref="Complex"/> instance on the left of the not equal sign.</param>
/// <param name="right"><see cref="Complex"/> instance on the right of the not equal sign.</param>
/// <returns><c>true</c> if the instances are not equal; <c>false</c> otherwise.</returns>
public static bool operator !=(Complex left, Complex right)
{
return left.R != right.R || left.i != right.i;
}
#endregion
#region IEquatable<Complex>
/// <summary>
/// Compares whether current instance is equal to specified <see cref="Object"/>.
/// </summary>
/// <param name="obj">The <see cref="Object"/> to compare.</param>
/// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
public override bool Equals(object obj)
{
if (obj is Complex)
return Equals((Complex)obj);
return false;
}
/// <summary>
/// Compares whether current instance is equal to specified <see cref="Complex"/>.
/// </summary>
/// <param name="other">The <see cref="Complex"/> to compare.</param>
/// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
public bool Equals(Complex other)
{
return R == other.R &&
i == other.i;
}
#endregion
/// <summary>
/// Gets the hash code of this <see cref="Complex"/>.
/// </summary>
/// <returns>Hash code of this <see cref="Complex"/>.</returns>
public override int GetHashCode()
{
return R.GetHashCode() + i.GetHashCode();
}
/// <inheritdoc/>
public override string ToString()
{
return String.Format("{{R: {0}, i: {1} }}", R, i);
}
}
}