-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
128 lines (97 loc) · 3.47 KB
/
main.cpp
File metadata and controls
128 lines (97 loc) · 3.47 KB
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
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "library.cpp"
#include <future>
using namespace Eigen;
TEST_CASE("7 choose 4 and 4 choose 3 is 140") {
REQUIRE(choice(7, 4) * choice(4, 3) == 140);
}
TEST_CASE("Build Square Matrix") {
vector<float> numbers = {1, 2, 3, 4, 5, 6, 7};
auto matrix = buildMatrix(numbers);
cout << matrix;
}
TEST_CASE("To Eigen") {
vector<float> numbers = {1, 2, 1, 2, 1, 0, -1, 1, 2};
auto eigen = toEigen(numbers, 3);
cout << eigen << endl << endl;
cout << eigen.inverse() << endl;
}
SCENARIO("7 choose 4 produces 35 combinations") {
GIVEN("I have seven unique numbers") {
vector<int> numbers = {1, 2, 3, 4, 5, 6, 7};
WHEN("I choose 4 from the numbers") {
auto combination = combinations(numbers, 4);
THEN("I get 35 combinations") {
REQUIRE(combination.size() == 35);
}
}
}
}
SCENARIO("4 choose 3 produces 4 combinations") {
GIVEN("I have seven unique numbers") {
vector<int> numbers = {1, 2, 3, 4};
WHEN("I choose 4 from the numbers") {
auto combination = combinations(numbers, 3);
THEN("I get 35 combinations") {
REQUIRE(combination.size() == 4);
}
}
}
}
SCENARIO("7 choose 4 then 4 choose 3") {
GIVEN("I have seven unique numbers") {
vector<int> numbers = {1, 2, 3, 4, 5, 6, 7};
WHEN("I choose 4 from 7 then 3 from 4 numbers") {
auto combination = kcombinations(combinations(numbers, 4), 3);
THEN("I have 140 combinations") {
REQUIRE(combination.size() == 140);
}
}
}
}
SCENARIO("7 choose 4 then 4 choose 3 the permute") {
GIVEN("I have seven numbers") {
vector<float> numbers = {1, 2, 1, 3, 1, 0, -1};
WHEN("I choose 4 from 7 then 3 from 4 numbers and invert them") {
auto combination = kcombinations(combinations(numbers, 4), 3);
THEN("I have 140 combinations") {
REQUIRE(combination.size() == 140);
}
auto invert = [](vector<float> c) -> pair<bool,string> {
auto mat = buildMatrix(c);
stringstream msg;
msg << "Combinations:" << endl;
for (auto e : c)
msg << e << " ";
msg << endl << endl;
msg << "Matrix:" << endl << mat << endl << endl;
if (mat.determinant() == 0) {
msg << "Not Invertible" << endl;
return make_pair(false, msg.str());
}
msg << "Inverse:" << endl << mat.inverse() << endl << endl;
return make_pair(true, msg.str());
};
// Calculate them asynchronously
vector<future<pair<bool, string>>> futures(combination.size());
for (auto c : combination) {
futures.push_back(async(launch::async, invert, c));
}
// Get the invertible combinations
int invertibles = 0;
for (auto &f : futures) {
if (f.valid()){
auto pair = f.get();
if(pair.first) {
invertibles++;
cout << pair.second << endl;
}
}
}
THEN("I have 10 which are invertible") {
REQUIRE(invertibles == 10);
}
}
}
}