-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_headers.h
259 lines (218 loc) · 5.38 KB
/
common_headers.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
#pragma once
// The USE_R variable allows us to switch between the R implementation
// and C++ without R. This variable can either be passed to the compiler
// using -DUSE_R=1 or -DUSE_R=0 or by changing the value below. By default,
// lessOptimizers will assume that you are using R and therefor set USE_R=1
// if the variable is not defined otherwise.
#ifndef USE_R
#define USE_R 1
#endif
#if USE_R
// ----------------------
// USING R
// Here, we rely on Rcpp and RcppArmadillo
// ----------------------
// include necessary headers:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// define print, warnings, and errors:
#define print Rcpp::Rcout
#define warn Rcpp::warning
#define error Rcpp::stop
namespace lessSEM
{
// define data types:
typedef Rcpp::NumericVector numericVector;
typedef Rcpp::StringVector stringVector;
// define functions:
inline arma::rowvec toArmaVector(numericVector numVec)
{
arma::rowvec armaVec(numVec.length());
for(unsigned int i = 0; i < numVec.length(); i++){
armaVec(i) = numVec(i);
}
return (armaVec);
}
inline numericVector toNumericVector(arma::rowvec vec)
{
numericVector numVec(vec.n_elem);
for(unsigned int i = 0; i < numVec.length(); i++){
numVec(i) = vec(i);
}
return (numVec);
}
inline stringVector toStringVector(std::vector<std::string> vec)
{
stringVector myStringVec(vec.size());
for(unsigned int i = 0; i < myStringVec.length(); i++){
myStringVec(i) = vec.at(i);
}
return (myStringVec);
}
inline numericVector sample(numericVector vec, int nSamples, bool replace)
{
return (Rcpp::sample(vec, nSamples, replace));
}
inline numericVector unif(int n, double min, double max)
{
return (Rcpp::runif(n, min, max));
}
}
#else
// ----------------------
// NOT USING R
// Here, we replace all Rcpp-specific elements with
// armadillo and custom classes that imitate those of
// Rcpp.
// ----------------------
// include headers:
#include <armadillo>
// define print, warnings, and errors:
#define print std::cout
namespace lessSEM
{
inline void warn(std::string warningMessage)
{
std::cout << "WARNING: " + warningMessage;
}
}
#define error throw std::invalid_argument
// NA
#define NA_REAL arma::datum::nan
// define data types:
namespace lessSEM
{
class stringVector
{
public:
std::vector<std::string> values;
stringVector() {}
stringVector(int n_elem)
{
values.resize(n_elem);
}
stringVector(std::vector<std::string> values_) : values(values_) {}
auto &at(unsigned int where)
{
return (values.at(where));
}
const auto &at(unsigned int where) const
{
return (values.at(where));
}
int size()
{
return (values.size());
}
const int size() const
{
return (values.size());
}
int length()
{
return (values.size());
}
void fill(std::string with)
{
for (auto &value : values)
value = with;
}
};
inline stringVector toStringVector(std::vector<std::string> vec)
{
stringVector myStringVec;
myStringVec.values = vec;
return (myStringVec);
}
class numericVector
{
public:
arma::rowvec values;
stringVector par_names;
numericVector()
{
values.resize(0);
par_names.values.resize(0);
}
numericVector(int n_elem)
{
values.resize(n_elem);
par_names.values.resize(n_elem);
for (int i = 0; i < n_elem; i++)
par_names.values.at(i) = "";
}
numericVector(arma::rowvec vec)
{
values = vec;
par_names.values.resize(values.n_elem);
for (int i = 0; i < values.n_elem; i++)
par_names.values.at(i) = "";
}
numericVector(arma::rowvec vec, std::vector<std::string> labels)
{
values = vec;
par_names.values = labels;
}
// methods
double &at(unsigned int position)
{
return (values(position));
}
double &operator()(unsigned int position)
{
return (this->at(position));
}
friend std::ostream &operator<<(std::ostream &output, const numericVector &numVec)
{
output << numVec.values;
return (output);
}
stringVector &names()
{
return par_names;
}
int length()
{
return (values.n_elem);
}
void fill(const double value)
{
values.fill(value);
}
};
// define functions:
inline arma::rowvec toArmaVector(numericVector numVec)
{
return (numVec.values);
}
inline numericVector toNumericVector(arma::rowvec vec)
{
return (numericVector(vec));
}
inline numericVector sample(numericVector vec, int nSamples, bool replace)
{
arma::rowvec values = vec.values;
std::vector<std::string> labels = vec.par_names.values;
arma::rowvec positions(vec.length());
for (int i = 0; i < vec.length(); i++)
positions(i) = i;
positions = arma::shuffle(positions);
for (unsigned int i = 0; i < positions.n_elem; i++)
{
vec.values(i) = values(positions(i));
vec.par_names.values.at(i) = labels.at(i);
}
return (vec);
}
inline numericVector unif(int n, double min, double max)
{
std::default_random_engine generator;
std::uniform_real_distribution<double> unif_dist(min, max);
arma::vec tst(n);
numericVector ret(n);
for (int i = 0; i < n; i++)
ret(i) = unif_dist(generator);
return (ret);
}
}
#endif