/
SblNumerics.h
executable file
·357 lines (293 loc) · 7.67 KB
/
SblNumerics.h
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**************************************************************************
SblNumerics - utility math functions
Credits:
- From SIBIL, the Silwood Biocomputing Library.
- By Paul-Michael Agapow, 2003, Dept. Biology, University College London,
London WC1E 6BT, UNITED KINGDOM.
- <mail://p.agapow@ucl.ac.uk> <http://www.bio.ic.ac.uk/evolve>
About:
- A group of math functions, largely generic and using valarrays
Changes:
- 99.8.7: Created.
To Do:
- other sort functions?
- can heapsort be set to work in descending order?
**************************************************************************/
// *** INCLUDES
#include "Sbl.h"
#include <valarray>
#include <cassert>
#include <vector>
#include <numeric>
#include <iterator>
#include <cmath>
#include <iostream>
#include <iomanip>
using std::valarray;
using std::vector;
using std::size_t;
using std::accumulate;
using std::pow;
using std::iterator;
using std::iterator_traits;
using namespace sbl;
using std::setw;
using std::cout;
using std::endl;
// *** INTERFACE *********************************************************/
UInt CountSamplesFromPopulation (UInt iPopulationSize, UInt iSampleSize);
UInt CountCombinations (UInt iPopulationSize, UInt iSampleSize);
UInt Factorial (UInt iOperand);
// SWAP
// Exchange two variables of any type that can be assigned.
template <typename element_t>
void Swap (element_t &ioElem1, element_t &ioElem2)
{
element_t theTmp = ioElem1;
ioElem1 = ioElem2;
ioElem2 = theTmp;
}
// Utility functions for heapsort
inline size_t parent (size_t k)
{
return ((k - 1)/2);
}
inline size_t leftchild (size_t k)
{
return (2*k + 1);
}
inline size_t rightchild (size_t k)
{
return (2*k + 2);
}
// HEAPSORT (valarray)
// A generic function for sorting valarrays in place, in ascending order.
template <typename element_t>
void Heapsort(valarray<element_t> &data)
{
size_t unsorted = data.size();
for (size_t i = 1; i < unsorted; i++)
{
// where i is index of next element to be added to heap &
// k is index of new element as it pushed upward thru heap
size_t k = i;
while ((k > 0) && (data[k] > data[parent(k)]))
{
Swap (data[k], data[parent(k)]);
k = parent(k);
}
}
while (unsorted > 1)
{
unsorted--;
Swap (data[0], data[unsorted]);
size_t current = 0; // index of node that's moving down
size_t big_child_index; // index of larger child of current node
bool heap_ok = false; // is heap correct?
// keep going until heap is okay, and while current node has at
// least a left child (leftchild (current) < iBound)
while ((!heap_ok) && (leftchild (current) < unsorted))
{
// which is the larger child:
if (rightchild (current) >= unsorted)
{
// no right child, so left child must be largest
big_child_index = leftchild(current);
}
else if (data[leftchild (current)] > data[rightchild (current)])
{
// the left child is the bigger
big_child_index = leftchild(current);
}
else
{
// the right child is the bigger
big_child_index = rightchild(current);
}
// Is larger child > the current node? If so, swap
// and continue; otherwise we are finished.
if (data[current] < data[big_child_index])
{
Swap (data[current], data[big_child_index]);
current = big_child_index;
}
else
heap_ok = true;
}
}
}
// HEAPSORT (vector)
// A generic function for sorting vector in place, in ascending order.
template <typename element_t>
void Heapsort(vector<element_t> &data)
{
size_t unsorted = data.size();
for (size_t i = 1; i < unsorted; i++)
{
size_t k = i;
while ((k > 0) && (data[k] > data[parent(k)]))
{
Swap (data[k], data[parent(k)]);
k = parent(k);
}
}
while (unsorted > 1)
{
unsorted--;
Swap (data[0], data[unsorted]);
size_t current = 0; // index of node that's moving down
size_t big_child_index; // index of larger child of current node
bool heap_ok = false; // is heap correct?
while ((!heap_ok) && (leftchild (current) < unsorted))
{
// which is the larger child:
if (rightchild (current) >= unsorted)
{
// no right child, so left child must be largest
big_child_index = leftchild(current);
}
else if (data[leftchild (current)] > data[rightchild (current)])
{
// the left child is the bigger
big_child_index = leftchild(current);
}
else
{
// the right child is the bigger
big_child_index = rightchild(current);
}
// Is larger child > the current node? If so, swap
// and continue; otherwise we are finished.
if (data[current] < data[big_child_index])
{
Swap (data[current], data[big_child_index]);
current = big_child_index;
}
else
heap_ok = true;
}
}
}
// HEAPSORT (array, size)
// A generic function for sorting arrays in place, in ascending order.
template <typename element_t>
void Heapsort (element_t data[], size_t n)
{
size_t unsorted = n;
for (size_t i = 1; i < unsorted; i++)
{
size_t k = i;
while ((k > 0) && (data[k] > data[parent (k)]))
{
Swap (data[k], data[parent(k)]);
k = parent (k);
}
}
while (unsorted > 1)
{
unsorted--;
Swap (data[0], data[unsorted]);
size_t current = 0;
size_t big_child_index;
bool heap_ok = false;
while ((!heap_ok) && (leftchild (current) < unsorted))
{
if (rightchild (current) >= unsorted)
{
big_child_index = leftchild(current);
}
else if (data[leftchild (current)] > data[rightchild (current)])
{
big_child_index = leftchild(current);
}
else
{
big_child_index = rightchild(current);
}
if (data[current] < data[big_child_index])
{
Swap (data[current], data[big_child_index]);
current = big_child_index;
}
else
heap_ok = true;
}
}
}
// *** DEBUG
// DEBUG PRINT (valarray)
// For dumping to screen
template <typename element_t>
void DebugPrint ( valarray<element_t> &data )
{
cout << "*** DEBUG: Contents of valarray at " << &data << endl;
for (int i = 0; i < data.size(); i++)
cout << setw(5) << i << " : " << data[i] << endl;
cout << "*** END DEBUG" << endl;
}
// AVERAGE (valarray)
// Specialise this for ints?
// !! Note that valarray doesn't support iterators.
template <typename element_t>
void Average (valarray<element_t>& data, double& oAvg, double& oVariance)
{
size_t theNumItems = data.size ();
assert (1 < theNumItems);
oAvg = data.sum () / theNumItems;
valarray<element_t> theDevVector = data - oAvg;
element_t theVal, theSumSq = 0;
for (size_t i = 0; i < theNumItems; i++)
{
theVal = theDevVector[i];
theSumSq += theVal * theVal;
}
oVariance = theSumSq / theNumItems;
}
// SUM OF SQUARED DEVIATIONS (valarray)
// Calculates Sum(x_obs - x_mean)^2
/*
template <typename element_t>
double SumSqDev (valarray<element_t>& data)
{
double theMean = sum;
}
*/
// STANDARD DEVIATION
// Given a valarray, calculates & returns the mean and std dev (by
// reference).
template <typename T>
T StdDeviation (T& oStdDev, valarray<T>& ikValues)
{
T theMean;
valarray<T> theDeviations;
theMean = ikValues.sum() / T(ikValues.size());
theDeviations = ikValues - theMean;
theDeviations *= theDeviations;
oStdDev = theDeviations.sum() / T(ikValues.size() - 1);
oStdDev = pow(oStdDev, .5);
return theMean;
}
// STANDARD ERROR
// Given a valarray, calculates & returns the mean and std error (by
// reference).
template <typename T>
T StdError (T& oStdError, valarray<T>& ikValues)
{
T theMean;
T theStdDev;
theMean = StdDeviation (theStdDev, ikValues);
oStdError = theStdDev / sqrt(T(ikValues.size()));
return theMean;
}
/*
template <class InputIter>
inline
typename iterator_traits<InputIter>::value_type
sum (InputIter first, InputIter last, iterator_traits<InputIter>::value_type init = 0)
{
for (; first != last; ++first)
init = init + *first;
return init;
}
*/
// *** END