This repository was archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
125 lines (106 loc) · 2.84 KB
/
util.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
#pragma once
#include <set>
#include <vector>
#include <numeric>
#include <iterator>
#include <algorithm>
using std::size_t;
void design1(const std::vector<int> &g, std::vector< std::vector<double> > &x);
void design2(const std::vector<int> &g, std::vector< std::vector<double> > &x);
void design3(const std::vector<int> &g, std::vector< std::vector<double> > &x);
double fpval(double x, double df1, double df2);
template<typename T1, typename T2>
size_t index(const std::vector<T1> &v, const T2 &a)
{
return std::distance(v.begin(), std::find(v.begin(), v.end(), a));
}
template<typename T>
std::vector<size_t> order(const std::vector<T> &v)
{
std::vector<size_t> z(v.size());
std::iota(z.begin(), z.end(), size_t(0));
std::sort(z.begin(), z.end(), [&v](size_t i, size_t j) { return v[i] < v[j]; });
return z;
}
template<typename T>
std::vector<T> unique(std::vector<T> v)
{
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());
return v;
}
template<typename T>
std::vector<T> stable_unique(std::vector<T> v)
{
std::set<T> seen;
auto last = v.begin();
for (auto itr = v.begin(); itr != v.end(); ++itr) {
if (seen.insert(*itr).second) {
if (last != itr)
*last = *itr;
++last;
}
}
v.erase(last, v.end());
return v;
}
template<typename T1, typename T2>
std::vector<T1> subset(const std::vector<T1> &v, const std::vector<T2> &idx)
{
std::vector<T1> z;
z.reserve(idx.size());
for (auto i : idx)
z.push_back(v[i]);
return z;
}
template<typename T>
std::vector<T> subset(const std::vector<T> &v, const std::vector<bool> &mask)
{
std::vector<T> z;
z.reserve(std::count(mask.begin(), mask.end(), true));
size_t n = v.size();
for (size_t i = 0; i < n; ++i) {
if (mask[i])
z.push_back(v[i]);
}
return z;
}
template<typename T>
std::vector<T> intersect(std::vector<T> a, std::vector<T> b)
{
std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end());
std::vector<T> c;
std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(c));
return c;
}
template<typename T>
std::vector<int> factor(const std::vector<T> &v)
{
std::vector<int> gi;
auto u = unique(v);
gi.reserve(v.size());
for (auto &e : v)
gi.push_back(index(u, e));
return gi;
}
template<typename T>
void factor(const std::vector<T> &v, std::vector<T> &gn, std::vector<int> &gi)
{
auto u = unique(v);
gi.clear();
gi.reserve(v.size());
for (auto &e : v)
gi.push_back(index(u, e));
gn.swap(u);
}
template<typename T>
double sumsqc(const std::vector<T> &v)
{
auto sx = std::accumulate(v.begin(), v.end(), T(0));
double mx = sx / v.size();
double ssx = 0;
for (auto x : v)
ssx += (x - mx)*(x - mx);
return ssx;
}