forked from Hydrologist/msnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithms.cs
227 lines (206 loc) · 6.66 KB
/
Algorithms.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Network
{
// Some generic algorithsm, a la the C++ STL
public sealed class Algorithms
{
public delegate T VoidFunctor<T>();
public delegate T Functor2<T>(double a);
public delegate T Functor<T>(double a,double b);
public static void Swap<T>(ref T a, ref T b)
{
T tmp = a;
a = b;
b = tmp;
}
public static void Fill<T>(IIndexable<T> obj, T value)
{
for (int i = 0; i < obj.Length; ++i)
obj[i] = value;
}
public static void Fill<T>(IIndexable<T> obj, VoidFunctor<T> f)
{
for (int i = 0; i < obj.Length; ++i)
obj[i] = f();
}
public static void Fill<T>(IIndexable<T> obj, Functor2<T> f, double a)
{
for (int i = 0; i < obj.Length; ++i)
obj[i] = f(a);
}
public static void Fill<T>(IIndexable<T> obj, Functor<T> f, double a, double b)
{
for (int i = 0; i < obj.Length; ++i)
obj[i] = f(a,b);
}
public static void Fill<T>(T[] array, T value)
{
for (int i = 0; i < array.Length; ++i)
array[i] = value;
}
public static void Fill<T>(T[] array, VoidFunctor<T> f)
{
for (int i = 0; i < array.Length; ++i)
array[i] = f();
}
public static void Iota(IIndexable<int> obj, int startValue)
{
for (int i = 0; i < obj.Length; ++i)
obj[i] = startValue + i;
}
public static void Iota(IIndexable<string> obj, int startValue)
{
for (int i = 0; i < obj.Length; ++i)
obj[i] = (startValue + i).ToString();
}
public static int Accumulate(IEnumerable<int> array, int startValue)
{
foreach (int i in array)
startValue += i;
return startValue;
}
public static double Accumulate(IEnumerable<double> array, double startValue)
{
foreach (double i in array)
startValue += i;
return startValue;
}
public static T MaxValue<T>(IEnumerable<T> obj) where T: IComparable
{
T max = default(T);
foreach (T i in obj)
{
max = i;
break; // only want to traverse through obj once to get first index
}
foreach (T i in obj)
{
//if (max .CompareTo(default(T)) == 0 || i.CompareTo(max) > 0)
if (i.CompareTo(max) > 0)
max = i;
}
return max;
}
public static T MinValue<T>(IEnumerable<T> obj) where T : IComparable
{
T max = default(T);
foreach (T i in obj)
{
max = i;
break; // only want to traverse through obj once to get first index
}
foreach (T i in obj)
{
//if (max.CompareTo(default(T)) == 0 || i.CompareTo(max) < 0)
if (i.CompareTo(max) < 0)
max = i;
}
return max;
}
public static double Mean(params double[] values)
{
double sum = 0;
foreach (double val in values)
sum += val;
return sum / values.Length;
}
public static double Mean(IEnumerable<double> values)
{
double sum = 0;
int count = 0;
foreach (double val in values)
{
sum += val;
++count;
}
return sum / count;
}
public static double Variance(params double[] values)
{
double mean = Mean(values);
double M2 = 0.0;
int n = 0;
foreach (double x in values)
{
++n;
//double delta = x - mean;
//mean += delta / n;
//M2 += delta * (x - mean);
double delta = (x - mean) * (x - mean);
M2 += delta;
}
return M2 / n;
}
public static double Variance(IEnumerable<double> values)
{
double mean = Mean(values);
double M2 = 0.0;
int n = 0;
foreach (double x in values)
{
++n;
double delta = (x - mean) * (x - mean);
M2 += delta;
}
return M2 / n;
}
// Returns the standard deviation of a sample
public static double Stdev(IEnumerable<double> values)
{
double mean = Mean(values);
double M2 = 0.0;
int n = 0;
foreach (double x in values)
{
++n;
double delta = (x - mean) * (x - mean);
M2 += delta;
}
double sample = M2 / (n - 1);
return Math.Sqrt(sample);
}
// Returns the standard deviation of a population
public static double Stdevp(IEnumerable<double> values)
{
return Math.Sqrt(Variance(values));
}
public static T[] Subarray<T>(T[] a, int start, int end)
{
T[] subarray = new T[end - start];
Array.Copy(a, start, subarray, 0, subarray.Length);
return subarray;
}
public static bool IsSupersetOf<T>(T[] superset, T[] subset)
{
if (superset.Length < subset.Length)
return false;
for (int i = 0; i < subset.Length; ++i)
{
if (Array.IndexOf(superset, subset[i]) < 0)
return false;
}
return true;
}
public static bool IsSupersetOf<T>(T[] subset, List<T[]> sets)
{
foreach (T[] set in sets)
if (IsSupersetOf(subset, set))
return true;
return false;
}
public static int GetMinNumMultiples(int multiple, double whole)
{
if (whole % multiple == 0)
{
return (int)(whole / multiple);
}
else
{
return (int)(whole / multiple) + 1;
}
}
}
}