-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseMethod.cs
224 lines (199 loc) · 6.17 KB
/
BaseMethod.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
namespace diplom
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class BaseMethod
{
protected const float F = 1.618F;
protected double A { get; set; }
protected double B { get; set; }
protected double E { get; set; }
protected MyDel Y { get; set; }
protected List<Segment> minStepsArray = new List<Segment>();
protected List<Segment> maxStepsArray = new List<Segment>();
public float Min { get; set; }
public float Max { get; set; }
public InfoBlock infoBlock = new InfoBlock();
public List<AdditionInfo> addInfoList = new List<AdditionInfo>(20);
private float[,] DeleteEmptyElements(float[,] temp)
{
var emptynum = 0;
var n = temp.GetLength(0) - 9;
while (n != temp.GetLength(0))
{
if (temp[n, 0] == 0 && temp[n, 1] == 0)
{
emptynum++;
}
n++;
}
var resultarray = new float[temp.GetLength(0) - emptynum, 3];
Array.Copy(temp, 0, resultarray, 0, temp.Length - emptynum * 3 - 1);
return resultarray;
}
protected virtual bool Compare(SearchType mType, float y1, float y2)
{
return (mType.HasFlag(SearchType.Minimum)) ? y1 < y2 : y1 > y2;
}
public void SetValues(double start, double end, double cur, MyDel y)
{
this.A = start;
this.B = end;
this.E = cur;
this.Y = y;
this.FillInfoBlock();
}
public void AddStep(float x1, float x2, SearchType mt)
{
switch (mt)
{
case SearchType.Minimum: this.minStepsArray.Add(new Segment(x1, x2));
break;
case SearchType.Maximum: this.maxStepsArray.Add(new Segment(x1, x2));
break;
default: throw new FormatException("Unexpected enumerator MethodType value");
}
}
public void Calculate()
{
this.CalculateByType(SearchType.Maximum);
this.CalculateByType(SearchType.Minimum);
}
public float GetYbyX(float x)
{
return this.Y(x);
}
public float[,] GetPoints()
{
float step = 0.001F;
float a = (float)this.A;
float b = (float)this.B;
int N = (int)Math.Round(Math.Abs(b - a) / step) + 1;
Form1.searcheablePointList = new List<Point>(N);
var temp = new float[N, 3];
for (int i = 0; b > a; i++, a = a + step)
{
if (N == i)
{
break;
}
temp[i, 0] = a;
temp[i, 1] = this.Y(a);
temp[i, 2] = 0;
Form1.searcheablePointList.Add(new Point(a, temp[i, 1]));
}
return DeleteEmptyElements(temp);
}
public string[] GetResults(SearchType mt)
{
float t = this.Y(this.Min);
return mt.HasFlag(SearchType.Minimum) ?
new string[2]
{
Math.Round(this.Min, 4).ToString(),
Math.Round(this.Y(this.Min), 4).ToString(),
}
:
new string[2]
{
Math.Round(this.Max,4).ToString(),
Math.Round(this.Y(this.Max),4).ToString()
};
}
public virtual void CalculateByType(SearchType mType)
{ }
public virtual void FillInfoBlock()
{ }
public List<Segment> GetSteps(SearchType mt)
{
return mt.HasFlag(SearchType.Maximum) ? this.maxStepsArray : this.minStepsArray;
}
public List<float[,]> TreatLastElement(List<float[,]> steplist)
{
float accuracy = 0.001F;
var el = steplist.Last();
var elres = new float[2, 3];
elres[0, 0] = el[0, 0] - accuracy;
elres[0, 1] = el[0, 1];
elres[1, 0] = el[0, 0] + accuracy;
elres[1, 1] = el[0, 1];
steplist.Remove(el);
steplist.Add(elres);
return steplist;
}
public static BaseMethod GetMethodType(int num)
{
switch (num)
{
case 1: return new Gold();
case 2: return new Fibonachi();
case 3: return new Dichotomy();
default: throw new Exception();
}
}
}
public enum SearchType
{
Minimum = 1,
Maximum = 2
}
public struct Segment
{
public double x1;
public double x2;
public Segment(double X1, double X2)
{
this.x1 = X1;
this.x2 = X2;
}
}
public struct Point
{
public float x;
public float y;
public float z;
public Point(float X, float Y)
{
this.x = X;
this.y = Y;
this.z = 0;
}
}
public struct InfoBlock
{
public string message;
public string left_border;
public string right_border;
public string delta;
public string Fn;
public string N;
public string k;
public InfoBlock(string message, string left_border, string right_border, string delta, string Fn, string N, string k)
{
this.message = message;
this.left_border = left_border;
this.right_border = right_border;
//dichotomy method
this.delta = delta;
//fibonachi method
this.Fn = Fn;
this.N = N;
this.k = k;
}
}
public struct AdditionInfo
{
public static int Fn;
public static int N;
public float k;
public float delta;
public AdditionInfo(float k, float delta)
{
this.k = k;
this.delta = delta;
}
}
}