-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
278 lines (242 loc) · 6.2 KB
/
test.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
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
260
261
262
263
264
265
//[[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <unordered_set>
using namespace Rcpp;
using namespace std;
// This is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp
// function (or via the Source button on the editor toolbar). Learn
// more about Rcpp at:
//
// http://www.rcpp.org/
// http://adv-r.had.co.nz/Rcpp.html
// http://gallery.rcpp.org/
//
// [[Rcpp::export]]
double meanC(NumericVector x, bool na_rm) {
double result = 0;
int n = x.size();
int temp_n = 0;
for (int i = 0; i < n; i++) {
if (NumericVector::is_na(x[i])) {
if (na_rm) {
temp_n += 1;
} else {
return NA_REAL;
}
} else {
result += x[i];
}
}
return result / (n - temp_n);
}
// [[Rcpp::export]]
bool allC(LogicalVector x) {
int n = x.size();
for(int i = 0; i < n; i++) {
if (!x[i]) {
return false;
}
}
return true;
}
// [[Rcpp::export]]
NumericVector cumsumC(NumericVector x, bool na_rm) {
int n = x.size(), j = 0, na_n = 0;
double temp = 0;
NumericVector res(n);
for (int i = 0; i < n; i++) {
if (!NumericVector::is_na(x[i]) | !na_rm) {
temp += x[i];
res[j] = temp;
j++;
} else if (NumericVector::is_na(x[i]) & na_rm) {
na_n += 1;
continue;
}
}
NumericVector result(n - na_n);
for (int i = 0; i < (n - na_n); i++) {
result[i] = res[i];
}
return result;
}
// [[Rcpp::export]]
NumericVector cumprodC(NumericVector x) {
int n = x.size();
double temp = 1;
NumericVector result(n);
for (int i = 0; i < n; i++) {
temp *= x[i];
result[i] = temp;
}
return result;
}
// [[Rcpp::export]]
NumericVector cumminC(NumericVector x) {
int n = x.size();
NumericVector result(1);
result[0] = x[0];
for (int i = 1; i < n; i++) {
if (x[i] <= min(result)) {
result.push_back(x[i]);
} else {
result.push_back(result[i-1]);
}
}
return result;
}
// [[Rcpp::export]]
NumericVector cummaxC(NumericVector x) {
int n = x.size();
NumericVector result(1);
result[0] = x[0];
for (int i = 1; i < n; i++) {
if (x[i] >= max(result)) {
result.push_back(x[i]);
} else {
result.push_back(result[i-1]);
}
}
return result;
}
// [[Rcpp::export]]
NumericVector diffC(NumericVector x, int lag) {
int n = x.size();
if (lag >= n) {
return false;
}
int m = n - lag;
NumericVector result(m);
for (int i = 0; i < m; i++) {
result[i] = x[i + lag] - x[i];
}
return result;
}
//[[Rcpp::export]]
double sumC(NumericVector x) {
double result = 0;
NumericVector::iterator it;
for (it = x.begin(); it != x.end(); ++it) {
result += *it;
}
return result;
}
//[[Rcpp::export]]
double sumC_std(NumericVector x) {
return std::accumulate(x.begin(), x.end(), 0.0);
}
//[[Rcpp::export]]
IntegerVector findintervalC(NumericVector x, NumericVector breaks) {
IntegerVector result(x.size());
NumericVector::iterator it, pos;
IntegerVector::iterator out;
for (it = x.begin(), out = result.begin(); it != x.end(); ++it, ++out) {
pos = std::upper_bound(breaks.begin(), breaks.end(), *it);
*out = std::distance(breaks.begin(), pos);
}
return result;
}
//[[Rcpp::export]]
List rleC(NumericVector x) {
std::vector<double> len_res;
std::vector<int> val_res;
NumericVector::iterator it;
int i = 0;
double temp = x[0];
len_res.push_back(1);
val_res.push_back(temp);
for (it = x.begin() + 1; it != x.end(); ++it) {
if (*it != temp) {
val_res.push_back(*it);
len_res.push_back(1);
temp = *it;
i++;
} else {
len_res[i] += 1;
}
}
return List::create(_["length"] = len_res, _["value"] = val_res);
}
//[[Rcpp::export]]
double medianC(NumericVector x) {
int n = x.size();
std::partial_sort(x.begin(), x.end(), x.end());
if (n % 2 == 1) {
return x[n / 2];
} else {
return (x[n / 2 - 1] + x[n / 2]) / 2;
}
}
//[[Rcpp::export]]
LogicalVector valueMatchC(NumericVector x, NumericVector y) {
std::unordered_set<double> temp;
LogicalVector result;
NumericVector::iterator it;
int pos;
for (it = y.begin(); it != y.end(); ++it) {
temp.insert(*it);
}
for (it = x.begin(); it != x.end(); ++it) {
pos = temp.count(*it);
if (pos == 0) {
result.push_back(false);
} else {
result.push_back(true);
}
}
return result;
}
//[[Rcpp::export]]
unordered_set<double> uniqueC(NumericVector x) {
std::unordered_set<double> out;
NumericVector::iterator it;
for (it = x.begin(); it != x.end(); ++it) {
out.insert(*it);
}
return out;
}
//[[Rcpp::export]]
List minmaxC(NumericVector x) {
double min_x = x[0], max_x = x[0];
NumericVector::iterator it;
for (it = x.begin(); it != x.end(); ++it) {
min_x = std::min(min_x, *it);
max_x = std::max(max_x, *it);
}
return List::create(_["min"] = min_x, _["max"] = max_x);
}
//[[Rcpp::export]]
double whichminC(NumericVector x) {
double temp = *std::min_element(x.begin(), x.end());
for (int i = 0; i < x.size(); ++i) {
if (x[i] == temp) {
return i;
}
}
return 0;
}
// [[Rcpp::export]]
NumericMatrix cppgibbs(int N, int thin) {
NumericMatrix mat(N, 2);
double x = 0, y = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < thin; ++j) {
x = ::Rf_rgamma(3.0, 1.0 / (y * y + 4));
y = ::Rf_rnorm(1.0 / (x + 1), 1.0 / sqrt(2 * (x + 1)));
}
mat(i, 0) = x;
mat(i, 1) = y;
}
return mat;
}
// You can include R code blocks in C++ files processed with sourceCpp
// (useful for testing and development). The R code will be automatically
// run after the compilation.
//
/*** R
whichminC(c(6:1, 2:3))
*/