-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.cpp
89 lines (70 loc) · 1.75 KB
/
library.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
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
#include "library.h"
#include "Eigen/Dense"
#include <iostream>
#include <algorithm>
using namespace std;
int choice(int n, int k) {
if (k > n) {
return 0;
}
int r = 1;
for (int d = 1; d <= k; ++d) {
r *= n--;
r /= d;
}
return r;
}
template<typename T>
auto combinations(vector <T> A, int k) {
auto N = A.size();
vector <vector<T>> combinations;
string select(k, 1);
select.resize(N, 0);
do {
vector <T> c;
for (auto i = 0; i < N; i++) {
if (select[i]) {
c.push_back(A[i]);
}
}
combinations.push_back(c);
} while (prev_permutation(select.begin(), select.end()));
return combinations;
}
template<typename T>
auto kcombinations(vector <vector<T>> A, int k) {
vector <vector<T>> seconds;
for (auto a : A) {
for (auto b : combinations(a, k)) {
vector <T> combined;
combined.insert(combined.end(), a.begin(), a.end());
combined.insert(combined.end(), b.begin(), b.end());
seconds.push_back(combined);
}
}
return seconds;
}
template<typename T>
auto toEigen(vector <T> A, int n) {
Eigen::MatrixXf eigen(n, n);
int k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
eigen(i, j) = A[k++];
}
}
return eigen;
}
template<typename T>
auto buildMatrix(vector <T> selection) {
vector <T> square;
int N = selection.size();
int i = 0;
sort(selection.begin(), selection.end());
while (i < N) {
square.insert(square.end(), selection.begin(), selection.end());
i++;
next_permutation(selection.begin(), selection.end());
}
return toEigen(square, N);
}