-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathComplex.cs
129 lines (104 loc) · 2.96 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
using System;
namespace PasiveRadar
{
public struct Complex
{
public float Rea;
public float Imag;
public Complex(float real, float imaginary)
{
this.Rea = real;
this.Imag = imaginary;
}
public float Re
{
get
{
return (Rea);
}
set
{
Rea = value;
}
}
public float Im
{
get
{
return (Imag);
}
set
{
Imag = value;
}
}
public override string ToString()
{
return (String.Format("({0}, {1}i)", Rea, Imag));
}
public static bool operator ==(Complex c1, Complex c2)
{
if ((c1.Rea == c2.Rea) &&
(c1.Imag == c2.Imag))
return (true);
else
return (false);
}
public static bool operator !=(Complex c1, Complex c2)
{
return (!(c1 == c2));
}
public override bool Equals(object o2)
{
Complex c2 = (Complex)o2;
return (this == c2);
}
public override int GetHashCode()
{
return (Rea.GetHashCode() ^ Imag.GetHashCode());
}
public static Complex operator +(Complex c1, Complex c2)
{
return (new Complex(c1.Rea + c2.Rea, c1.Imag + c2.Imag));
}
public static Complex operator -(Complex c1, Complex c2)
{
return (new Complex(c1.Rea - c2.Rea, c1.Imag - c2.Imag));
}
// product of complex and float
public static Complex operator *(Complex c1, float f)
{
return (new Complex(c1.Rea * f, c1.Imag * f));
}
// product of two complex numbers
public static Complex operator *(Complex c1, Complex c2)
{
return (new Complex(c1.Rea * c2.Rea - c1.Imag * c2.Imag,
c1.Rea * c2.Imag + c2.Rea * c1.Imag));
}
// quotient of two complex numbers
public static Complex operator /(Complex c1, Complex c2)
{
if ((c2.Rea == 0.0f) &&
(c2.Imag == 0.0f))
throw new DivideByZeroException("Can't divide by zero Complex number");
float newReal =
(c1.Rea * c2.Rea + c1.Imag * c2.Imag) /
(c2.Rea * c2.Rea + c2.Imag * c2.Imag);
float newImaginary =
(c2.Rea * c1.Imag - c1.Rea * c2.Imag) /
(c2.Rea * c2.Rea + c2.Imag * c2.Imag);
return (new Complex(newReal, newImaginary));
}
// non-operator versions for other languages
// public static Complex Zero()
// {
// Real(0);
// Imaginary(0);
// }
public double Module()
{
return (Math.Sqrt(Rea * Rea + Imag * Imag));
}
}
}