-
Notifications
You must be signed in to change notification settings - Fork 68
/
main.cpp
41 lines (34 loc) · 1.21 KB
/
main.cpp
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
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <random>
#include "Complex.hpp"
// The code below here should not need to change !!
template<typename T>
void compute(int len, T initial, T step) {
// allocate vectors
std::vector<T> v(len+1), diffs(len+1);
// fill and randomize v
std::generate(v.begin(), v.end(), [value = initial, step]() mutable {
const T cur = value;
value += step;
return cur;
});
std::shuffle(v.begin(), v.end(), std::default_random_engine{});
// compute differences
std::adjacent_difference(v.begin(), v.end(), diffs.begin());
// compute standard deviation of it
const T sum = std::reduce(diffs.begin()+1, diffs.end());
const T sumsq = std::accumulate(diffs.begin()+1, diffs.end(), T(),
[](const T& s, const T& a) { return s + a * a; });
const T mean = sum/len;
const T variance = sumsq/len - mean*mean;
std::cout << "Range = [" << initial << ", " << step*len << "]\n"
<< "Mean = " << mean << '\n'
<< "Variance = " << variance << '\n';
}
int main() {
compute(1000, 0.0, 7.0);
compute(1000, Complex(0,0), Complex(1,2));
}