Common
A library of common classes.
Maths.cs
1using System;
2using System.Collections.Generic;
3
5{
9 public static class Maths
10 {
15 public static double[] CumSum(IList<double> values)
16 {
17 if (values.Count == 0)
18 return Array.Empty<double>();
19
20 double total = 0;
21 var result = new double[values.Count];
22
23 for (int i = 0; i < values.Count; i++)
24 {
25 total += values[i];
26 result[i] = total;
27 }
28
29 return result;
30 }
31
37 public static void CumSum(IList<double> values, List<double> result)
38 {
39 result.Clear();
40 double total = 0;
41
42 for (int i = 0; i < values.Count; i++)
43 {
44 total += values[i];
45 result.Add(total);
46 }
47 }
48
53 public static double[] CumSum(IList<float> values)
54 {
55 if (values.Count == 0)
56 return Array.Empty<double>();
57
58 double total = 0;
59 var result = new double[values.Count];
60
61 for (int i = 0; i < values.Count; i++)
62 {
63 total += values[i];
64 result[i] = total;
65 }
66
67 return result;
68 }
69
75 public static void CumSum(IList<float> values, List<double> result)
76 {
77 result.Clear();
78 double total = 0;
79
80 for (int i = 0; i < values.Count; i++)
81 {
82 total += values[i];
83 result.Add(total);
84 }
85 }
86
91 public static double[] Softmax(IList<double> values)
92 {
93 if (values.Count == 0)
94 return Array.Empty<double>();
95
96 double total = 0;
97 var softmax = new double[values.Count];
98
99 for (int i = 0; i < softmax.Length; i++)
100 {
101 var value = Math.Exp(values[i]);
102 total += value;
103 softmax[i] = value;
104 }
105
106 if (total > 0)
107 {
108 for (int i = 0; i < softmax.Length; i++)
109 {
110 softmax[i] /= total;
111 }
112 }
113
114 return softmax;
115 }
116
121 public static double[] Softmax(IList<float> values)
122 {
123 if (values.Count == 0)
124 return Array.Empty<double>();
125
126 double total = 0;
127 var softmax = new double[values.Count];
128
129 for (int i = 0; i < softmax.Length; i++)
130 {
131 var value = Math.Exp(values[i]);
132 total += value;
133 softmax[i] = value;
134 }
135
136 if (total > 0)
137 {
138 for (int i = 0; i < softmax.Length; i++)
139 {
140 softmax[i] /= total;
141 }
142 }
143
144 return softmax;
145 }
146
152 public static int MaxIndex(IList<float> values)
153 {
154 if (values.Count == 0)
155 return -1;
156
157 var index = 0;
158 var maxValue = values[0];
159
160 for (int i = 1; i < values.Count; i++)
161 {
162 var value = values[i];
163
164 if (value > maxValue)
165 {
166 index = i;
167 maxValue = value;
168 }
169 }
170
171 return index;
172 }
173
179 public static int MaxIndex(IList<double> values)
180 {
181 if (values.Count == 0)
182 return -1;
183
184 var index = 0;
185 var maxValue = values[0];
186
187 for (int i = 1; i < values.Count; i++)
188 {
189 var value = values[i];
190
191 if (value > maxValue)
192 {
193 index = i;
194 maxValue = value;
195 }
196 }
197
198 return index;
199 }
200
206 public static int MaxIndex(IList<int> values)
207 {
208 if (values.Count == 0)
209 return -1;
210
211 var index = 0;
212 var maxValue = values[0];
213
214 for (int i = 1; i < values.Count; i++)
215 {
216 var value = values[i];
217
218 if (value > maxValue)
219 {
220 index = i;
221 maxValue = value;
222 }
223 }
224
225 return index;
226 }
227
233 public static int MinIndex(IList<float> values)
234 {
235 if (values.Count == 0)
236 return -1;
237
238 var index = 0;
239 var minValue = values[0];
240
241 for (int i = 1; i < values.Count; i++)
242 {
243 var value = values[i];
244
245 if (value < minValue)
246 {
247 index = i;
248 minValue = value;
249 }
250 }
251
252 return index;
253 }
254
260 public static int MinIndex(IList<double> values)
261 {
262 if (values.Count == 0)
263 return -1;
264
265 var index = 0;
266 var minValue = values[0];
267
268 for (int i = 1; i < values.Count; i++)
269 {
270 var value = values[i];
271
272 if (value < minValue)
273 {
274 index = i;
275 minValue = value;
276 }
277 }
278
279 return index;
280 }
281
287 public static int MinIndex(IList<int> values)
288 {
289 if (values.Count == 0)
290 return -1;
291
292 var index = 0;
293 var minValue = values[0];
294
295 for (int i = 1; i < values.Count; i++)
296 {
297 var value = values[i];
298
299 if (value < minValue)
300 {
301 index = i;
302 minValue = value;
303 }
304 }
305
306 return index;
307 }
308
313 public static double Clamp01(double t)
314 {
315 if (t < 0)
316 return 0;
317 if (t > 1)
318 return 1;
319 return t;
320 }
321
329 public static double Lerp(double a, double b, double t)
330 {
331 return LerpUnclamped(a, b, Clamp01(t));
332 }
333
340 public static double LerpUnclamped(double a, double b, double t)
341 {
342 return a * (1 - t) + b * t;
343 }
344 }
345}
Contains extra math operations.
Definition: Maths.cs:10
static void CumSum(IList< float > values, List< double > result)
Adds the cumulative sum of the list to teh specified results list.
Definition: Maths.cs:75
static double[] Softmax(IList< double > values)
Returns the softmax of the collection of values.
Definition: Maths.cs:91
static double Clamp01(double t)
Clamps the value between 0 and 1.
Definition: Maths.cs:313
static double[] CumSum(IList< float > values)
Returns the cumulative sums of the list.
Definition: Maths.cs:53
static int MaxIndex(IList< double > values)
Returns the index in the collection where the first maximum value occurs. Returns -1 if the collectio...
Definition: Maths.cs:179
static int MaxIndex(IList< float > values)
Returns the index in the collection where the first maximum value occurs. Returns -1 if the collectio...
Definition: Maths.cs:152
static int MinIndex(IList< float > values)
Returns the index in the collection where the first minimum value occurs. Returns -1 if the collectio...
Definition: Maths.cs:233
static int MinIndex(IList< int > values)
Returns the index in the collection where the first minimum value occurs. Returns -1 if the collectio...
Definition: Maths.cs:287
static int MinIndex(IList< double > values)
Returns the index in the collection where the first minimum value occurs. Returns -1 if the collectio...
Definition: Maths.cs:260
static double LerpUnclamped(double a, double b, double t)
Linearly interpolates between two points based on a fractional distance parameter.
Definition: Maths.cs:340
static double[] CumSum(IList< double > values)
Returns the cumulative sums of the list.
Definition: Maths.cs:15
static void CumSum(IList< double > values, List< double > result)
Adds the cumulative sum of the list to teh specified results list.
Definition: Maths.cs:37
static int MaxIndex(IList< int > values)
Returns the index in the collection where the first maximum value occurs. Returns -1 if the collectio...
Definition: Maths.cs:206
static double[] Softmax(IList< float > values)
Returns the softmax of the collection of values.
Definition: Maths.cs:121
static double Lerp(double a, double b, double t)
Linearly interpolates between two points based on a fractional distance parameter....
Definition: Maths.cs:329