-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplex.cs
665 lines (606 loc) · 19.7 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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// A two-dimensional complex number. The imaginary component is a
/// coefficient of i, or the square-root of negative one.
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Explicit, Pack = 8)]
public readonly struct Complex : IComparable<Complex>, IEquatable<Complex>, IEnumerable
{
/// <summary>
/// The coefficient of the imaginary component i.
/// </summary>
[FieldOffset(4)] private readonly float imag;
/// <summary>
/// The real component.
/// </summary>
[FieldOffset(0)] private readonly float real;
/// <summary>
/// The coefficient of the imaginary component i.
/// </summary>
/// <value>imaginary</value>
public float Imag { get { return this.imag; } }
/// <summary>
/// Gets the number of components held by the complex number.
/// </summary>
/// <value>length</value>
public int Length { get { return 2; } }
/// <summary>
/// The real component.
/// </summary>
/// <value>real number</value>
public float Real { get { return this.real; } }
/// <summary>
/// Retrieves a component by index. When the provided index is 0 or -2,
/// returns real; 1 or -1, imaginary.
/// </summary>
/// <value>component</value>
public float this[int i]
{
get
{
return i switch
{
0 or -2 => this.real,
1 or -1 => this.imag,
_ => 0.0f,
};
}
}
/// <summary>
/// Constructs a complex number from single precision numbers.
/// </summary>
/// <param name="real">real number</param>
/// <param name="imag">imaginary number</param>
public Complex(in float real = 0.0f, in float imag = 0.0f)
{
this.real = real;
this.imag = imag;
}
/// <summary>
/// Tests this complex mumber for equivalence with an object. For
/// approximate equality with another complex number, use the static approx
/// function instead.
/// </summary>
/// <param name="value">object</param>
/// <returns>equivalence</returns>
public override bool Equals(object value)
{
if (Object.ReferenceEquals(this, value)) { return true; }
if (value is null) { return false; }
if (value is Complex complex) { return this.Equals(complex); }
return false;
}
/// <summary>
/// Returns a hash code representing this complex number.
/// </summary>
/// <returns>hash code</returns>
public override int GetHashCode()
{
unchecked
{
return (Utils.MulBase ^ this.real.GetHashCode()) *
Utils.HashMul ^ this.imag.GetHashCode();
}
}
/// <summary>
/// Returns a string representation of this complex number.
/// </summary>
/// <returns>string</returns>
public override string ToString()
{
return Complex.ToString(this);
}
/// <summary>
/// Compares this complex number to another.
/// Returns 1 when a component of this vector is
/// greater than another; -1 when lesser. Prioritizes the imaginary
/// component over the real component. Returns 0 as a last resort.
/// </summary>
/// <param name="z">comparisand</param>
/// <returns>evaluation</returns>
public int CompareTo(Complex z)
{
return (this.imag < z.imag) ? -1 :
(this.imag > z.imag) ? 1 :
(this.real < z.real) ? -1 :
(this.real > z.real) ? 1 :
0;
}
/// <summary>
/// Tests this complex number for equivalence with another in compliance
/// with the IEquatable interface. For approximate equality with another
/// complex number, use the static approx function instead.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>equivalence</returns>
public bool Equals(Complex z)
{
if (this.real.GetHashCode() != z.real.GetHashCode()) { return false; }
if (this.imag.GetHashCode() != z.imag.GetHashCode()) { return false; }
return true;
}
/// <summary>
/// Returns an enumerator (or iterator) for this complex number, allowing
/// its components to be accessed in a foreach loop.
/// </summary>
/// <returns>enumerator</returns>
public IEnumerator GetEnumerator()
{
yield return this.real;
yield return this.imag;
}
/// <summary>
/// Promotes a real number to a complex number.
/// </summary>
/// <param name="s">real number</param>
public static implicit operator Complex(in float s)
{
return new(s, 0.0f);
}
/// <summary>
/// Converts a complex number to a 2D vector.
/// </summary>
/// <param name="z">complex number</param>
public static implicit operator Vec2(in Complex z)
{
return new Vec2(z.real, z.imag);
}
/// <summary>
/// Converts a 2D vector to a complex number.
/// </summary>
/// <param name="v">vector</param>
public static implicit operator Complex(in Vec2 v)
{
return new(v.X, v.Y);
}
/// <summary>
/// A complex number evaluates to true when any of its components are not
/// equal to zero.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>evaluation</returns>
public static bool operator true(in Complex z)
{
return Complex.Any(z);
}
/// <summary>
/// A complex number evaluates to false when all of its components are equal
/// to zero.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>evaluation</returns>
public static bool operator false(in Complex z)
{
return Complex.None(z);
}
/// <summary>
/// Negates a complex number.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>the negation</returns>
public static Complex operator -(in Complex z)
{
return new(-z.real, -z.imag);
}
/// <summary>
/// Multiplies two complex numbers. Complex multiplication is not
/// commutative.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>product</returns>
public static Complex operator *(in Complex a, in Complex b)
{
return new(
a.real * b.real - a.imag * b.imag,
a.real * b.imag + a.imag * b.real);
}
/// <summary>
/// Multiplies a complex and real number.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>product</returns>
public static Complex operator *(in Complex a, in float b)
{
return new(a.real * b, a.imag * b);
}
/// <summary>
/// Multiplies a real and complex number.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>product</returns>
public static Complex operator *(in float a, in Complex b)
{
return new(a * b.real, a * b.imag);
}
/// <summary>
/// Divides one complex number by another. Equivalent to multiplying the
/// numerator and the inverse of the denominator.
/// </summary>
/// <param name="a">numerator</param>
/// <param name="b">denominator</param>
/// <returns>quotient</returns>
public static Complex operator /(in Complex a, in Complex b)
{
return a * Complex.Inverse(b);
}
/// <summary>
/// Divides a complex number by a real number.
/// </summary>
/// <param name="a">numerator</param>
/// <param name="b">denominator</param>
/// <returns>quotient</returns>
public static Complex operator /(in Complex a, in float b)
{
if (b != 0.0f) { return new(a.real / b, a.imag / b); }
return Complex.Zero;
}
/// <summary>
/// Divides a real number by a complex number.
/// </summary>
/// <param name="a">numerator</param>
/// <param name="b">denominator</param>
/// <returns>quotient</returns>
public static Complex operator /(in float a, in Complex b)
{
return a * Complex.Inverse(b);
}
/// <summary>
/// Adds two complex numbers.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>sum</returns>
public static Complex operator +(in Complex a, in Complex b)
{
return new(a.real + b.real, a.imag + b.imag);
}
/// <summary>
/// Adds a complex and real number.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>sum</returns>
public static Complex operator +(in Complex a, in float b)
{
return new(a.real + b, a.imag);
}
/// <summary>
/// Adds a real and complex number.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>sum</returns>
public static Complex operator +(in float a, in Complex b)
{
return new(a + b.real, b.imag);
}
/// <summary>
/// Subtracts the left complex number from the right.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>difference</returns>
public static Complex operator -(in Complex a, in Complex b)
{
return new(a.real - b.real, a.imag - b.imag);
}
/// <summary>
/// Subtracts a real number from a complex number.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>difference</returns>
public static Complex operator -(in Complex a, in float b)
{
return new(a.real - b, a.imag);
}
/// <summary>
/// Subtracts a complex number from a real number.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>difference</returns>
public static Complex operator -(in float a, in Complex b)
{
return new(a - b.real, -b.imag);
}
/// <summary>
/// Finds the absolute of a complex number. Similar to a vector's magnitude.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>absolute</returns>
public static float Abs(in Complex z)
{
return MathF.Sqrt(Complex.AbsSq(z));
}
/// <summary>
/// Finds the absolute squared of a complex number. Similar to a vector's
/// magnitude squared.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>absolute squared</returns>
public static float AbsSq(in Complex z)
{
return z.real * z.real + z.imag * z.imag;
}
/// <summary>
/// Tests to see if all of the complex number's components are non-zero.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>evaluation</returns>
public static bool All(in Complex z)
{
return z.real != 0.0f && z.imag != 0.0f;
}
/// <summary>
/// Tests to see if any of the complex number's components are non-zero.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>evaluation</returns>
public static bool Any(in Complex z)
{
return z.real != 0.0f || z.imag != 0.0f;
}
/// <summary>
/// Evaluates whether or not two complex numbers approximate each other
/// according to a tolerance.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <param name="tol">tolerance</param>
/// <returns>evaluation</returns>
public static bool Approx(
in Complex a, in Complex b,
in float tol = Utils.Epsilon)
{
return Utils.Approx(a.real, b.real, tol) &&
Utils.Approx(a.imag, b.imag, tol);
}
/// <summary>
/// Finds the conjugate of the complex number, where the imaginary component
/// where the imaginary component is negated.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>conjugate</returns>
public static Complex Conj(in Complex z)
{
return new(z.real, -z.imag);
}
/// <summary>
/// Finds the cosine of a complex number.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>cosine</returns>
public static Complex Cos(in Complex z)
{
double zr = z.real;
double zi = z.imag;
return new(
(float)(Math.Cos(zr) * Math.Cosh(zi)),
(float)(-Math.Sin(zr) * Math.Sinh(zi)));
}
/// <summary>
/// Returns Euler's number, e, raised to a complex number.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>result</returns>
public static Complex Exp(in Complex z)
{
return Complex.Rect(MathF.Exp(z.real), z.imag);
}
/// <summary>
/// Returns the inverse, or reciprocal, of the complex number.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>inverse</returns>
public static Complex Inverse(in Complex z)
{
float absSq = Complex.AbsSq(z);
if (absSq > 0.0f)
{
return new(
z.real / absSq, -z.imag / absSq);
}
return Complex.Zero;
}
/// <summary>
/// Finds the complex logarithm.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>logarithm</returns>
public static Complex Log(in Complex z)
{
return new(
MathF.Log(Complex.Abs(z)),
Complex.Phase(z));
}
/// <summary>
/// Performs the Mobius transformation on the variable z. Uses the formula
/// (c z + d) / (a z + b) .
/// </summary>
/// <returns>mobius transformation</returns>
public static Complex Mobius(
in Complex a,
in Complex b,
in Complex c,
in Complex d,
in Complex z)
{
// Denominator: (c * z) + d .
float czdr = c.real * z.real - c.imag * z.imag + d.real;
float czdi = c.real * z.imag + c.imag * z.real + d.imag;
float mSq = czdr * czdr + czdi * czdi;
if (mSq <= 0.0f) { return Complex.Zero; }
// Numerator: (a * z) + b .
float azbr = a.real * z.real - a.imag * z.imag + b.real;
float azbi = a.real * z.imag + a.imag * z.real + b.imag;
// Find inverse.
float mSqInv = 1.0f / mSq;
float czdrInv = czdr * mSqInv;
float czdiInv = -czdi * mSqInv;
// Multiply numerator with inverse of denominator.
return new(
azbr * czdrInv - azbi * czdiInv,
azbr * czdiInv + azbi * czdrInv);
}
/// <summary>
/// Tests to see if all the complex number's components are zero.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>evaluation</returns>
public static bool None(in Complex z)
{
return z.real == 0.0f && z.imag == 0.0f;
}
/// <summary>
/// Finds the signed phase of a complex number. Similar to a 2D vector's
/// heading.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>phase</returns>
public static float Phase(in Complex z)
{
return MathF.Atan2(z.imag, z.real);
}
/// <summary>
/// Returns a named value tuple with the radius and angle of a complex
/// number.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>tuple</returns>
public static (float r, float phi) Polar(in Complex z)
{
return (r: Complex.Abs(z), phi: Complex.Phase(z));
}
/// <summary>
/// Raises a complex number to the power of another. Uses the formula
/// pow ( a, b ) := exp ( b log ( a ) )
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static Complex Pow(in Complex a, in Complex b)
{
return Complex.Exp(b * Complex.Log(a));
}
/// <summary>
/// Raises a complex number to the power of a real number. Uses the formula
/// pow ( a, b ) := exp ( b log ( a ) )
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static Complex Pow(in Complex a, in float b)
{
return Complex.Exp(b * Complex.Log(a));
}
/// <summary>
/// Raises a real number to the power of a complex number. Uses the formula
/// pow ( a, b ) := exp ( b log ( a ) )
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static Complex Pow(in float a, in Complex b)
{
return Complex.Exp(b * Complex.Log(a));
}
/// <summary>
/// Creates a random complex number.
/// </summary>
/// <param name="rng">random number generator</param>
/// <param name="radius">radius</param>
/// <returns>random complex number</returns>
public static Complex Random(
in System.Random rng,
in float radius = 1.0f)
{
float r = Utils.NextGaussian(rng);
float i = Utils.NextGaussian(rng);
float absq = r * r + i * i;
if (absq != 0.0f)
{
float scalar = radius / MathF.Sqrt(absq);
return new(r * scalar, i * scalar);
}
return Complex.Zero;
}
/// <summary>
/// Converts from polar to rectilinear coordinates.
/// </summary>
/// <param name="r">radius</param>
/// <param name="phi">angle in radians</param>
/// <returns>complex number</returns>
public static Complex Rect(in float r = 1.0f, in float phi = 0.0f)
{
return new(r * MathF.Cos(phi), r * MathF.Sin(phi));
}
/// <summary>
/// Finds the sine of a complex number.
/// </summary>
/// <param name="z">complex number</param>
/// <returns>sine</returns>
public static Complex Sin(in Complex z)
{
double zr = z.real;
double zi = z.imag;
return new(
(float)(Math.Sin(zr) * Math.Cosh(zi)),
(float)(Math.Cos(zr) * Math.Sinh(zi)));
}
/// <summary>
/// Finds the square root of a real number which could be either positive
/// or negative.
/// </summary>
/// <param name="a">value</param>
/// <returns>square root</returns>
public static Complex Sqrt(in float a)
{
return (a > 0.0f) ?
new(MathF.Sqrt(a), 0.0f) :
(a < -0.0f) ?
new(0.0f, MathF.Sqrt(-a)) :
Complex.Zero;
}
/// <summary>
/// Returns a string representation of a complex number.
/// </summary>
/// <param name="z">real number</param>
/// <param name="places">number of decimal places</param>
/// <returns>string</returns>
public static string ToString(in Complex z, in int places = 4)
{
return Complex.ToString(new StringBuilder(64), z, places).ToString();
}
/// <summary>
/// Appends a representation of a complex number to a string builder.
/// </summary>
/// <param name="sb">string builder</param>
/// <param name="z">complex number</param>
/// <param name="places">number of decimal places</param>
/// <returns>string builder</returns>
public static StringBuilder ToString(
in StringBuilder sb,
in Complex z,
in int places = 4)
{
sb.Append("{\"real\":");
Utils.ToFixed(sb, z.real, places);
sb.Append(",\"imag\":");
Utils.ToFixed(sb, z.imag, places);
sb.Append('}');
return sb;
}
/// <summary>
/// Returns a complex number with all components set to zero.
/// </summary>
/// <value>zero</value>
public static Complex Zero { get { return new(0.0f, 0.0f); } }
}