-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathComplex.cs
More file actions
1839 lines (1602 loc) · 53.7 KB
/
Complex.cs
File metadata and controls
1839 lines (1602 loc) · 53.7 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
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------
// <copyright file="Complex.cs" company="Math.NET Project">
// Copyright (c) 2002-2009, Christoph Rüegg, Joannes Vermorel.
// All Right Reserved.
// </copyright>
// <author>
// Christoph Rüegg, http://christoph.ruegg.name
// Joannes Vermorel, http://www.vermorel.com
// </author>
// <product>
// Math.NET Iridium, part of the Math.NET Project.
// http://mathnet.opensourcedotnet.info
// </product>
// <license type="opensource" name="LGPL" version="2 or later">
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
// </license>
//-----------------------------------------------------------------------
using System;
using System.Collections;
using System.Globalization;
using System.Text;
namespace MathNet.Numerics
{
using Distributions;
/// <summary>
/// Complex numbers class.
/// </summary>
/// <remarks>
/// <p>
/// The class <c>Complex</c> provides all elementary operations
/// on complex numbers. All the operators <c>+</c>, <c>-</c>,
/// <c>*</c>, <c>/</c>, <c>==</c>, <c>!=</c> are defined in the
/// canonical way. Additional complex trigonometric functions such
/// as <see cref="Complex.Cosine"/>, ...
/// are also provided. Note that the <c>Complex</c> structures
/// has two special constant values <see cref="Complex.NaN"/> and
/// <see cref="Complex.Infinity"/>.
/// </p>
/// <p>
/// In order to avoid possible ambiguities resulting from a
/// <c>Complex(double, double)</c> constructor, the static methods
/// <see cref="Complex.FromRealImaginary"/> and <see cref="Complex.FromModulusArgument"/>
/// are provided instead.
/// </p>
/// <code>
/// Complex x = Complex.FromRealImaginary(1d, 2d);
/// Complex y = Complex.FromModulusArgument(1d, Math.Pi);
/// Complex z = (x + y) / (x - y);
/// </code>
/// <p>
/// Since there is no canonical order among the complex numbers,
/// <c>Complex</c> does not implement <c>IComparable</c> but several
/// lexicographic <c>IComparer</c> implementations are provided, see
/// <see cref="Complex.RealImaginaryComparer"/>,
/// <see cref="Complex.ModulusArgumentComparer"/> and
/// <see cref="Complex.ArgumentModulusComparer"/>.
/// </p>
/// <p>
/// For mathematical details about complex numbers, please
/// have a look at the <a href="http://en.wikipedia.org/wiki/Complex_number">
/// Wikipedia</a>
/// </p>
/// </remarks>
[Serializable]
public struct Complex :
IEquatable<Complex>,
IAlmostEquatable<Complex>,
IComparable<Complex>
{
#region Complex comparers
private sealed class RealImaginaryLexComparer : IComparer
{
public int Compare(object x, object y)
{
throw new NotImplementedException();
}
}
private sealed class ModulusArgumentLexComparer : IComparer
{
public int Compare(object x, object y)
{
throw new NotImplementedException();
}
}
private sealed class ArgumentModulusLexComparer : IComparer
{
public int Compare(object x, object y)
{
throw new NotImplementedException();
}
}
private static IComparer _realImaginaryComparer;
private static IComparer _modulusArgumentComparer;
private static IComparer _argumentModulusComparer;
/// <summary>
/// Gets the lexicographical comparer based on <c>(real, imaginary)</c>.
/// </summary>
public static IComparer RealImaginaryComparer
{
get
{
if(_realImaginaryComparer == null)
{
_realImaginaryComparer = new RealImaginaryLexComparer();
}
return _realImaginaryComparer;
}
}
/// <summary>
/// Gets the lexicographical comparer based on <c>(modulus, argument)</c>.
/// </summary>
public static IComparer ModulusArgumentComparer
{
get
{
if(_modulusArgumentComparer == null)
{
_modulusArgumentComparer = new ModulusArgumentLexComparer();
}
return _modulusArgumentComparer;
}
}
/// <summary>
/// Gets the lexicographical comparer based on <c>(argument, modulus)</c>.
/// </summary>
public static IComparer ArgumentModulusComparer
{
get
{
if(_argumentModulusComparer == null)
{
_argumentModulusComparer = new ArgumentModulusLexComparer();
}
return _argumentModulusComparer;
}
}
#endregion
double _real;
double _imag;
#region Constructors and Constants
/// <summary>
/// Initializes a new instance of the Complex
/// from its real and imaginary parts.
/// </summary>
public
Complex(double real, double imag)
{
_real = real;
_imag = imag;
}
/// <summary>
/// Constructs a <c>Complex</c> from its real
/// and imaginary parts.
/// </summary>
public static
Complex
FromRealImaginary(double real, double imag)
{
return new Complex(real, imag);
}
/// <summary>
/// Constructs a <c>Complex</c> from its modulus and
/// argument.
/// </summary>
/// <param name="modulus">Must be non-negative.</param>
/// <param name="argument">Real number.</param>
public static
Complex
FromModulusArgument(double modulus, double argument)
{
if(modulus < 0d)
{
throw new ArgumentOutOfRangeException(
"modulus",
modulus,
Properties.LocalStrings.ArgumentNotNegative);
}
return new Complex(
modulus * Math.Cos(argument),
modulus * Math.Sin(argument));
}
/// <summary>
/// Constructs a complex number with random real and imaginary value.
/// </summary>
/// <param name="realRandomDistribution">Continuous random distribution or source for the real part.</param>
/// <param name="imagRandomDistribution">Continuous random distribution or source for the imaginary part.</param>
public static
Complex
Random(
IContinuousGenerator realRandomDistribution,
IContinuousGenerator imagRandomDistribution)
{
return new Complex(
realRandomDistribution.NextDouble(),
imagRandomDistribution.NextDouble());
}
/// <summary>
/// Constructs a complex number with random real and imaginary value.
/// </summary>
/// <param name="randomDistribution">Continuous random distribution or source for the real and imaginary parts.</param>
public static
Complex
Random(IContinuousGenerator randomDistribution)
{
return new Complex(
randomDistribution.NextDouble(),
randomDistribution.NextDouble());
}
/// <summary>
/// Constructs a complex number with random modulus and argument.
/// </summary>
/// <param name="modulusRandomDistribution">Continuous random distribution or source for the modulus.</param>
/// <param name="argumentRandomDistribution">Continuous random distribution or source for the argument.</param>
public static
Complex
RandomPolar(
IContinuousGenerator modulusRandomDistribution,
IContinuousGenerator argumentRandomDistribution)
{
return FromModulusArgument(
modulusRandomDistribution.NextDouble(),
argumentRandomDistribution.NextDouble());
}
/// <summary>
/// Constructs a complex number on the unit circle with random argument.
/// </summary>
/// <param name="argumentRandomDistribution">Continuous random distribution or source for the argument.</param>
public static
Complex
RandomUnitCircle(IContinuousGenerator argumentRandomDistribution)
{
return FromModulusArgument(
1d,
argumentRandomDistribution.NextDouble());
}
/// <summary>
/// Represents the zero value. This field is constant.
/// </summary>
public static Complex Zero
{
get { return new Complex(0d, 0d); }
}
/// <summary>
/// Indicates whether the <c>Complex</c> is zero.
/// </summary>
public bool IsZero
{
get { return Number.AlmostZero(_real) && Number.AlmostZero(_imag); }
}
/// <summary>
/// Represents the <c>1</c> value. This field is constant.
/// </summary>
public static Complex One
{
get { return new Complex(1d, 0d); }
}
/// <summary>
/// Indicates whether the <c>Complex</c> is one.
/// </summary>
public bool IsOne
{
get { return Number.AlmostEqual(_real, 1) && Number.AlmostZero(_imag); }
}
/// <summary>
/// Represents the imaginary unit number. This field is constant.
/// </summary>
public static Complex I
{
get { return new Complex(0d, 1d); }
}
/// <summary>
/// Indicates whether the <c>Complex</c> is the imaginary unit.
/// </summary>
public bool IsI
{
get { return Number.AlmostZero(_real) && Number.AlmostEqual(_imag, 1); }
}
/// <summary>
/// Represents a value that is not a number. This field is constant.
/// </summary>
public static Complex NaN
{
get { return new Complex(double.NaN, double.NaN); }
}
/// <summary>
/// Indicates whether the provided <c>Complex</c> evaluates to a
/// value that is not a number.
/// </summary>
public bool IsNaN
{
get { return double.IsNaN(_real) || double.IsNaN(_imag); }
}
/// <summary>
/// Represents the infinity value. This field is constant.
/// </summary>
/// <remarks>
/// The semantic associated to this value is a <c>Complex</c> of
/// infinite real and imaginary part. If you need more formal complex
/// number handling (according to the Riemann Sphere and the extended
/// complex plane C*, or using directed infinity) please check out the
/// alternative MathNet.PreciseNumerics and MathNet.Symbolics packages
/// instead.
/// </remarks>
public static Complex Infinity
{
get { return new Complex(double.PositiveInfinity, double.PositiveInfinity); }
}
/// <summary>
/// Indicates the provided <c>Complex</c> evaluates to an
/// infinite value.
/// </summary>
/// <remarks>
/// True if it either evaluates to a complex infinity
/// or to a directed infinity.
/// </remarks>
public bool IsInfinity
{
get { return double.IsInfinity(_real) || double.IsInfinity(_imag); }
}
/// <summary>
/// Indicates the provided <c>Complex</c> is real.
/// </summary>
public bool IsReal
{
get { return Number.AlmostZero(_imag); }
}
/// <summary>
/// Indicates the provided <c>Complex</c> is real and not negative, that is >= 0.
/// </summary>
public bool IsRealNonNegative
{
get { return Number.AlmostZero(_imag) && _real >= 0; }
}
/// <summary>
/// Indicates the provided <c>Complex</c> is imaginary.
/// </summary>
public bool IsImaginary
{
get { return Number.AlmostZero(_real); }
}
#endregion
#region Cartesian and Polar Components
/// <summary>
/// Gets or sets the real part of this <c>Complex</c>.
/// </summary>
/// <seealso cref="Imag"/>
public double Real
{
get { return _real; }
set { _real = value; }
}
/// <summary>
/// Gets or sets the imaginary part of this <c>Complex</c>.
/// </summary>
/// <seealso cref="Real"/>
public double Imag
{
get { return _imag; }
set { _imag = value; }
}
/// <summary>
/// Gets or sets the modulus of this <c>Complex</c>.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if an attempt is made to set a negative modulus.
/// </exception>
/// <remarks>
/// If this <c>Complex</c> is zero when the modulus is set,
/// the Complex is assumed to be positive real with an argument of zero.
/// </remarks>
/// <seealso cref="Argument"/>
public double Modulus
{
get
{
return Math.Sqrt((_real * _real) + (_imag * _imag));
}
set
{
if(value < 0d)
{
throw new ArgumentOutOfRangeException(
"value",
value,
Properties.LocalStrings.ArgumentNotNegative);
}
if(double.IsInfinity(value))
{
_real = value;
_imag = value;
}
else
{
if(IsZero)
{
_real = value;
_imag = 0;
}
else
{
double factor = value / Math.Sqrt((_real * _real) + (_imag * _imag));
_real *= factor;
_imag *= factor;
}
}
}
}
/// <summary>
/// Gets or sets the squared modulus of this <c>Complex</c>.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if an attempt is made to set a negative modulus.
/// </exception>
/// <remarks>
/// If this <c>Complex</c> is zero when the modulus is set,
/// the Complex is assumed to be positive real with an argument of zero.
/// </remarks>
/// <seealso cref="Argument"/>
public double ModulusSquared
{
get
{
return (_real * _real) + (_imag * _imag);
}
set
{
if(value < 0d)
{
throw new ArgumentOutOfRangeException(
"value",
value,
Properties.LocalStrings.ArgumentNotNegative);
}
if(double.IsInfinity(value))
{
_real = value;
_imag = value;
}
else
{
if(IsZero)
{
_real = Math.Sqrt(value);
_imag = 0;
}
else
{
double factor = value / ((_real * _real) + (_imag * _imag));
_real *= factor;
_imag *= factor;
}
}
}
}
/// <summary>
/// Gets or sets the argument of this <c>Complex</c>.
/// </summary>
/// <remarks>
/// Argument always returns a value bigger than negative Pi and
/// smaller or equal to Pi. If this <c>Complex</c> is zero, the Complex
/// is assumed to be positive real with an argument of zero.
/// </remarks>
public double Argument
{
get
{
if(IsReal && _real < 0)
{
return Math.PI;
}
if(IsRealNonNegative)
{
return 0;
}
return Math.Atan2(_imag, _real);
}
set
{
double modulus = Modulus;
_real = Math.Cos(value) * modulus;
_imag = Math.Sin(value) * modulus;
}
}
/// <summary>
/// Gets the unity of this complex (same argument, but on the unit circle; exp(I*arg))
/// </summary>
public Complex Sign
{
get
{
if(double.IsPositiveInfinity(_real) && double.IsPositiveInfinity(_imag))
{
return new Complex(Constants.Sqrt1_2, Constants.Sqrt1_2);
}
if(double.IsPositiveInfinity(_real) && double.IsNegativeInfinity(_imag))
{
return new Complex(Constants.Sqrt1_2, -Constants.Sqrt1_2);
}
if(double.IsNegativeInfinity(_real) && double.IsPositiveInfinity(_imag))
{
return new Complex(-Constants.Sqrt1_2, -Constants.Sqrt1_2);
}
if(double.IsNegativeInfinity(_real) && double.IsNegativeInfinity(_imag))
{
return new Complex(-Constants.Sqrt1_2, Constants.Sqrt1_2);
}
// don't replace this with "Modulus"!
double mod = Fn.Hypot(_real, _imag);
if(mod == 0)
{
return One;
}
return new Complex(_real / mod, _imag / mod);
}
}
#endregion
/// <summary>
/// Gets or sets the conjugate of this <c>Complex</c>.
/// </summary>
/// <remarks>
/// The semantic of <i>setting the conjugate</i> is such that
/// <code>
/// // a, b of type Complex
/// a.Conjugate = b;
/// </code>
/// is equivalent to
/// <code>
/// // a, b of type Complex
/// a = b.Conjugate
/// </code>
/// </remarks>
public Complex Conjugate
{
get { return new Complex(_real, -_imag); }
set { this = value.Conjugate; }
}
#region Equality & Hashing
/// <summary>
/// Indicates whether <c>obj</c> is equal to this instance.
/// </summary>
public override
bool
Equals(object obj)
{
return (obj is Complex) && Equals((Complex)obj);
}
/// <summary>
/// Indicates whether <c>z</c> is equal to this complex number.
/// </summary>
public
bool
Equals(Complex other)
{
return !IsNaN
&& !other.IsNaN
&& (_real == other._real)
&& (_imag == other._imag);
}
/// <summary>
/// Indicates whether <paramref name="other"/> is almost equal to this complex number, up to the default maximum relative error.
/// </summary>
public
bool
AlmostEquals(Complex other)
{
return !IsNaN
&& !other.IsNaN
&& Number.AlmostEqual(_real, other._real)
&& Number.AlmostEqual(_imag, other._imag);
}
/// <summary>
/// Indicates whether <paramref name="other"/> is almost equal to this complex number, up to the provided maximum relative error.
/// </summary>
public
bool
AlmostEquals(
Complex other,
double maximumRelativeError)
{
return !IsNaN
&& !other.IsNaN
&& Number.AlmostEqual(_real, other._real, maximumRelativeError)
&& Number.AlmostEqual(_imag, other._imag, maximumRelativeError);
}
/// <summary>
/// Returns true if two complex numbers are almost equal, up to the provided maximum relative error.
/// </summary>
public static
bool
AlmostEqual(
Complex x,
Complex y,
double maximumRelativeError)
{
return x.AlmostEquals(y, maximumRelativeError);
}
/// <summary>
/// Returns true if two complex numbers are almost equal, up to the default maximum relative error.
/// </summary>
public static
bool
AlmostEqual(
Complex x,
Complex y)
{
return x.AlmostEquals(y);
}
/// <summary>
/// Gets the hash code of this <c>Complex</c>.
/// </summary>
public override
int
GetHashCode()
{
return _real.GetHashCode() ^ (-_imag.GetHashCode());
}
/// <summary>
/// Compare this complex number with another complex number.
/// </summary>
/// <remarks>
/// The complex number's real part takes precedence over the imaginary part.
/// </remarks>
/// <param name="other">The complex number to compare with.</param>
public
int
CompareTo(Complex other)
{
int res = Real.CompareTo(other.Real);
if(res != 0)
{
return res;
}
return Imag.CompareTo(other.Imag);
}
#endregion
#region Operators
/// <summary>
/// Equality test.
/// </summary>
public static bool operator ==(Complex complex1, Complex complex2)
{
return complex1.Equals(complex2);
}
/// <summary>
/// Inequality test.
/// </summary>
public static bool operator !=(Complex complex1, Complex complex2)
{
return !complex1.Equals(complex2);
}
/// <summary>
/// Less-than test.
/// </summary>
public static bool operator <(Complex complex1, Complex complex2)
{
return complex1.CompareTo(complex2) < 0;
}
/// <summary>
/// Greater-than test.
/// </summary>
public static bool operator >(Complex complex1, Complex complex2)
{
return complex1.CompareTo(complex2) > 0;
}
/// <summary>
/// Less-than-or-equal test.
/// </summary>
public static bool operator <=(Complex complex1, Complex complex2)
{
return complex1.CompareTo(complex2) <= 0;
}
/// <summary>
/// Greater-than-or-equal test.
/// </summary>
public static bool operator >=(Complex complex1, Complex complex2)
{
return complex1.CompareTo(complex2) >= 0;
}
/// <summary>
/// Unary addition.
/// </summary>
public static Complex operator +(Complex summand)
{
return summand;
}
/// <summary>
/// Unary minus.
/// </summary>
public static Complex operator -(Complex subtrahend)
{
return new Complex(-subtrahend._real, -subtrahend._imag);
}
/// <summary>
/// Complex addition.
/// </summary>
public static Complex operator +(Complex summand1, Complex summand2)
{
return new Complex(summand1._real + summand2._real, summand1._imag + summand2._imag);
}
/// <summary>
/// Complex subtraction.
/// </summary>
public static Complex operator -(Complex minuend, Complex subtrahend)
{
return new Complex(minuend._real - subtrahend._real, minuend._imag - subtrahend._imag);
}
/// <summary>
/// Complex addition.
/// </summary>
public static Complex operator +(Complex summand1, double summand2)
{
return new Complex(summand1._real + summand2, summand1._imag);
}
/// <summary>
/// Complex subtraction.
/// </summary>
public static Complex operator -(Complex minuend, double subtrahend)
{
return new Complex(minuend._real - subtrahend, minuend._imag);
}
/// <summary>
/// Complex addition.
/// </summary>
public static Complex operator +(double summand1, Complex summand2)
{
return new Complex(summand2._real + summand1, summand2._imag);
}
/// <summary>
/// Complex subtraction.
/// </summary>
public static Complex operator -(double minuend, Complex subtrahend)
{
return new Complex(minuend - subtrahend._real, -subtrahend._imag);
}
/// <summary>
/// Complex multiplication.
/// </summary>
public static Complex operator *(Complex multiplicand, Complex multiplier)
{
return new Complex((multiplicand._real * multiplier._real) - (multiplicand._imag * multiplier._imag), (multiplicand._real * multiplier._imag) + (multiplicand._imag * multiplier._real));
}
/// <summary>
/// Complex multiplication.
/// </summary>
public static Complex operator *(double multiplicand, Complex multiplier)
{
return new Complex(multiplier._real * multiplicand, multiplier._imag * multiplicand);
}
/// <summary>
/// Complex multiplication.
/// </summary>
public static Complex operator *(Complex multiplicand, double multiplier)
{
return new Complex(multiplicand._real * multiplier, multiplicand._imag * multiplier);
}
/// <summary>
/// Complex division.
/// </summary>
public static Complex operator /(Complex dividend, Complex divisor)
{
if(divisor.IsZero)
{
return Infinity;
}
if(Math.Abs(divisor._real) >= Math.Abs(divisor._imag))
{
double r = divisor._imag / divisor._real;
double den = divisor._real + (r * divisor._imag);
return new Complex(
(dividend._real + (dividend._imag * r)) / den,
(dividend._imag - (dividend._real * r)) / den);
}
else
{
double r = divisor._real / divisor._imag;
double den = divisor._imag + (r * divisor._real);
return new Complex(
((dividend._real * r) + dividend._imag) / den,
((dividend._imag * r) - dividend._real) / den);
}
}
/// <summary>
/// Complex division.
/// </summary>
public static Complex operator /(double dividend, Complex divisor)
{
if(divisor.IsZero)
{
return Infinity;
}
if(Math.Abs(divisor._real) >= Math.Abs(divisor._imag))
{
double r = divisor._imag / divisor._real;
double den = divisor._real + (r * divisor._imag);
return new Complex(
dividend / den,
-dividend * r / den);
}
else
{
double r = divisor._real / divisor._imag;
double den = divisor._imag + (r * divisor._real);
return new Complex(
dividend * r / den,
-dividend / den);
}
}
/// <summary>
/// Complex division.
/// </summary>
public static Complex operator /(Complex dividend, double divisor)
{
if(Number.AlmostZero(divisor))
{
return Infinity;
}
return new Complex(dividend._real / divisor, dividend._imag / divisor);
}
/// <summary>
/// Implicit conversion of a real double to a real <c>Complex</c>.
/// </summary>
public static implicit operator Complex(double number)
{
return new Complex(number, 0d);
}
#endregion
#region Trigonometric Functions
/// <summary>
/// Trigonometric Sine (sin, Sinus) of this <c>Complex</c>.
/// </summary>
public
Complex
Sine()
{
if(IsReal)
{
return new Complex(Trig.Sine(_real), 0d);
}
return new Complex(
Trig.Sine(_real) * Trig.HyperbolicCosine(_imag),
Trig.Cosine(_real) * Trig.HyperbolicSine(_imag));
}
/// <summary>
/// Trigonometric Cosine (cos, Cosinus) of this <c>Complex</c>.
/// </summary>
public
Complex
Cosine()
{
if(IsReal)
{
return new Complex(Trig.Cosine(_real), 0d);
}
return new Complex(
Trig.Cosine(_real) * Trig.HyperbolicCosine(_imag),
-Trig.Sine(_real) * Trig.HyperbolicSine(_imag));
}
/// <summary>
/// Trigonometric Tangent (tan, Tangens) of this <c>Complex</c>.
/// </summary>
public
Complex
Tangent()
{
if(IsReal)
{
return new Complex(Trig.Tangent(_real), 0d);
}
double cosr = Trig.Cosine(_real);
double sinhi = Trig.HyperbolicSine(_imag);
double denom = (cosr * cosr) + (sinhi * sinhi);
return new Complex(
Trig.Sine(_real) * cosr / denom,
sinhi * Trig.HyperbolicCosine(_imag) / denom);
}
/// <summary>
/// Trigonometric Cotangent (cot, Cotangens) of this <c>Complex</c>.
/// </summary>
public
Complex
Cotangent()
{
if(IsReal)