-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathComplexNumber.cs
More file actions
188 lines (165 loc) · 4.79 KB
/
Copy pathComplexNumber.cs
File metadata and controls
188 lines (165 loc) · 4.79 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
namespace CsCodeExample;
public class ComplexNumber // class
{
private double real; // Fields (usually hidden with properties)
public double Real // Properties ( Encapsulation, auto generated - Encapsulate field )
{
get { return real; }
set { real = value; }
}
public double Imag { get; set; } // Property with hidded field, PascalCase
public static string Format { get { return "a + b * i"; } }
protected const double PI = 3.14; // keyword: const (just for example, in this class Math.PI is used because it has more precision)
public ComplexNumber() { } // class constructor, default - no parameters
public ComplexNumber(double real, double imag = 0) // constructor with 2 parameters, second has default value
{
Real = real;
Imag = imag;
}
public ComplexNumber Add(ComplexNumber input)
{
var result = new ComplexNumber
{
Real = Real + input.Real,
Imag = Imag + input.Imag
};
return result;
}
public static ComplexNumber Add(ComplexNumber a, ComplexNumber b)
{
var result = new ComplexNumber
{
Real = a.Real + b.Real,
Imag = b.Imag + b.Imag
};
return result;
}
public ComplexNumber Subtract(ComplexNumber input)
{
var result = new ComplexNumber
{
Real = Real - input.Real,
Imag = Imag - input.Imag
};
return result;
}
public ComplexNumber Multiply(ComplexNumber input)
{
var result = new ComplexNumber
{
Real = Real * input.Real - Imag * input.Imag,
Imag = Real * input.Imag + Imag * input.Real
};
return result;
}
public ComplexNumber Divide(ComplexNumber input)
{
var result = new ComplexNumber
{
Real = (Real * input.Real + Imag * input.Imag) / (input.Real * input.Real + input.Imag * input.Imag),
Imag = (Real * input.Imag - Imag * input.Real) / (input.Real * input.Real + input.Imag * input.Imag)
};
return result;
}
public double Modul ()
{
return Math.Sqrt(Real * Real + Imag * Imag);
}
public ComplexNumber Pow(int n)
{
var result = new ComplexNumber(1, 0);
if (n != 0)
{
for (int i = 0; i < Math.Abs(n); i++)
{
result = result.Multiply(this);
}
if (n < 0)
{
result = new ComplexNumber(1, 0).Divide(result);
}
}
return result;
}
public List<ComplexNumber> Nroot(int n)
{
var result = new List<ComplexNumber>();
double r = Modul();
double f = Math.Atan(Imag / Real);
if (Real == 0.0)
{
f = Math.PI/2;
if (Imag < 0)
{
f += Math.PI;
}
}
if (Real < 0)
{
f += Math.PI;
}
double p1 = Math.Pow(r, n);
for(var i = 0; i < n; i++)
{
double p2 = (f + 2 * i * Math.PI) / n;
result.Add(new ComplexNumber(p1 * Math.Cos(p2), p1 * Math.Sin(p2)));
}
return result;
}
public override string ToString() // keyword: override
{
return string.Format("({0:0.00}, {1:0.00})", Real, Imag);
}
public string ToCSV() // CSV - Comma Separated Value
{
return string.Format("{0:0.00},{1:0.00}", Real, Imag);
}
public virtual string ToDiffString() // keyword: virtual(can be overridden in child class)
{
string output = "";
if (Imag == 0.0)
{
output = string.Format("{0}", Real);
}
else if (Imag > 0)
{
output = string.Format("{0} + {1}i", Real, Imag);
}
else if (Imag < 0)
{
output = string.Format("{0} - {1}i", Real, -Imag);
}
return output;
}
public static string GetDescription()
{
return "This class implements basic math function with complex numbers.";
}
}
public class MoreComplexNumber : ComplexNumber
{
public MoreComplexNumber() : base() { }
public MoreComplexNumber(double real, double imag = 0) : base(real, imag) { }
//public MoreComplexNumber(double real, double imag = 0)
//{
// Real = real;
// Imag = imag;
//}
public override string ToDiffString()
{
string output = "";
if (Imag == 0.0)
{
output = String.Format("{0}", Real);
}
else if (Imag > 0)
{
output = String.Format("{0} + {1} * i", Real, Imag);
}
else if (Imag < 0)
{
output = String.Format("{0} - {1} * i", Real, -Imag);
}
return output;
}
}